java,1、绘图应用程序。利用学过的GUI方法实现一个较为完整 的绘图应用程序。用户可以选择要画的图形(如线椭圆,圆,矩形,弧,圆角矩形,多边形,折线等等),选择画笔颜色和画笔粗度,用鼠标单击和拖拽等动作绘图。
时间: 2024-06-09 09:04:28 浏览: 96
好的,这个任务可以分成几个步骤来完成。
1. 创建一个GUI界面,包含一个画板和一些控制按钮,比如选择图形、选择颜色、选择粗细等等。
2. 在画板上监听鼠标事件,根据用户的动作绘制相应的图形。
3. 实现不同图形的绘制方法,比如绘制线条、椭圆、矩形、弧、圆角矩形、多边形、折线等等。
4. 实现控制按钮的响应方法,比如选择颜色、选择粗细、选择图形等等。
下面是一个简单的Java代码示例,可以帮助你了解如何实现这个应用程序:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DrawingApp extends JFrame {
private JPanel controlPanel;
private JPanel drawingPanel;
private JComboBox<String> shapeBox;
private JComboBox<String> colorBox;
private JComboBox<Integer> sizeBox;
private JButton clearButton;
private int startX, startY, endX, endY;
private int selectedShape = 0;
private Color selectedColor = Color.BLACK;
private int selectedSize = 1;
public DrawingApp() {
// 初始化界面
setTitle("Drawing App");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建控制面板和绘图面板
controlPanel = new JPanel();
drawingPanel = new DrawingPanel();
drawingPanel.setBackground(Color.WHITE);
// 添加控制按钮
String[] shapes = {"Line", "Oval", "Rectangle", "Arc", "Round Rectangle", "Polygon", "Polyline"};
shapeBox = new JComboBox<>(shapes);
shapeBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectedShape = shapeBox.getSelectedIndex();
}
});
controlPanel.add(shapeBox);
String[] colors = {"Black", "Red", "Green", "Blue"};
colorBox = new JComboBox<>(colors);
colorBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String selected = (String) colorBox.getSelectedItem();
switch (selected) {
case "Red":
selectedColor = Color.RED;
break;
case "Green":
selectedColor = Color.GREEN;
break;
case "Blue":
selectedColor = Color.BLUE;
break;
default:
selectedColor = Color.BLACK;
break;
}
}
});
controlPanel.add(colorBox);
Integer[] sizes = {1, 2, 3, 4, 5};
sizeBox = new JComboBox<>(sizes);
sizeBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectedSize = (int) sizeBox.getSelectedItem();
}
});
controlPanel.add(sizeBox);
clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
((DrawingPanel) drawingPanel).clear();
}
});
controlPanel.add(clearButton);
// 将控制面板和绘图面板添加到窗口中
add(controlPanel, BorderLayout.NORTH);
add(drawingPanel, BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args) {
new DrawingApp();
}
private class DrawingPanel extends JPanel {
private int[] xPoints = new int[100];
private int[] yPoints = new int[100];
private int nPoints = 0;
public DrawingPanel() {
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();
switch (selectedShape) {
case 0:
drawLine();
break;
case 1:
drawOval();
break;
case 2:
drawRectangle();
break;
case 3:
drawArc();
break;
case 4:
drawRoundRectangle();
break;
case 5:
drawPolygon();
break;
case 6:
drawPolyline();
break;
}
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
endX = e.getX();
endY = e.getY();
switch (selectedShape) {
case 0:
drawLine();
break;
case 1:
drawOval();
break;
case 2:
drawRectangle();
break;
case 3:
drawArc();
break;
case 4:
drawRoundRectangle();
break;
case 5:
break;
case 6:
xPoints[nPoints] = endX;
yPoints[nPoints] = endY;
nPoints++;
drawPolyline();
break;
}
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(selectedColor);
g.drawLine(startX, startY, endX, endY);
}
private void drawLine() {
Graphics g = getGraphics();
g.setColor(selectedColor);
g.drawLine(startX, startY, endX, endY);
}
private void drawOval() {
Graphics g = getGraphics();
int width = endX - startX;
int height = endY - startY;
g.setColor(selectedColor);
g.drawOval(startX, startY, width, height);
}
private void drawRectangle() {
Graphics g = getGraphics();
int width = endX - startX;
int height = endY - startY;
g.setColor(selectedColor);
g.drawRect(startX, startY, width, height);
}
private void drawArc() {
Graphics g = getGraphics();
int width = endX - startX;
int height = endY - startY;
g.setColor(selectedColor);
g.drawArc(startX, startY, width, height, 45, 90);
}
private void drawRoundRectangle() {
Graphics g = getGraphics();
int width = endX - startX;
int height = endY - startY;
g.setColor(selectedColor);
g.drawRoundRect(startX, startY, width, height, 10, 10);
}
private void drawPolygon() {
Graphics g = getGraphics();
if (nPoints > 0) {
g.setColor(selectedColor);
g.drawPolygon(xPoints, yPoints, nPoints);
}
}
private void drawPolyline() {
Graphics g = getGraphics();
if (nPoints > 0) {
g.setColor(selectedColor);
g.setStroke(new BasicStroke(selectedSize));
g.drawPolyline(xPoints, yPoints, nPoints);
}
}
public void clear() {
Graphics g = getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
nPoints = 0;
}
}
}
```
这是一个简单的绘图应用程序,你可以根据需求对其进行修改和完善。
阅读全文