1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| package draw;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ShapesPanel extends JPanel{
private ArrayList<Drawable> shapes = new ArrayList<Drawable>();
/**
* @return the shapes
*/
public ArrayList<Drawable> getShapes() {
return shapes;
}
/**
* @param shapes the shapes to set
*/
public void setShapes(ArrayList<Drawable> shapes) {
this.shapes = shapes;
}
/**
*
* @param d The shape to add
*/
public void addShape(Drawable d) {
this.shapes.add(d);
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
for(Drawable d : shapes) {
d.draw(g);
}
}
public static void main(String[] args) {
Circle c = new Circle(new Point(100, 200), Color.yellow, Color.blue, Color.blue, 150);
Rectangle rec = new Rectangle(new Point(400, 300), Color.blue.brighter().brighter().brighter(), Color.blue.darker().darker(), Color.blue.darker().darker(), 50, 100);
ShapesPanel panel = new ShapesPanel();
panel.addShape(c);
panel.addShape(rec);
JFrame f = new JFrame();
f.add(panel);
f.setSize(800, 600);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
} |