像这样的东西:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
class ColoredShapes {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
JPanel gui = new JPanel(new BorderLayout(5,5));
final ArrayList<Drawable> drawables = new ArrayList<Drawable>();
final PaintPanel paintPanel = new PaintPanel(drawables);
gui.add(paintPanel, BorderLayout.CENTER);
final Random r = new Random();
JToolBar tools = new JToolBar();
gui.add(tools, BorderLayout.PAGE_START);
JButton addCircle = new JButton("Add Circle");
ActionListener addCircleListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Point p = new Point(r.nextInt(400), r.nextInt(400));
int s = r.nextInt(50) + 50;
Color c = new Color(
r.nextInt(255), r.nextInt(255), r.nextInt(255));
drawables.add(new DrawableCircle(p, s, c));
paintPanel.repaint();
}
};
tools.add(addCircle);
addCircle.addActionListener(addCircleListener);
JButton addSquare = new JButton("Add Square");
ActionListener addSquareListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Point p = new Point(r.nextInt(400), r.nextInt(400));
int s = r.nextInt(50) + 50;
Color c = new Color(
r.nextInt(255), r.nextInt(255), r.nextInt(255));
drawables.add(new DrawableSquare(p, s, c));
paintPanel.repaint();
}
};
tools.add(addSquare);
addSquare.addActionListener(addSquareListener);
JOptionPane.showMessageDialog(null, gui);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
interface Drawable {
public abstract void draw(Graphics2D g);
public abstract Color getColor();
}
class DrawableCircle implements Drawable {
private Color color;
private Shape shape;
DrawableCircle(Point topLeft, int size, Color color) {
shape = new Ellipse2D.Double(topLeft.x, topLeft.y, size, size);
this.color = color;
}
public void draw(Graphics2D g) {
g.setColor(getColor());
g.fill(shape);
}
public Color getColor() {
return color;
}
}
class DrawableSquare implements Drawable {
private Color color;
private Shape shape;
DrawableSquare(Point topLeft, int size, Color color) {
shape = new Rectangle2D.Double(topLeft.x, topLeft.y, size, size);
this.color = color;
}
public void draw(Graphics2D g) {
g.setColor(getColor());
g.fill(shape);
}
public Color getColor() {
return color;
}
}
class PaintPanel extends JPanel {
Dimension preferredSize = new Dimension(400,400);
ArrayList<Drawable> drawables;
PaintPanel(ArrayList<Drawable> drawables) {
this.drawables = drawables;
}
public Dimension getPreferredSize() {
return preferredSize;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for (Drawable drawable : drawables) {
drawable.draw(g2);
}
}
}