Creating a Grid

grid wont display

Page 1 of 1

7 Replies - 1169 Views - Last Post: 16 September 2009 - 04:55 PM Rate Topic: -----

#1 paperLocust  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 04-August 09

Creating a Grid

Post icon  Posted 16 September 2009 - 10:20 AM

I am creating a grid of cells that will hold objects. The problem is they wont display completely. I wish I could show the output but only part of the first cell will display, the second wont display at all, and the third is complete. I think it might be because I have the paint method in the cell class and should try paint after I have created the array but i don't know for certain. Here is the code:

import java.awt.Graphics;

import javax.swing.JComponent;

public class Cell extends JComponent{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public Cell(){
		
	}
	public Cell(int x, int y){
		locX = x;
		locY = y;
		setLocation(locX, locY);
		setSize(50, 50);
	}
	public void setInsideObject(Object b){
		inside = b;
	}
	public boolean getInside(){
		if(inside != null){
			return true;
		} else{
			return false;
		}
	}
	public void paint(Graphics g){
		g.drawRect(locX, locY, 50, 50);
	}
	private Object inside;
	private int locX;
	private int locY;
}



import javax.swing.JFrame;
import javax.swing.SwingUtilities;


public class Test {

	public Test(){
		JFrame.setDefaultLookAndFeelDecorated(true);
		JFrame frame = new JFrame("Testing Environment");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(500, 500);
		int x, y;
		/*for(int i=0; i<10; i++){
			x = i*50;
			y = 0;
			cells[i] = new Cell(x, y);
		}*/
		Cell cell1 = new Cell(0,0);
		Cell cell2 = new Cell(50,0);
		Cell cell3 = new Cell(100,0);
		cells[0] = cell1;
		cells[1] = cell2;
		cells[2] = cell3;
		for(int i=0; i<3; i++){
			frame.getContentPane().add(cells[i]);
		}
		frame.setVisible(true);
	}
	public static void main(String args[]){
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				new Test();
			}
		});
	}
	private Cell[] cells = new Cell[10];
	private Grid grid = new Grid();
}


Is This A Good Question/Topic? 0
  • +

Replies To: Creating a Grid

#2 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Creating a Grid

Posted 16 September 2009 - 10:48 AM

Sir, do you mind posting your Grid class as well?
Was This Post Helpful? 0
  • +
  • -

#3 paperLocust  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 04-August 09

Re: Creating a Grid

Posted 16 September 2009 - 11:03 AM

View PostAntonWebsters, on 16 Sep, 2009 - 09:48 AM, said:

Sir, do you mind posting your Grid class as well?



It is never used, that is why I didn't post it.
Was This Post Helpful? 0
  • +
  • -

#4 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Creating a Grid

Posted 16 September 2009 - 11:24 AM

Modify the for loop in your code like this...
for(int i=0; i<3; i++){
			frame.getContentPane().add(cells[i]);
			frame.setVisible(true);

		}


Is that how you want it to be? The three rectangles are visible now.

This post has been edited by AntonWebsters: 16 September 2009 - 11:24 AM

Was This Post Helpful? 1
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Creating a Grid

Posted 16 September 2009 - 01:03 PM

View PostAntonWebsters, on 16 Sep, 2009 - 10:24 AM, said:

Modify the for loop in your code like this...
for(int i=0; i<3; i++){
			frame.getContentPane().add(cells[i]);
			frame.setVisible(true);

		}


Is that how you want it to be? The three rectangles are visible now.


Yes, in order for the cells to display on the screen, they have to be added to the JFrame which represents the screen
Was This Post Helpful? 0
  • +
  • -

#6 paperLocust  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 04-August 09

Re: Creating a Grid

Posted 16 September 2009 - 01:06 PM

View PostAntonWebsters, on 16 Sep, 2009 - 10:24 AM, said:

Modify the for loop in your code like this...
for(int i=0; i<3; i++){
			frame.getContentPane().add(cells[i]);
			frame.setVisible(true);

		}


Is that how you want it to be? The three rectangles are visible now.



Thank you that did it!
Was This Post Helpful? 0
  • +
  • -

#7 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Creating a Grid

Posted 16 September 2009 - 01:13 PM

No problem, sir. Glad I've helped. =)

This post has been edited by AntonWebsters: 16 September 2009 - 01:13 PM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Creating a Grid

Posted 16 September 2009 - 04:55 PM

Have your Cell to extends JLabel rather than JComponent
put a Border around them

then you won't even have to overload the paint() method
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1