java语言用 bresebham算法画圆,怎么也运行不出来,求高手指点。代码如下:

import javax.swing.*;
import java.awt.*;

class SetPixel extends JPanel
{Color color;
int r;
public void SetPixel()
{color=Color.blue;
r=100;
}

public void paintComponent(Graphics g)
{g.setColor(color);
bresenham_arc(g,r);
}

void bresenham_arc(Graphics g ,int radius)
{
int x,y,d;
x=0;
y=radius;
d=3-2*radius;
while(x>y)
{
g.drawLine(x+200,y+200,x+200,y+200);
g.drawLine(-x+200,y+200,-x+200,y+200);
g.drawLine(-x+200,-y+200,-x+200,-y+200);
g.drawLine(x+200,-y+200,x+200,-y+200);
g.drawLine(y+200,x+200,y+200,x+200);
g.drawLine(-y+200,x+200,-y+200,x+200);
g.drawLine(-y+200,-x+200,-y+200,-x+200);
g.drawLine(y+200,-x+200,y+200,-x+200);
if(d<0)
d=d+4*x+6;
else
{
d=d+4*(x-y)+10;
y--;
}
x++;
}
if(x==y)
g.drawLine(x+200,y+200,x+200,y+200);
g.drawLine(-x+200,y+200,-x+200,y+200);
g.drawLine(-x+200,-y+200,-x+200,-y+200);
g.drawLine(x+200,-y+200,x+200,-y+200);
g.drawLine(y+200,x+200,y+200,x+200);
g.drawLine(-y+200,x+200,-y+200,x+200);
g.drawLine(-y+200,-x+200,-y+200,-x+200);
g.drawLine(y+200,-x+200,y+200,-x+200);
}

}
public class Circle extends JFrame
{public Circle ()
{super("PixelColor");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(new Rectangle(500,500,500,500));
SetPixel set=new SetPixel();
add(set);
}
public static void main(String[] args)
{
Circle setPixel=new Circle();
setPixel.show();
}
}

首先public void SetPixel()这个函数错了,构造方法没有返回值,把void去掉
其次算法写错了,while(x>y)改成while(x<y)

改完的
import javax.swing.*;
import java.awt.*;

class SetPixel extends JPanel {
Color color;
int r;

public SetPixel() {
color = Color.blue;
r = 100;
}

public void paintComponent(Graphics g) {
g.setColor(color);
bresenham_arc(g, r);
}

void bresenham_arc(Graphics g, int radius) {

int x, y, d;
x = 0;
y = radius;
d = 3 - 2 * radius;
while (x < y) {
g.drawLine(x + 200, y + 200, x + 200, y + 200);
g.drawLine(-x + 200, y + 200, -x + 200, y + 200);
g.drawLine(-x + 200, -y + 200, -x + 200, -y + 200);
g.drawLine(x + 200, -y + 200, x + 200, -y + 200);
g.drawLine(y + 200, x + 200, y + 200, x + 200);
g.drawLine(-y + 200, x + 200, -y + 200, x + 200);
g.drawLine(-y + 200, -x + 200, -y + 200, -x + 200);
g.drawLine(y + 200, -x + 200, y + 200, -x + 200);
if (d < 0)
d = d + 4 * x + 6;
else {
d = d + 4 * (x - y) + 10;
y--;
}
x++;
}
if (x == y)
g.drawLine(x + 200, y + 200, x - 200, y + 200);

g.drawLine(-x + 200, y + 200, -x + 200, y + 200);
g.drawLine(-x + 200, -y + 200, -x + 200, -y + 200);
g.drawLine(x + 200, -y + 200, x + 200, -y + 200);
g.drawLine(y + 200, x + 200, y + 200, x + 200);
g.drawLine(-y + 200, x + 200, -y + 200, x + 200);
g.drawLine(-y + 200, -x + 200, -y + 200, -x + 200);
g.drawLine(y + 200, -x + 200, y + 200, -x + 200);
}

}

public class Circle extends JFrame {
public Circle() {
super("PixelColor");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(new Rectangle(500, 500, 500, 500));
SetPixel set = new SetPixel();
add(set);
}

public static void main(String[] args) {
Circle setPixel = new Circle();
setPixel.setVisible(true);
}
}
温馨提示:答案为网友推荐,仅供参考
相似回答