Solved

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

Posted on 2006-10-28
11
277 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

747 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

11 Experts available now in Live!

Get 1:1 Help Now