使用Java实现程序通过鼠标拖拽画图,当单选按钮选择直线时,拖拽鼠标可以画直线;当单选按钮选择任意时,鼠标拖拽可以画出鼠标轨迹。
时间: 2024-05-10 14:18:11 浏览: 95
以下是Java实现程序的代码:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DrawPanel extends JPanel implements MouseListener, MouseMotionListener {
private int startX, startY, endX, endY;
private boolean isDrawingLine = false;
private boolean isDrawingFree = false;
private boolean isButtonSelected = false;
private String selectedButton = "";
public DrawPanel() {
addMouseListener(this);
addMouseMotionListener(this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (isDrawingLine) {
g.drawLine(startX, startY, endX, endY);
}
}
public void mousePressed(MouseEvent e) {
startX = e.getX();
startY = e.getY();
isButtonSelected = true;
}
public void mouseDragged(MouseEvent e) {
endX = e.getX();
endY = e.getY();
if (selectedButton.equals("line")) {
isDrawingLine = true;
} else if (selectedButton.equals("free")) {
isDrawingFree = true;
}
repaint();
}
public void mouseReleased(MouseEvent e) {
endX = e.getX();
endY = e.getY();
if (selectedButton.equals("line")) {
isDrawingLine = false;
} else if (selectedButton.equals("free")) {
isDrawingFree = false;
}
repaint();
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void setSelectedButton(String button) {
selectedButton = button;
}
public static void main(String[] args) {
JFrame frame = new JFrame("Draw Panel");
DrawPanel panel = new DrawPanel();
JPanel buttonPanel = new JPanel();
JRadioButton lineButton = new JRadioButton("Line");
JRadioButton freeButton = new JRadioButton("Free");
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(lineButton);
buttonGroup.add(freeButton);
buttonPanel.add(lineButton);
buttonPanel.add(freeButton);
lineButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.setSelectedButton("line");
}
});
freeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.setSelectedButton("free");
}
});
frame.add(panel, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
```
在程序中,我们创建了一个`DrawPanel`类,该类继承自`JPanel`,并实现了`MouseListener`和`MouseMotionListener`接口。在`DrawPanel`类中,我们定义了一些变量,如`startX`、`startY`、`endX`、`endY`等,用于存储鼠标的位置。我们还定义了三个布尔类型的变量,`isDrawingLine`、`isDrawingFree`和`isButtonSelected`,分别表示当前是否正在画直线、是否正在画任意曲线以及是否选择了按钮。我们还定义了一个字符串类型的变量`selectedButton`,用于存储当前选择的按钮。
在`DrawPanel`类中,我们重写了`paintComponent()`方法,用于绘制直线。在`mousePressed()`方法中,我们记录鼠标的起始位置,并将`isButtonSelected`设为`true`。在`mouseDragged()`方法中,我们记录鼠标的结束位置,并根据当前选择的按钮设置`isDrawingLine`和`isDrawingFree`的值。最后,在`mouseReleased()`方法中,我们更新`isDrawingLine`和`isDrawingFree`的值,并调用`repaint()`方法重新绘制面板。
在`main()`方法中,我们创建了一个`JFrame`窗口,并将`DrawPanel`面板添加到窗口中。我们还创建了一个`JPanel`面板,用于放置单选按钮,并将它添加到窗口的南边。我们使用`ButtonGroup`类来组合两个单选按钮,并为每个单选按钮添加了一个事件监听器,当单选按钮被选中时,我们将`selectedButton`变量的值设为相应的字符串。最后,我们设置了窗口的关闭方式、大小和可见性。
阅读全文