Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-10-28
11
Medium Priority
?
284 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

650 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