⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drawingprogram.java

📁 书籍"Java_面向事件编程"的附带光盘代码
💻 JAVA
字号:
import objectdraw.*;import java.awt.*;import javax.swing.*; // This program demonstrates the use of gui componentspublic class DrawingProgram extends WindowController {    // Size and locations of buttons in the palette used to create shapes    private static final int BUTTON_HEIGHT = 40;    private static final int BUTTON_WIDTH = 40;    private static final int PALETTE_X = 0;    private static final int PALETTE_Y = 0;        // Location of icon labeling shape buttons    private static final int ICON_MARGIN = 10;    private static final int ICON_X = PALETTE_X + ICON_MARGIN;    private static final int ICON_DIM = BUTTON_WIDTH - (2 * ICON_MARGIN);        // Location and shape of the drawing area    private static final int AREA_X = PALETTE_X + BUTTON_WIDTH;    private static final int AREA_Y = PALETTE_Y;    private static final int AREA_WIDTH = 300;    private static final int AREA_HEIGHT = 300;        // Location and shape of the buttons used to change colors    private static final int COLORS_X = AREA_X + AREA_WIDTH + 2;    private static final int COLORS_Y = PALETTE_Y;    private static final int COLOR_WIDTH = BUTTON_WIDTH;    private static final int COLOR_HEIGHT = BUTTON_HEIGHT;        // Size parameters    private static final int MIN_SIZE = 10;    private static final int MAX_SIZE = 100;    private static final int INIT_SIZE = 20;        // Default location and size when we create a new shape    private static final int DEFAULT_SIZE = 50;    private static final Location DEFAULT_LOC =        new Location(AREA_X + AREA_WIDTH / 2 - DEFAULT_SIZE / 2, AREA_Y + AREA_HEIGHT / 2 - DEFAULT_SIZE / 2);        // boundary of area drawing in    private FramedRect drawingArea;        // The variable to hold the last shape created    private DrawableInterface newShape;        // Used to support the usual dragging algorithm    private boolean dragging;    private Location lastMouse;        // Buttons to change color    private FilledRect redRect, blueRect, greenRect, yellowRect;            // The menu determining which kind of object is drawn    private JComboBox figureMenu;        // Size of newly created objects    private JSlider sizeSlider;            /*      * Draws the program with the buttons and a blank drawing area     */    public void begin() {        // Create menu for selecting figures        figureMenu = new JComboBox();        figureMenu.addItem("FramedSquare");        figureMenu.addItem("FramedCircle");        figureMenu.addItem("FilledSquare");                Container contentPane = getContentPane();        contentPane.add(figureMenu, BorderLayout.SOUTH);                // Create and add the size slider        sizeSlider = new JSlider(JSlider.VERTICAL, MIN_SIZE, MAX_SIZE, INIT_SIZE);        contentPane.add(sizeSlider, BorderLayout.WEST);                contentPane.validate();                // Draw the drawing area        drawingArea = new FramedRect(AREA_X, AREA_Y, AREA_WIDTH, AREA_HEIGHT, canvas);                // Create color palette        redRect = new FilledRect(COLORS_X, COLORS_Y, COLOR_WIDTH, COLOR_HEIGHT, canvas);        redRect.setColor(Color.red);        blueRect = new FilledRect(COLORS_X, redRect.getY() + COLOR_HEIGHT, COLOR_WIDTH, COLOR_HEIGHT, canvas);        blueRect.setColor(Color.blue);        greenRect = new FilledRect(COLORS_X, blueRect.getY() + COLOR_HEIGHT, COLOR_WIDTH, COLOR_HEIGHT, canvas);        greenRect.setColor(Color.green);        yellowRect = new FilledRect(COLORS_X, greenRect.getY() + COLOR_HEIGHT, COLOR_WIDTH, COLOR_HEIGHT, canvas);        yellowRect.setColor(Color.yellow);    }        /*      * When the user clicks inside the drawing area, create the selected shape.     * When the user clicks on a color button, change the color of the last     * shape created.  If the drawing area is empty, clicking on a color button     * does nothing.     */    public void onMouseClick(Location loc) {        // Handle shape buttons        if (drawingArea.contains(loc)) {            int size = sizeSlider.getValue();            Object choice = figureMenu.getSelectedItem();            if (choice.equals("FramedSquare")) {                newShape = new FramedRect(DEFAULT_LOC, size, size, canvas);            } else if (choice.equals("FramedCircle")) {                newShape = new FramedOval(DEFAULT_LOC, size, size, canvas);            } else if (choice.equals("FilledSquare")) {                newShape = new FilledRect(DEFAULT_LOC, size, size, canvas);            }        } else if (newShape != null) {            // Handle color buttons            if (redRect.contains(loc)) {                newShape.setColor(Color.red);            } else if (blueRect.contains(loc)) {                newShape.setColor(Color.blue);            } else if (greenRect.contains(loc)) {                newShape.setColor(Color.green);            } else if (yellowRect.contains(loc)) {                newShape.setColor(Color.yellow);            }        }    }        /*      * Drag the shape with the mouse.  Notice that it doesn't matter what     * kind of shape (FramedRect, FilledOval, etc.) we have.     */    public void onMouseDrag(Location loc) {        if (dragging) {            newShape.move(loc.getX() - lastMouse.getX(), loc.getY() - lastMouse.getY());            lastMouse = loc;        }    }        /*      * Start a drag     */    public void onMousePress(Location loc) {        if (newShape != null && newShape.contains(loc)) {            dragging = true;            lastMouse = loc;        }    }        /*      * Stop a drag     */    public void onMouseRelease(Location arg0) {        dragging = false;    }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -