requirement:

A drawing is defined as a 2D space (canvas) that contains an unlimited number of drawing primitives.  
Canvas has initial size, and it uses a unified x, y coordinate system (left corner is 0.0, 0.0).
Canvas can set a background color (black, white, grey, red, green, blue, or yellow) and a border type (thin, heavy, none)
Each drawing primitives (element) in canvas must have its center within the range of canvas.
 
There are 4 drawing primitives (element) defined:
 
1. dot 
2. ellipse (width, height)
3. rectangle (width, height)
4. polygon (diameter, vertex)
 
Element uses the centre point to represent its location (x, y)
Element can be set different foreground color (black, white, grey, red, green, blue, or yellow)
Element can use a different line type (solid, dash, dot) to display
 
Canvas can support zoom in/out operation, and at the same time, elements in drawing also support zoom in/out with the same ratio as canvas. (zoom in/out is based on its centre point)
 
Your software needs to support a draw function to show the result of drawing.  It is not required to use DC to draw the canvas and those elements on the screen.  

import java.awt.BorderLayout;
import java.awt.Choice;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.Border;

/**
 * The <code>SimpleDraw</code> class describes draw simple figures.
 */
public class SimpleDraw extends JFrame {

    // constant value
    private static final int DEFAULT_WIDTH = 600;

    private static final int DEFAULT_HEIGHT = 400;

    private static final int GRIDLAYOUT_ROWS = 3;

    private static final int GRIDLAYOUT_COLS = 5;

    private static final int DEFAULT_HGAP = 5;

    private static final int DEFAULT_VGAP = 2;

    private static final String DEFAULT_FIGURE_POSITION_X = "100";

    private static final String DEFAULT_FIGURE_POSITION_Y = "100";

    private static final String DEFAULT_FIGURE_WIDTH = "100";

    private static final String DEFAULT_FIGURE_HEIGHT = "100";
    
    private static final String BORDERTYPETHIN = "Thin";
    
    private static final String BORDERTYPEHEAVY = "Heavy";
    
    private static final String BORDERTYPENONE = "None";

    // component of GUI
    private static JTextField positionX;

    private static JTextField positionY;

    private static JTextField width;

    private static JTextField height;

    private static JRadioButton rectButton;

    private static Container container;

    private static JPanel figurePanel;

    private JRadioButton EllipsButton;
    
    private JRadioButton dotButton;
    
    private JRadioButton polyButton;    

    private JButton drawButton;

    private JButton clearButton;

    private JLabel chooseLabel;

    private ButtonGroup buttonGroup;
    
    private Choice colorChooser;
    
    private static Choice typeChooser;
    
    private Border border;
    
    private Border configBorder;

    private JPanel configPanel;

    private JPanel drawPanel;

    /**
     * Constructor
     * 
     * set up GUI
     */
    public SimpleDraw() {

        // Set up panel and button group.
        configPanel = new JPanel();
        figurePanel = new JPanel();
        drawPanel = new JPanel();
        buttonGroup = new ButtonGroup();

        // Set up config panel.
        configPanel.setLayout(new GridLayout(GRIDLAYOUT_ROWS, GRIDLAYOUT_COLS,
                DEFAULT_HGAP, DEFAULT_VGAP));

        // Define chose background color choice.        
        colorChooser = new Choice();
        colorChooser.add("Green");
        colorChooser.add("Red");
        colorChooser.add("Blue");
        
        // Define chose border type choice.        
        typeChooser = new Choice();
        typeChooser.add(SimpleDraw.BORDERTYPETHIN);
        typeChooser.add(SimpleDraw.BORDERTYPEHEAVY);
        typeChooser.add(SimpleDraw.BORDERTYPENONE);
        
        // Add chose figure label and button group.
        configPanel
                .add(new JLabel("Choose a figure :", SwingConstants.LEADING));
        dotButton = new JRadioButton("dot", true);
        buttonGroup.add(dotButton);
        configPanel.add(dotButton);
        EllipsButton = new JRadioButton("ellipse");
        buttonGroup.add(EllipsButton);
        configPanel.add(EllipsButton);
        rectButton = new JRadioButton("rectangle");
        buttonGroup.add(rectButton);
        configPanel.add(rectButton);
        polyButton = new JRadioButton("polygon");
        buttonGroup.add(polyButton);
        configPanel.add(polyButton);


        // Add set position label and fields.        
        configPanel
                .add(new JLabel("Set position :", SwingConstants.LEADING));
        positionX = new JTextField(DEFAULT_FIGURE_POSITION_X);
        configPanel.add(positionX);
        positionY = new JTextField(DEFAULT_FIGURE_POSITION_Y);
        configPanel.add(positionY);
        
        // Add chose background color label and choice.
        configPanel
        .add(new JLabel("Canvas color :", SwingConstants.LEADING));
        configPanel.add(colorChooser);

        // Add set size label and fields.        
        configPanel.add(new JLabel("Set size :", SwingConstants.LEADING));
        width = new JTextField(DEFAULT_FIGURE_WIDTH);
        configPanel.add(width);
        height = new JTextField(DEFAULT_FIGURE_HEIGHT);
        configPanel.add(height);
        
        // Add chose border type label and choice.
        configPanel
        .add(new JLabel("Border type :", SwingConstants.LEADING));
        configPanel.add(typeChooser);

        // Set border type for config panel.
        border = BorderFactory.createRaisedBevelBorder();
        configBorder = BorderFactory.createTitledBorder(border,
                "Config panel");
        configPanel.setBorder(configBorder);

        // create action
        
        // create button actions
        DrawButtonAction drawButtonAction = new DrawButtonAction();
        ClearButtonAction clearButtonAction = new ClearButtonAction();
        
        // create chose actions
        DrawButtonAction choseBorderTypeAction = new DrawButtonAction();
//        ClearButtonAction clearButtonAction = new ClearButtonAction();    
        
        // associate actions with choice
        typeChooser.addMouseListener(choseBorderTypeAction);

        // setup draw panel
        drawButton = new JButton("Draw the figure");
        clearButton = new JButton("Clear the figure");
        
        // associate actions with buttons
        drawButton.addActionListener((ActionListener) drawButtonAction);
        clearButton.addActionListener(clearButtonAction);
        drawPanel.add(drawButton);
        drawPanel.add(clearButton);

        // get content pane and set its layout
        container = getContentPane();

        container.add(configPanel, BorderLayout.NORTH);
        container.add(figurePanel);
        container.add(drawPanel, BorderLayout.SOUTH);

        // set up frame title, size and visible
        setTitle("Simple Draw");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        setVisible(true);

    }

    /**
     * Inner class to implement chose border type action.
     */
    private class ChooseBorderTypeAction implements ActionListener {
        /**
         * process GUI events
         * 
         * @param event
         */
        public void actionPerformed(ActionEvent event) {
            if (SimpleDraw.typeChooser.getSelectedItem().equalsIgnoreCase(
                    SimpleDraw.BORDERTYPENONE)) {
                // Set border type for config panel.
                border = BorderFactory.createEmptyBorder();
                configBorder = BorderFactory.createTitledBorder(border,
                        "Config panel");
                configPanel.setBorder(configBorder);
            }
        }
    }
    
    /**
     * inner class to implement draw button action
     */
    private class DrawButtonAction implements ActionListener {

        /**
         * draw rectangle in figure panel
         * 
         * @param leftX
         * @param topY
         * @param width
         * @param height
         */
        private void drawRectangle(int leftX, int topY, int width, int height) {
            SimpleDraw.figurePanel.getGraphics().drawRect(leftX, topY, width,
                    height);
        }

        /**
         * draw ellips in figure panel
         * 
         * @param leftX
         * @param topY
         * @param width
         * @param height
         */
        private void drawEllips(int leftX, int topY, int width, int height) {
            SimpleDraw.figurePanel.getGraphics().drawOval(leftX, topY, width,
                    height);
        }

        /**
         * process GUI events
         * 
         * @param event
         */
        public void actionPerformed(ActionEvent event) {
            if (SimpleDraw.rectButton.isSelected()) {
                this.drawRectangle(Integer.parseInt(SimpleDraw.positionX
                        .getText()), Integer.parseInt(SimpleDraw.positionY
                        .getText()), Integer.parseInt(SimpleDraw.width
                        .getText()), Integer.parseInt(SimpleDraw.height
                        .getText()));
            } else {
                this.drawEllips(Integer
                        .parseInt(SimpleDraw.positionX.getText()), Integer
                        .parseInt(SimpleDraw.positionY.getText()), Integer
                        .parseInt(SimpleDraw.width.getText()), Integer
                        .parseInt(SimpleDraw.height.getText()));
            }
        }

    }

    /**
     * inner class to implement clear button action
     */
    private class ClearButtonAction implements ActionListener {

        /**
         * process GUI events
         * 
         * @param event
         */
        public void actionPerformed(ActionEvent event) {
            SimpleDraw.container.repaint();
        }

    }

    // main method
    public static void main(String[] args) {
        SimpleDraw application = new SimpleDraw();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

--
                   '  :  `
                 .-----:
               .'       `.
         ,    /       (o) \
         \`._/          ,__)
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
※ 来源:.天大求实BBS http://bbs.tju.edu.cn [FROM: 61.152.132.230]