Solved

How to create a grid and display the item at (0,0)?

Posted on 2006-10-28
11
278 Views
Last Modified: 2010-03-31
Hi experts,

I have a question on java, I want to create a 21x21 grid (from -10 to +10 on each
axis). Furthermore, I want my first item to be displayed at the centre, at position (0,0).

I do not know how to go about starting it, please enlighten!

hy
0
Comment
Question by:heyhey84
  • 7
  • 4
11 Comments
 
LVL 6

Expert Comment

by:valipotor
ID: 17824979
Hi
Use the GridLayout.

valipotor
0
 
LVL 6

Expert Comment

by:valipotor
ID: 17824981
0
 

Author Comment

by:heyhey84
ID: 17824995
valipotor,

I've searched the page before, but can i don't use GridLayout?
Like just delcare it as a private class, and call it.

Furthermore, is there any sample codes? because I'm clueless in starting it!
Thanks in advance.

hy
0
 
LVL 6

Expert Comment

by:valipotor
ID: 17824998
You don't want to use GridLayout?
Or you don't know how?
0
 

Author Comment

by:heyhey84
ID: 17825011
Frankly, I don't know how to

and I thought that the private method is easier?? please feel free to correct me!!
Thanks for the help.

hy
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:valipotor
ID: 17825076
import java.awt.*;
 import java.applet.Applet;
 public class ButtonGrid extends Applet {
     public void init() {
         setLayout(new GridLayout(21,21));
         for(int i=0;i<21;i++)
           for(int j=0;j<21;j++)
          {
            add(new JLabel());
           if (i==11 & j==11)
           {
             //this is the center cell
           }
         }
     }
 }
0
 

Author Comment

by:heyhey84
ID: 17825188
thanks for the help so far,

if I use GridLayout, it will pop a window to display the grid,
but I just want to print the grid (as in white spaces) on the testing platform (unix or linux),
therefore cannot use applet, apologise that I did not state clearly in my question,

is there any alternative rather then the GridLayout option?

hy
0
 
LVL 6

Expert Comment

by:valipotor
ID: 17825333
  for(int i=0;i<21;i++)
           for(int j=0;j<21;j++)
          {
            System.out.print("[ ]");
           if (i==11 & j==11)
           {
             //this is the center cell
           }
System.out.println();
         }
0
 

Author Comment

by:heyhey84
ID: 17828413
valipotor,

I've tried using the code above, it gave a very large output, below are only part of the output, when I tried to print out something within the if statement,

           if (i==11 & j==11)
           {
             System.out.print("?");
           }
 
the printout is as follows, care to advise?? Thanks in advance.

[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]?

[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]

hy
0
 
LVL 6

Expert Comment

by:valipotor
ID: 17828427
Sorry, here is the correct form:

for(int i=0;i<21;i++)
{
           for(int j=0;j<21;j++)
           {
               System.out.print("[ ]");
               if (i==11 & j==11)
               {
                     //this is the center cell
                }
         }
System.out.println();
}
0
 
LVL 6

Accepted Solution

by:
valipotor earned 20 total points
ID: 17828429
A better version:

for(int i=0;i<21;i++)
{
           for(int j=0;j<21;j++)
           {
               
               if (i==11 & j==11)
               {
                     //this is the center cell
                     System.out.print("[x]");
                }            
                else
                {
                     System.out.print("[ ]");
                }
         }
System.out.println();
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now