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