Java 使用eclipsesdk
我做了一个程序来画形状和改变颜色。我使用重置按钮重置为默认形状和颜色,但无法从画布上清除形状。我该怎么做?这是我的密码:Java 使用eclipsesdk,java,Java,我做了一个程序来画形状和改变颜色。我使用重置按钮重置为默认形状和颜色,但无法从画布上清除形状。我该怎么做?这是我的密码: // Color Clear choice box Choice colorChoice; // the canvas DrawCanvas canvas; /** * Constructor */ public Draw() { super("Java Draw"); setLayout(new BorderLayout()); // cr
// Color Clear choice box
Choice colorChoice;
// the canvas
DrawCanvas canvas;
/**
* Constructor
*/
public Draw() {
super("Java Draw");
setLayout(new BorderLayout());
// create panel for controls
Panel topPanel = new Panel(new GridLayout(3, 0));
add(topPanel, BorderLayout.NORTH);
// create button control
Panel buttonPanel = new Panel(new FlowLayout(FlowLayout.LEFT));
topPanel.add(buttonPanel);
circle = new Button("Circle");
buttonPanel.add(circle);
roundRec = new Button("Rounded Rectangle");
buttonPanel.add(roundRec);
threeDRec = new Button("3D Rectangle");
buttonPanel.add(threeDRec);
// add button listener
circle.addActionListener(this);
roundRec.addActionListener(this);
threeDRec.addActionListener(this);
Panel buttonPanel1 = new Panel(new FlowLayout(FlowLayout.LEFT));
topPanel.add(buttonPanel1);
lines = new Button("Lines");
buttonPanel1.add(lines);
squares = new Button("Square");
buttonPanel1.add(squares);
ovals = new Button("Ovals");
buttonPanel1.add(ovals);
lines.addActionListener(this);
squares.addActionListener(this);
ovals.addActionListener(this);
// create panel for color choices
Panel colorPanel = new Panel(new FlowLayout(FlowLayout.LEFT));
topPanel.add(colorPanel);
Label label = new Label("Filled Color:");
colorPanel.add(label);
colorChoice = new Choice();
for(int i=0; i<COLOR_NAMES.length; i++) {
colorChoice.add(COLOR_NAMES[i]);
}
colorPanel.add(colorChoice);
colorChoice.addItemListener(this);
// create reset button
Button resetButton = new Button("Reset");
colorPanel.add(resetButton);
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
colorChoice.select(0); // reset color choice box
canvas.setFilledColor(COLORS[0]); // reset color used
canvas.setShape(DrawCanvas.CIRCLE); // reset shape
}
});
// create the canvas
canvas = new DrawCanvas();
add(canvas, BorderLayout.CENTER);
}// 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);
}
}
//颜色清除选择框
颜色选择;
//画布
帆布;
/**
*建造师
*/
公众抽签(){
超级(“Java绘图”);
setLayout(新的BorderLayout());
//为控件创建面板
面板顶部面板=新面板(新网格布局(3,0));
添加(topPanel,BorderLayout.NORTH);
//创建按钮控件
面板按钮面板=新面板(新FlowLayout(FlowLayout.LEFT));
顶部面板添加(按钮面板);
圆圈=新按钮(“圆圈”);
按钮面板。添加(圆形);
roundRec=新按钮(“圆角矩形”);
按钮面板添加(roundRec);
threeDRec=新按钮(“3D矩形”);
按钮面板添加(三个);
//添加按钮侦听器
circle.addActionListener(this);
roundRec.addActionListener(此);
3drec.addActionListener(此);
面板按钮Panel1=新面板(新的FlowLayout(FlowLayout.LEFT));
顶部面板添加(按钮面板1);
行=新按钮(“行”);
按钮Panel1.添加(行);
方形=新按钮(“方形”);
按钮面板1。添加(正方形);
椭圆形=新按钮(“椭圆形”);
按钮面板1。添加(椭圆形);
lines.addActionListener(this);
squares.addActionListener(这个);
ovals.addActionListener(此);
//为颜色选择创建面板
面板颜色面板=新面板(新FlowLayout(FlowLayout.LEFT));
topPanel.add(彩色面板);
标签=新标签(“填充颜色:”);
彩色面板。添加(标签);
colorChoice=新选项();
对于(int i=0;i假设DrawCanvas
扩展JPanel
,您需要在重置侦听器的末尾调用repaint
。什么是DrawCanvas?您可以粘贴完整的代码吗?似乎与