用Java小程序和Bresenham算法实现对已知圆心和半径的圆弧的绘制;使用4条圆弧组合,画出完整的圆形,圆心坐标: (200,100); 圆的半径:100,四条圆弧的颜色分别为黑色,青色和红色
时间: 2024-06-03 22:09:17 浏览: 70
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawCircle extends JPanel {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
JFrame frame = new JFrame("Draw Circle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setContentPane(new DrawCircle());
frame.setVisible(true);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int x = 200;
int y = 100;
int r = 100;
int x1 = x + r;
int y1 = y;
int x2 = x - r;
int y2 = y;
int x3 = x;
int y3 = y + r;
int x4 = x;
int y4 = y - r;
g.setColor(Color.BLACK);
drawArc(g, x, y, r, 0, 90);
g.setColor(Color.CYAN);
drawArc(g, x, y, r, 90, 180);
g.setColor(Color.RED);
drawArc(g, x, y, r, 180, 270);
g.setColor(Color.BLACK);
drawArc(g, x, y, r, 270, 360);
}
private void drawArc(Graphics g, int x, int y, int r, int startAngle, int endAngle) {
int x1 = x + (int) (r * Math.cos(Math.toRadians(startAngle)));
int y1 = y + (int) (r * Math.sin(Math.toRadians(startAngle)));
for (int i = startAngle; i <= endAngle; i++) {
int x2 = x + (int) (r * Math.cos(Math.toRadians(i)));
int y2 = y + (int) (r * Math.sin(Math.toRadians(i)));
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
}
}
阅读全文