Solved

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

Posted on 2006-10-28
11
281 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jboss wildfly 10.1 10 437
How do I remove an object from a 3 53
Eclipse for Java EE development 2 45
passing enum to a method 4 36
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

726 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