利用Java设计绘图版,可以绘画在界面设定位置和大小的直线、空心椭圆或者实心椭圆、空心矩形或者实心矩形和多边形
时间: 2024-03-11 10:44:45 浏览: 71
好的,下面是一个稍微复杂一点的利用Java设计绘图板的示例,可以绘制直线、空心椭圆或者实心椭圆、空心矩形或者实心矩形和多边形。示例代码如下:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DrawBoard extends JFrame {
private DrawPanel drawPanel;
private JPanel toolPanel;
private JButton lineBtn, ovalBtn, rectBtn, polyBtn;
private JComboBox<String> thicknessBox;
private JLabel statusLabel;
private int startX, startY, endX, endY;
private int shapeType = 0;
private int thickness = 1;
private boolean isFilled = false;
private enum ShapeType {
LINE, OVAL, RECT, POLY
}
public DrawBoard() {
setTitle("绘图板");
setSize(600, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// 工具面板
toolPanel = new JPanel();
toolPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
lineBtn = new JButton("直线");
ovalBtn = new JButton("椭圆");
rectBtn = new JButton("矩形");
polyBtn = new JButton("多边形");
thicknessBox = new JComboBox<String>(new String[] {"1", "2", "3", "4", "5"});
statusLabel = new JLabel("就绪");
toolPanel.add(lineBtn);
toolPanel.add(ovalBtn);
toolPanel.add(rectBtn);
toolPanel.add(polyBtn);
toolPanel.add(new JLabel("线宽:"));
toolPanel.add(thicknessBox);
toolPanel.add(statusLabel);
add(toolPanel, BorderLayout.NORTH);
// 绘图面板
drawPanel = new DrawPanel();
add(drawPanel, BorderLayout.CENTER);
// 直线按钮事件
lineBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
shapeType = ShapeType.LINE.ordinal();
statusLabel.setText("绘制直线");
}
});
// 椭圆按钮事件
ovalBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
shapeType = ShapeType.OVAL.ordinal();
statusLabel.setText("绘制椭圆");
}
});
// 矩形按钮事件
rectBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
shapeType = ShapeType.RECT.ordinal();
statusLabel.setText("绘制矩形");
}
});
// 多边形按钮事件
polyBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
shapeType = ShapeType.POLY.ordinal();
statusLabel.setText("绘制多边形");
}
});
// 线宽下拉框事件
thicknessBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
thickness = thicknessBox.getSelectedIndex() + 1;
}
});
// 鼠标事件
drawPanel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
startX = e.getX();
startY = e.getY();
}
public void mouseReleased(MouseEvent e) {
endX = e.getX();
endY = e.getY();
drawShape(startX, startY, endX, endY, shapeType, thickness, isFilled);
statusLabel.setText("就绪");
}
});
// 多边形绘制事件
drawPanel.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if (shapeType == ShapeType.POLY.ordinal()) {
drawPanel.setTempPoint(e.getX(), e.getY());
drawPanel.repaint();
}
}
});
setVisible(true);
}
private void drawShape(int x1, int y1, int x2, int y2, int shapeType, int thickness, boolean isFilled) {
Graphics2D g2d = (Graphics2D) drawPanel.getGraphics();
g2d.setStroke(new BasicStroke(thickness));
g2d.setColor(Color.BLACK);
if (shapeType == ShapeType.LINE.ordinal()) {
g2d.drawLine(x1, y1, x2, y2);
} else if (shapeType == ShapeType.OVAL.ordinal()) {
if (isFilled) {
g2d.fillOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1), Math.abs(y2 - y1));
} else {
g2d.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1), Math.abs(y2 - y1));
}
} else if (shapeType == ShapeType.RECT.ordinal()) {
if (isFilled) {
g2d.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1), Math.abs(y2 - y1));
} else {
g2d.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1), Math.abs(y2 - y1));
}
} else if (shapeType == ShapeType.POLY.ordinal()) {
int[] xPoints = drawPanel.getxPoints();
int[] yPoints = drawPanel.getyPoints();
int nPoints = drawPanel.getNumPoints();
if (isFilled) {
g2d.fillPolygon(xPoints, yPoints, nPoints);
} else {
g2d.drawPolygon(xPoints, yPoints, nPoints);
}
}
g2d.dispose();
}
public static void main(String[] args) {
new DrawBoard();
}
}
class DrawPanel extends JPanel {
private int[] xPoints;
private int[] yPoints;
private int numPoints;
private int tempX, tempY;
public DrawPanel() {
setBackground(Color.WHITE);
setVisible(true);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (numPoints > 0) {
g.drawPolyline(xPoints, yPoints, numPoints);
}
if (tempX != 0 && tempY != 0) {
g.drawLine(xPoints[numPoints - 1], yPoints[numPoints - 1], tempX, tempY);
}
}
public void setTempPoint(int x, int y) {
tempX = x;
tempY = y;
repaint();
}
public int[] getxPoints() {
return xPoints;
}
public int[] getyPoints() {
return yPoints;
}
public int getNumPoints() {
return numPoints;
}
public void addPoint(int x, int y) {
if (numPoints == 0) {
xPoints = new int[100];
yPoints = new int[100];
}
xPoints[numPoints] = x;
yPoints[numPoints] = y;
numPoints++;
}
public void clearPoints() {
numPoints = 0;
tempX = 0;
tempY = 0;
repaint();
}
}
```
这个示例中,工具面板包括直线、椭圆、矩形和多边形四个按钮,以及线宽下拉框和状态栏。绘图面板包括一个鼠标事件监听器和一个多边形绘制事件监听器。在鼠标事件监听器中,根据当前选择的形状类型、线宽和是否填充来绘制直线、椭圆或矩形。在多边形绘制事件监听器中,根据鼠标移动的轨迹来绘制多边形。
您可以根据自己的需求和能力进行修改和扩展,例如添加更多的图形类型、颜色选择、撤销操作等等,使得绘图板更加实用和方便。
阅读全文