使用Java小程序和Bresenham算法写出如下代码 使用4条圆弧组合,画出完整的圆形。圆心坐标: (200,100); 圆的半径:100。 第一条圆弧的颜色是黑色,第二条圆弧的颜色是青色,第三条圆弧的颜色是红色,第四条圆弧的颜色是红色。
时间: 2024-05-03 15:20:02 浏览: 65
java实现Bresenham算法画圆
4星 · 用户满意度95%
```java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawCircle extends JPanel {
private final int centerX = 200;
private final int centerY = 100;
private final int radius = 100;
public void paintComponent(Graphics g) {
super.paintComponent(g);
// 画黑色圆弧
g.setColor(Color.BLACK);
drawArc(g, 0, 90);
// 画青色圆弧
g.setColor(Color.CYAN);
drawArc(g, 90, 180);
// 画红色圆弧
g.setColor(Color.RED);
drawArc(g, 180, 270);
drawArc(g, 270, 360);
}
private void drawArc(Graphics g, int startAngle, int endAngle) {
int x = 0;
int y = radius;
int d = 3 - 2 * radius;
while (x <= y) {
plot(g, x, y, startAngle, endAngle);
if (d < 0) {
d = d + 4 * x + 6;
} else {
d = d + 4 * (x - y) + 10;
y--;
}
x++;
}
}
private void plot(Graphics g, int x, int y, int startAngle, int endAngle) {
if (startAngle <= endAngle) {
if (isInArc(x, y, startAngle, endAngle)) {
g.drawLine(centerX + x, centerY + y, centerX + x, centerY + y);
}
} else {
if (isInArc(x, y, startAngle, 360) || isInArc(x, y, 0, endAngle)) {
g.drawLine(centerX + x, centerY + y, centerX + x, centerY + y);
}
}
}
private boolean isInArc(int x, int y, int startAngle, int endAngle) {
double angle = Math.atan2(y, x) * 180 / Math.PI;
if (angle < 0) {
angle += 360;
}
return angle >= startAngle && angle <= endAngle;
}
public static void main(String[] args) {
JFrame frame = new JFrame("Draw Circle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
DrawCircle panel = new DrawCircle();
frame.add(panel);
frame.setVisible(true);
}
}
```
阅读全文