我需要帮助来弄清楚如何在画布上绘制多个形状。目前,我一次只能绘制一个形状。目的是例如绘制一个圆并绘制一个矩形,而不会使圆消失以绘制矩形。我知道我必须将以前的形状临时存储在程序中的某个位置,但我不是100%如何解决该问题。我希望从大家那里找到一些指示和指导。谢谢。
这是我的代码:
Draw.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
public class Draw extends Frame implements ActionListener, ItemListener {
// Initial Frame size
static final int WIDTH = 960; // frame width
static final int HEIGHT = 720; // frame height
// Color choices
static final String COLOR_NAMES[] = {"None", "Red", "Blue", "Green"};
static final Color COLORS[] = {null, Color.red, Color.blue, Color.green};
// Button control
JButton circle;
JButton roundRec;
JButton threeDRec;
// Color choice box
JComboBox<String> colorChoice;
//Shape List
JList<String>shapeList;
// the canvas
DrawCanvas canvas;
/**
* Constructor
*/
public Draw() {
super("Java Draw");
setLayout(new BorderLayout());
// create panel for controls
JPanel topPanel = new JPanel(new GridLayout(2, 1));
add(topPanel, BorderLayout.NORTH);
// create button control
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
topPanel.add(buttonPanel);
circle = new JButton("Circle");
buttonPanel.add(circle);
roundRec = new JButton("Rounded Rectangle");
buttonPanel.add(roundRec);
threeDRec = new JButton("3D Rectangle");
buttonPanel.add(threeDRec);
// add button listener
circle.addActionListener(this);
roundRec.addActionListener(this);
threeDRec.addActionListener(this);
// create panel for color choices
JPanel colorPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
topPanel.add(colorPanel);
JLabel label = new JLabel("Filled Color:");
colorPanel.add(label);
colorChoice = new JComboBox<String>();
for(int i=0; i<COLOR_NAMES.length; i++) {
colorChoice.addItem(COLOR_NAMES[i]);
}
colorPanel.add(colorChoice);
colorChoice.addItemListener(this);
JPanel leftPanel = new JPanel(new GridLayout(2,1));
add(leftPanel, BorderLayout.WEST);
shapeList = new JList<>();
leftPanel.add(shapeList);
shapeList.setBackground(Color.LIGHT_GRAY);
JPanel removeButton = new JPanel(new FlowLayout(FlowLayout.LEFT));
leftPanel.add(removeButton);
JButton remove = new JButton ("Remove");
removeButton.add(remove);
// create the canvas
canvas = new DrawCanvas();
add(canvas, BorderLayout.CENTER);
canvas.setBackground(Color.WHITE);
} // end of constructor
/**
* Implementing ActionListener
*/
public void actionPerformed(ActionEvent event) {
if(event.getSource() == circle) { // circle button
canvas.setShape(DrawCanvas.CIRCLE);
}
else if(event.getSource() == roundRec) { // rounded rectangle button
canvas.setShape(DrawCanvas.ROUNDED_RECTANGLE);
}
else if(event.getSource() == threeDRec) { // 3D rectangle button
canvas.setShape(DrawCanvas.RECTANGLE_3D);
}
}
/**
* Implementing ItemListener
*/
public void itemStateChanged(ItemEvent event) {
Color color = COLORS[colorChoice.getSelectedIndex()];
canvas.setFilledColor(color);
}
/**
* the main method
*/
public static void main(String[] argv) {
// Create a frame
Draw frame = new Draw();
frame.setSize(WIDTH, HEIGHT);
frame.setLocation(150, 100);
// add window closing listener
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
});
// Show the frame
frame.setVisible(true);
}
}
DrawCanvas.java
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
public class DrawCanvas extends Canvas implements MouseListener,
MouseMotionListener {
// Constants for shapes
public static final int CIRCLE = 1;
public static final int ROUNDED_RECTANGLE = 2;
public static final int RECTANGLE_3D = 3;
// Coordinates of points to draw
private int x1, y1, x2, y2;
//Can I store previous shape using this?
//ArrayList<Shape> shapes = new ArrayList<>();
// shape to draw
private int shape = CIRCLE;
/**
* Method to set the shape
*/
public void setShape(int shape) {
this.shape = shape;
}
// filled color
private Color filledColor = null;
/**
* Method to set filled color
*/
public void setFilledColor(Color color) {
filledColor = color;
}
/**
* Constructor
*/
public DrawCanvas() {
addMouseListener(this);
addMouseMotionListener(this);
} // end of constructor
/**
* painting the component
*/
public void paint(Graphics g) {
// the drawing area
int x, y, width, height;
// determine the upper-left corner of bounding rectangle
x = Math.min(x1, x2);
y = Math.min(y1, y2);
// determine the width and height of bounding rectangle
width = Math.abs(x1 - x2);
height = Math.abs(y1 - y2);
if(filledColor != null)
g.setColor(filledColor);
switch (shape) {
case ROUNDED_RECTANGLE :
if(filledColor == null)
g.drawRoundRect(x, y, width, height, width/4, height/4);
else
g.fillRoundRect(x, y, width, height, width/4, height/4);
break;
case CIRCLE :
int diameter = Math.max(width, height);
if(filledColor == null)
g.drawOval(x, y, diameter, diameter);
else
g.fillOval(x, y, diameter, diameter);
break;
case RECTANGLE_3D :
if(filledColor == null)
g.draw3DRect(x, y, width, height, true);
else
g.fill3DRect(x, y, width, height, true);
break;
}
}
/**
* Implementing MouseListener
*/
public void mousePressed(MouseEvent event) {
x1 = event.getX();
y1 = event.getY();
}
public void mouseReleased(MouseEvent event) {
x2 = event.getX();
y2 = event.getY();
repaint();
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
/**
* Implementing MouseMotionListener
*/
public void mouseDragged(MouseEvent event) {
x2 = event.getX();
y2 = event.getY();
repaint();
}
public void mouseMoved(MouseEvent e) {}
}