java绘制图形涂鸦板实验
时间: 2023-10-08 08:04:34 浏览: 99
好的,我可以为您提供一些Java绘制图形涂鸦板的实验代码。以下是一个简单的示例:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DrawingBoard extends JFrame implements ActionListener, MouseMotionListener {
private int x, y;
private JButton btnClear;
private JPanel panel;
public DrawingBoard() {
super("Drawing Board");
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.addMouseMotionListener(this);
add(panel, BorderLayout.CENTER);
btnClear = new JButton("Clear");
btnClear.addActionListener(this);
add(btnClear, BorderLayout.SOUTH);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnClear) {
panel.repaint();
}
}
public void mouseMoved(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
Graphics g = panel.getGraphics();
g.setColor(Color.BLACK);
g.fillOval(x, y, 10, 10);
}
public static void main(String[] args) {
DrawingBoard board = new DrawingBoard();
}
}
```
这个程序创建了一个窗口,其中包含一个白色的面板和一个“Clear”按钮。当用户拖动鼠标时,它会在面板上绘制黑色的圆形,用户可以使用“Clear”按钮来清除整个面板。
希望这个示例对您有所帮助!
阅读全文