Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java2D outputing shapes from an array 1

Status
Not open for further replies.

mfc2005

Programmer
Jul 25, 2005
8
GB
Hi,

I am trying draw a number of shapes that are stored in an array, currently the program is not drawing at all. I have a basic understanding of how the paint method works and how to draw shapes that are hard coded am struggling shapes stored in a collection, is this the right way to do this?

here's the code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class PaintTest extends JComponent
{

private ArrayList shapeList;
private Rectangle2D rect;
private Rectangle2D rect2;
int t;



public PaintTest()
{
shapeList = new ArrayList();
rect = new Rectangle2D.Float(10, 20, 70, 90);
rect = new Rectangle2D.Float(100, 20, 50, 90);
shapeList.add(rect);
shapeList.add(rect2);
repaint();

}

public void paint(Graphics g)

{


Graphics2D g2 = (Graphics2D)g;


while(t<shapeList.size())
{
Shape s = (Shape)shapeList.get(t);
g2.setPaint(Color.blue);
g2.draw(s);
}



}

public static void main(String[] args)
{
JFrame frame = new JFrame("StereoViewer");
Container c = frame.getContentPane();
c.setLayout(new BorderLayout());
c.add(new PaintTest(), BorderLayout.CENTER);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

}
}


Cheers

 
You forgot to increment t in your while loop,
change it to
Code:
        t = 0;
        while(t<shapeList.size())
        {
            Shape s = (Shape)shapeList.get(t);
            g2.setPaint(Color.blue);
            g2.draw(s);
            t++;
        }

There is also a typo in the constructor - you're assigning a value to rect twice, and rect2 remains null
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top