用Java小程序和Bresenham算法实现对已知圆心和半径的圆弧的绘制;使用4条圆弧组合,画出完整的圆形,圆心坐标: (200,100); 圆的半径:100,四条圆弧的颜色分别为黑色,青色,红色,红色。
时间: 2024-05-11 22:14:19 浏览: 5
```java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CircleDrawing extends JPanel {
private static final long serialVersionUID = 1L;
private int centerX, centerY, radius;
public CircleDrawing(int centerX, int centerY, int radius) {
this.centerX = centerX;
this.centerY = centerY;
this.radius = radius;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawCircle(g, centerX, centerY, radius, Color.BLACK);
drawCircle(g, centerX, centerY, radius, Color.CYAN);
drawCircle(g, centerX, centerY, radius, Color.RED);
drawCircle(g, centerX, centerY, radius, Color.RED);
}
private void drawCircle(Graphics g, int x0, int y0, int r, Color color) {
g.setColor(color);
int x = 0;
int y = r;
int d = 3 - 2 * r;
while (x <= y) {
drawPixel(g, x0, y0, x, y);
if (d < 0) {
d = d + 4 * x + 6;
} else {
d = d + 4 * (x - y) + 10;
y--;
}
x++;
}
}
private void drawPixel(Graphics g, int x0, int y0, int x, int y) {
g.drawLine(x0 + x, y0 + y, x0 + x, y0 + y);
g.drawLine(x0 + x, y0 - y, x0 + x, y0 - y);
g.drawLine(x0 - x, y0 + y, x0 - x, y0 + y);
g.drawLine(x0 - x, y0 - y, x0 - x, y0 - y);
g.drawLine(x0 + y, y0 + x, x0 + y, y0 + x);
g.drawLine(x0 + y, y0 - x, x0 + y, y0 - x);
g.drawLine(x0 - y, y0 + x, x0 - y, y0 + x);
g.drawLine(x0 - y, y0 - x, x0 - y, y0 - x);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Circle");
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CircleDrawing circleDrawing = new CircleDrawing(200, 100, 100);
frame.add(circleDrawing);
frame.setVisible(true);
}
}
```