public class JavaMain { public static void main(String[] args) { new MyWindow(); } } class MyWindow extends JFrame implements MouseListener, MouseMotionListener, ActionListener { //定义两个点 private Point oldPoint = null,newPoint = null; public MyWindow () { this.setSize(600,600); addMouseListener(this); addMouseMotionListener(this); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } //画图 public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setStroke(new BasicStroke(10.0f)); g2.setColor(Color.red); if(oldPoint!=null&&newPoint!=null) { g2.drawLine(oldPoint.x, oldPoint.y, newPoint.x, newPoint.y); oldPoint=newPoint; newPoint=null;//置为空,为了迎接下一个点的到来 } } //鼠标拖动事件 @Override public void mouseDragged(MouseEvent e) { if(oldPoint==null) { oldPoint=e.getPoint(); }else { newPoint=e.getPoint(); repaint();//生成新的点,通知系统去更新绘图 } } @Override public void mouseMoved(MouseEvent e) {} //鼠标单击事件 @Override public void mouseClicked(MouseEvent e) {} @Override public void mouseEntered(MouseEvent e) {} @Override public void mouseExited(MouseEvent e) {} @Override public void mousePressed(MouseEvent e) {} //鼠标释放事件,清空工作 @Override public void mouseReleased(MouseEvent e) { newPoint=null; oldPoint=null; } }设置清除画布按钮
时间: 2024-02-04 22:02:10 浏览: 69
您可以在 `MyWindow` 类中添加一个按钮,并为其添加一个 `ActionListener`,在按钮被点击时清除画布。代码示例如下:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyWindow extends JFrame implements MouseListener, MouseMotionListener, ActionListener {
private Point oldPoint = null, newPoint = null;
private JButton clearButton; // 添加一个清除按钮
public MyWindow() {
this.setSize(600, 600);
addMouseListener(this);
addMouseMotionListener(this);
// 初始化清除按钮
clearButton = new JButton("清除画布");
clearButton.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.add(clearButton);
add(buttonPanel, BorderLayout.SOUTH);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(10.0f));
g2.setColor(Color.red);
if (oldPoint != null && newPoint != null) {
g2.drawLine(oldPoint.x, oldPoint.y, newPoint.x, newPoint.y);
oldPoint = newPoint;
newPoint = null;
}
}
@Override
public void mouseDragged(MouseEvent e) {
if (oldPoint == null) {
oldPoint = e.getPoint();
} else {
newPoint = e.getPoint();
repaint();
}
}
@Override
public void mouseMoved(MouseEvent e) {}
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {
newPoint = null;
oldPoint = null;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == clearButton) {
Graphics g = getGraphics();
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
}
}
}
```
在上面的代码中,我们添加了一个 `clearButton` 按钮,并在其上添加了一个 `ActionListener`。当按钮被点击时,会调用 `actionPerformed` 方法清除画布。在 `actionPerformed` 方法中,我们获取了 `MyWindow` 的 `Graphics` 对象,并使用 `fillRect` 方法覆盖整个画布。这样就达到了清除画布的效果。
阅读全文