实现一个绘图工具,可以按拉橡皮筋的方法绘制直线,椭圆,矩形,并可以选择形状的颜色java代码
时间: 2024-03-02 11:54:36 浏览: 130
以下是一个简单的Java代码示例,实现了一个绘图工具,可以按拉橡皮筋的方法绘制直线、椭圆、矩形,并可以选择形状的颜色:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DrawingTool extends JFrame {
private int startX, startY, endX, endY;
private Color currentColor = Color.BLACK;
private String currentShape = "Line";
public DrawingTool() {
super("Drawing Tool");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel drawingPanel = new JPanel();
drawingPanel.setBackground(Color.WHITE);
drawingPanel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
startX = e.getX();
startY = e.getY();
}
});
drawingPanel.addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
endX = e.getX();
endY = e.getY();
repaint();
}
});
drawingPanel.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
endX = e.getX();
endY = e.getY();
repaint();
}
});
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
JButton colorButton = new JButton("Choose Color");
colorButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
currentColor = JColorChooser.showDialog(null, "Choose Color", currentColor);
}
});
controlPanel.add(colorButton);
JRadioButton lineButton = new JRadioButton("Line", true);
lineButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
currentShape = "Line";
}
});
controlPanel.add(lineButton);
JRadioButton ovalButton = new JRadioButton("Oval");
ovalButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
currentShape = "Oval";
}
});
controlPanel.add(ovalButton);
JRadioButton rectButton = new JRadioButton("Rectangle");
rectButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
currentShape = "Rectangle";
}
});
controlPanel.add(rectButton);
ButtonGroup shapeGroup = new ButtonGroup();
shapeGroup.add(lineButton);
shapeGroup.add(ovalButton);
shapeGroup.add(rectButton);
add(drawingPanel, BorderLayout.CENTER);
add(controlPanel, BorderLayout.SOUTH);
setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(currentColor);
if (currentShape.equals("Line")) {
g2d.drawLine(startX, startY, endX, endY);
} else if (currentShape.equals("Oval")) {
int width = Math.abs(endX - startX);
int height = Math.abs(endY - startY);
int x = Math.min(startX, endX);
int y = Math.min(startY, endY);
g2d.drawOval(x, y, width, height);
} else if (currentShape.equals("Rectangle")) {
int width = Math.abs(endX - startX);
int height = Math.abs(endY - startY);
int x = Math.min(startX, endX);
int y = Math.min(startY, endY);
g2d.drawRect(x, y, width, height);
}
}
public static void main(String[] args) {
new DrawingTool();
}
}
```
这段代码使用JFrame作为窗口,使用JPanel作为绘图区域,使用鼠标事件来实现按拉橡皮筋的方法绘制直线、椭圆、矩形,使用JColorChooser类实现颜色选择器。注意,在paint方法中需要使用Graphics2D类来绘制图形,并且需要根据当前选择的形状来绘制不同的图形。
阅读全文