代码如下:(拜托各位看一下,在Drawing class里面那部分如何实现?思路也好啊!)
package com.abc.draw.demo;import java.awt.event.*;import javax.swing.*;import com.abc.draw.*;
import com.abc.draw.geometry.*;public class DrawDemo {
    private static Drawing createDrawing() {
        Drawing drawing = new Drawing();        Point p1 = new Point(50.0, 5.0);
        Point p2 = new Point(300.0, 50.0);
        Line line = new Line(p1, p2);
        drawing.append(line);        Triangle t1 = new Triangle(
            new Point(10.0, 20.0),
            new Point(10.0, 300.0),
            new Point(50.0, 50.0));
        drawing.append(t1);        Triangle t2 = new Triangle(
            new Point(100.0, 100.0),
            new Point(380.0, 170.0),
            new Point(150.0, 300.0));
        drawing.append(t2);        Square s = new Square(new Point(200.0, 200.0), 300.0);
        s.setWidth(200.0); // just to see that this method works
        drawing.append(s);        Rectangle rect1 = new Rectangle(new Point(400.0, 50.0), 300.0, 1.0);
        rect1.setHeight(130.0); // just to see that this method works
        drawing.append(rect1);        return drawing;
    }    public static void main(String[] args) {
        Drawing drawing = createDrawing();
        DrawingComponent drawingComponent = new DrawingComponent(drawing);        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame f = new JFrame("DrawDemo");
        f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        f.setContentPane(drawingComponent);
        f.setSize(800, 450);
        f.setVisible(true);
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}package com.abc.draw;import java.awt.*;import javax.swing.*;public class DrawingComponent extends JComponent {
private Drawing drawing; public DrawingComponent(Drawing drawing) {
        this.drawing = drawing;
} protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
        
g2.setPaint(Color.YELLOW);
        Dimension size = getSize();
g2.fillRect(0, 0, size.width, size.height); g2.setPaint(Color.BLUE);
drawing.drawAll(g2);
}
}package com.abc.draw;import java.awt.*;public interface Drawable {
void draw(Graphics2D g2);
}
package com.abc.draw.geometry;import java.awt.*;import com.abc.draw.*;public class Line implements Drawable {
    private Point p1;
    private Point p2;
    
    public Line(Point p1, Point p2) {
        this.p1 = p1;
        this.p2 = p2;
    }
    
    public Point getP1() {
        return p1;
    }
    
    public Point getP2() {
        return p2;
    }
    
    public void draw(Graphics2D g2) {
        int x1 = (int) Math.round(p1.getX());
        int y1 = (int) Math.round(p1.getY());
        int x2 = (int) Math.round(p2.getX());
        int y2 = (int) Math.round(p2.getY());
        
        g2.drawLine(x1, y1, x2, y2);
    }
}package com.abc.draw.geometry;public class Point {
private double x;
private double y; public Point(double x, double y) {
this.x = x;
this.y = y;
} public double getX() {
return x;
} public double getY() {
return y;
}
}
package com.abc.draw;import java.awt.*;public class Drawing extends Object {
    // FIXME - probably need a member variable or two...
    
public Drawing() {
        // FIXME - initialize
} public void drawAll(Graphics2D g2) {
        // FIXME - Invoke draw(Graphics2D) on each of the Drawable's that
        //         have been added via append(Drawable).
} public void append(Drawable drawable) {
    // FIXME - Add the passed Drawable to the list of items that make up
        //         this Drawing.
}
}