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
Solved

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

Posted on 2006-10-28
11
280 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
difference of if loops 23 60
login form jsp example 2 35
difference between sorce folder and folder in eclipise 3 28
going to wrong jsp page 2 33
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

808 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