4 Replies - 756 Views - Last Post: 16 March 2012 - 11:54 PM Rate Topic: -----

#1 ecksphive  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 24-November 11

2D array of Rectangles

Posted 16 March 2012 - 05:47 AM

Hi all,
I am trying to create a 2D array of Rectangles, and that works fine, however what wont work is when i try to set a dimension for one of the Rectangles in the array and draw it. My code is:

 import java.awt.*;
 import java.awt.geom.*;
 import javax.swing.*;
 import java.awt.Graphics2D;
 
public class DrawingPanel extends JPanel 
{
    Graphics2D MyG;
    Rectangle[][] rect = new Rectangle[3][3];

    DrawingPanel () 
    { 
        setBackground (Color.WHITE);
    } 
    
    public void paint(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setColor(Color.BLACK);
        update(g);
    }
    
    public void update(Graphics g)
    {
    }    
}


However when i try something like
rect[0][0] = new Rectangle (0,0,25,25);
it gives me an error. And even when i try
Rectangle r1 = new Rectangle (0,0,25,25);
rect[0][0] = r1;

it gives me an error about 'expecting a ]'

Any advice?
x5

Is This A Good Question/Topic? 0
  • +

Replies To: 2D array of Rectangles

#2 pbl  Icon User is offline

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

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: 2D array of Rectangles

Posted 16 March 2012 - 06:29 AM

First you overload the update(Graphics g) method, you just can't do that
JComponent have a update(Graphics g) method, I don't know if you were planning to do your own, yu will have to give it another name
Second, paint() should not call update() because update() calls paint() (at least the JComponent update() method) so you would be in an infine loop.

As far as your error is concerned, we will need more code and the context. Their is no error on those two lines of code.
Was This Post Helpful? 0
  • +
  • -

#3 ecksphive  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 24-November 11

Re: 2D array of Rectangles

Posted 16 March 2012 - 07:09 AM

That was all the code i had. (save a seperate class that calls on the DrawingPanel). So with the method that i was trying it would look like this:
 import java.awt.*;
 import java.awt.geom.*;
 import javax.swing.*;
 import java.awt.Graphics2D;
 
public class DrawingPanel extends JPanel 
{
    Graphics2D MyG;
    Rectangle[][] rect = new Rectangle[3][3];
    Rectangle r1 = new Rectangle (0,0,25,25);
    rect[0][0] = r1;
    // or comment that out and uncomment the line beneath
    //rect[0][0] = new Rectangle (0,0,25,25);

    DrawingPanel () 
    { 
        setBackground (Color.WHITE);
    } 
    
    public void paint(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setColor(Color.BLACK);
        update(g);
    }
    
    public void update(Graphics g)
    {
    }    
}


The exact error i get is: ']' expected and it highlights Rectangle rect[*here*0][0] on both codes
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: 2D array of Rectangles

Posted 16 March 2012 - 07:15 AM

This is code
    Rectangle r1 = new Rectangle (0,0,25,25);
    rect[0][0] = r1;


It should be in a method or in your constructor. It cannot be in the variables declaration.

In the constructor is dangerous :)
Building your new Rectangle() you will try to build r1 which will call your constructor which will try to create a new r1 which will... so infinite loop
Was This Post Helpful? 0
  • +
  • -

#5 ecksphive  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 24-November 11

Re: 2D array of Rectangles

Posted 16 March 2012 - 11:54 PM

Thankyou :) That worked :) I will watch out for that infinitie loop :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1