Java图形用户界面绘制一个三角形,输入坐标,选择颜色,调用draw(g)绘制相应图形,计算图像的周长和面积,输入旋转角度实现旋转,输入缩放比例,实现图形缩放
时间: 2024-06-08 22:07:47 浏览: 98
以下是一个Java图形用户界面绘制三角形、计算周长和面积、旋转和缩放的示例代码:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TriangleDrawer extends JFrame implements ActionListener {
private JTextField x1Field, y1Field, x2Field, y2Field, x3Field, y3Field;
private JComboBox<String> colorBox;
private JTextField rotateField, scaleField;
private JButton drawButton;
private JLabel resultLabel;
public TriangleDrawer() {
super("Triangle Drawer");
JPanel inputPanel = new JPanel(new GridLayout(4, 3));
inputPanel.add(new JLabel("x1:"));
x1Field = new JTextField("0", 5);
inputPanel.add(x1Field);
inputPanel.add(new JLabel("y1:"));
y1Field = new JTextField("0", 5);
inputPanel.add(y1Field);
inputPanel.add(new JLabel("x2:"));
x2Field = new JTextField("0", 5);
inputPanel.add(x2Field);
inputPanel.add(new JLabel("y2:"));
y2Field = new JTextField("0", 5);
inputPanel.add(y2Field);
inputPanel.add(new JLabel("x3:"));
x3Field = new JTextField("0", 5);
inputPanel.add(x3Field);
inputPanel.add(new JLabel("y3:"));
y3Field = new JTextField("0", 5);
inputPanel.add(y3Field);
JPanel colorPanel = new JPanel();
colorPanel.add(new JLabel("Color:"));
String[] colorNames = {"Black", "Red", "Green", "Blue"};
colorBox = new JComboBox<>(colorNames);
colorPanel.add(colorBox);
JPanel rotatePanel = new JPanel();
rotatePanel.add(new JLabel("Rotate:"));
rotateField = new JTextField("0", 5);
rotatePanel.add(rotateField);
JButton rotateButton = new JButton("Rotate");
rotateButton.addActionListener(this);
rotatePanel.add(rotateButton);
JPanel scalePanel = new JPanel();
scalePanel.add(new JLabel("Scale:"));
scaleField = new JTextField("1", 5);
scalePanel.add(scaleField);
JButton scaleButton = new JButton("Scale");
scaleButton.addActionListener(this);
scalePanel.add(scaleButton);
JPanel buttonPanel = new JPanel();
drawButton = new JButton("Draw");
drawButton.addActionListener(this);
buttonPanel.add(drawButton);
JPanel resultPanel = new JPanel();
resultLabel = new JLabel("");
resultPanel.add(resultLabel);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(inputPanel, BorderLayout.CENTER);
contentPane.add(colorPanel, BorderLayout.NORTH);
contentPane.add(rotatePanel, BorderLayout.WEST);
contentPane.add(scalePanel, BorderLayout.EAST);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
contentPane.add(resultPanel, BorderLayout.PAGE_END);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == drawButton) {
repaint();
} else if (e.getActionCommand().equals("Rotate")) {
double angle = Double.parseDouble(rotateField.getText());
Point center = getTriangleCenter();
rotateTriangle(angle, center);
repaint();
} else if (e.getActionCommand().equals("Scale")) {
double scale = Double.parseDouble(scaleField.getText());
Point center = getTriangleCenter();
scaleTriangle(scale, center);
repaint();
}
}
private void rotateTriangle(double angle, Point center) {
double cos = Math.cos(Math.toRadians(angle));
double sin = Math.sin(Math.toRadians(angle));
for (Point p : getTrianglePoints()) {
double x = p.getX() - center.getX();
double y = p.getY() - center.getY();
p.setLocation(x * cos - y * sin + center.getX(), x * sin + y * cos + center.getY());
}
}
private void scaleTriangle(double scale, Point center) {
for (Point p : getTrianglePoints()) {
double x = p.getX() - center.getX();
double y = p.getY() - center.getY();
p.setLocation(x * scale + center.getX(), y * scale + center.getY());
}
}
private Point getTriangleCenter() {
int x1 = Integer.parseInt(x1Field.getText());
int y1 = Integer.parseInt(y1Field.getText());
int x2 = Integer.parseInt(x2Field.getText());
int y2 = Integer.parseInt(y2Field.getText());
int x3 = Integer.parseInt(x3Field.getText());
int y3 = Integer.parseInt(y3Field.getText());
return new Point((x1 + x2 + x3) / 3, (y1 + y2 + y3) / 3);
}
private Point[] getTrianglePoints() {
int x1 = Integer.parseInt(x1Field.getText());
int y1 = Integer.parseInt(y1Field.getText());
int x2 = Integer.parseInt(x2Field.getText());
int y2 = Integer.parseInt(y2Field.getText());
int x3 = Integer.parseInt(x3Field.getText());
int y3 = Integer.parseInt(y3Field.getText());
Point[] points = {new Point(x1, y1), new Point(x2, y2), new Point(x3, y3)};
return points;
}
public void paint(Graphics g) {
super.paint(g);
Point[] points = getTrianglePoints();
int[] xPoints = {points[0].x, points[1].x, points[2].x};
int[] yPoints = {points[0].y, points[1].y, points[2].y};
String color = (String) colorBox.getSelectedItem();
if (color.equals("Black")) {
g.setColor(Color.BLACK);
} else if (color.equals("Red")) {
g.setColor(Color.RED);
} else if (color.equals("Green")) {
g.setColor(Color.GREEN);
} else if (color.equals("Blue")) {
g.setColor(Color.BLUE);
}
g.drawPolygon(xPoints, yPoints, 3);
double perimeter = getTrianglePerimeter();
double area = getTriangleArea();
resultLabel.setText(String.format("Perimeter: %.2f, Area: %.2f", perimeter, area));
}
private double getTrianglePerimeter() {
Point[] points = getTrianglePoints();
double side1 = points[0].distance(points[1]);
double side2 = points[1].distance(points[2]);
double side3 = points[2].distance(points[0]);
return side1 + side2 + side3;
}
private double getTriangleArea() {
Point[] points = getTrianglePoints();
double side1 = points[0].distance(points[1]);
double side2 = points[1].distance(points[2]);
double side3 = points[2].distance(points[0]);
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
public static void main(String[] args) {
TriangleDrawer frame = new TriangleDrawer();
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
```
在这个代码中,我们使用了`JFrame`和`JPanel`来创建一个GUI窗口,其中包含了输入三角形坐标、选择颜色、旋转和缩放的控件,并且可以绘制三角形并计算它的周长和面积。具体来说:
- `x1Field`、`y1Field`、`x2Field`、`y2Field`、`x3Field`和`y3Field`是用来输入三角形的坐标的文本框。
- `colorBox`是用来选择三角形颜色的下拉框。
- `rotateField`和`scaleField`是用来输入旋转角度和缩放比例的文本框。
- `drawButton`是用来绘制三角形的按钮。
- `resultLabel`是用来显示三角形周长和面积的标签。
在`actionPerformed`方法中,我们根据不同的按钮事件来执行不同的操作。`drawButton`点击事件会调用`repaint`方法来重新绘制三角形;`rotateButton`点击事件会根据输入的旋转角度和三角形中心点来旋转三角形;`scaleButton`点击事件会根据输入的缩放比例和三角形中心点来缩放三角形。
在`paint`方法中,我们首先根据输入的三角形坐标和颜色来绘制三角形。然后计算三角形的周长和面积,并将它们显示在`resultLabel`中。
在`getTrianglePerimeter`方法中,我们根据三角形的三个点来计算三角形的周长。在`getTriangleArea`方法中,我们根据三角形的三个点和海伦公式来计算三角形的面积。在`rotateTriangle`方法中,我们根据输入的旋转角度和三角形中心点来旋转三角形。在`scaleTriangle`方法中,我们根据输入的缩放比例和三角形中心点来缩放三角形。
最后,在`main`方法中,我们创建一个`TriangleDrawer`对象并显示它。
阅读全文