📄 drawingprogram.java~
字号:
import objectdraw.*;import java.awt.*;import javax.swing.*;import java.awt.event.*;// This program demonstrates the use of the listener interface, and its use // with gui components. This version includes buttons.public class DrawingProgram extends WindowController implements ItemListener, ActionListener{ // Width of border around drawing area private static final int BORDER_X = 10; private static final int BORDER_Y = 10; // Default size when we create a new shape private static final int DEFAULT_SIZE = 50; // Initial Location of shapes to be drawn private Location defaultLocation; // Buttons determining which kind of object is drawn private JButton framedSquareButton, framedCircleButton, filledSquareButton; // The menu determining which color the object should be private JComboBox colorMenu; // 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; /* * Draws the program with the buttons and a blank drawing area */ public void begin() { // Create buttons for selecting figures framedSquareButton = new JButton("FramedSquare"); framedCircleButton = new JButton("FramedCircle"); filledSquareButton = new JButton("FilledSquare"); framedSquareButton.addActionListener(this); framedCircleButton.addActionListener(this); filledSquareButton.addActionListener(this); // create menu for selecting colors colorMenu = new JComboBox(); colorMenu.addItem("Red"); colorMenu.addItem("Blue"); colorMenu.addItem("Green"); colorMenu.addItem("Yellow"); colorMenu.addItemListener(this); JPanel bottomPanel = new JPanel(); bottomPanel.setLayout(new GridLayout(2, 1)); JPanel buttonPanel = new JPanel(); buttonPanel.add(framedSquareButton); buttonPanel.add(framedCircleButton); buttonPanel.add(filledSquareButton); bottomPanel.add(buttonPanel); bottomPanel.add(colorMenu); Container contentPane = getContentPane(); contentPane.add(bottomPanel, BorderLayout.SOUTH); contentPane.validate(); // Draw the drawing area drawingArea = new FramedRect( BORDER_X, BORDER_Y, canvas.getWidth() - 2 * BORDER_X, canvas.getHeight() - 2 * BORDER_Y, canvas); defaultLocation = new Location((canvas.getWidth() - DEFAULT_SIZE) / 2, (canvas.getHeight() - DEFAULT_SIZE) / 2); } /** * change newShape to color on color menu * Assumes newShape not null */ private void setColor() { Object colorChoiceString = colorMenu.getSelectedItem(); if (colorChoiceString.equals("Red")) { newShape.setColor(Color.red); } else if (colorChoiceString.equals("Blue")) { newShape.setColor(Color.blue); } else if (colorChoiceString.equals("Green")) { newShape.setColor(Color.green); } else if (colorChoiceString.equals("Yellow")) { newShape.setColor(Color.yellow); } } /* * When the user clicks the button on a shape button, create that shape. */ public void actionPerformed(ActionEvent evt) { // Determine the appropriate button pushed if (evt.getSource() == framedSquareButton) { newShape = new FramedRect(defaultLocation, DEFAULT_SIZE, DEFAULT_SIZE, canvas); } else if (evt.getSource() == framedCircleButton) { newShape = new FramedOval(defaultLocation, DEFAULT_SIZE, DEFAULT_SIZE, canvas); } else if (evt.getSource() == filledSquareButton) { newShape = new FilledRect(defaultLocation, DEFAULT_SIZE, DEFAULT_SIZE, canvas); } setColor(); } /** * Select a new color for the last object drawn */ public void itemStateChanged(ItemEvent event) { if (newShape != null) { setColor(); } } /* * 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 + -