📄 circlepaint.java
字号:
import java.awt.*;
import java.awt.event.*;
public class CirclePaint extends Frame implements ActionListener
{ int x0,y0,r;
Label lc= new Label("半径和圆心:");
Label l1=new Label("0");
Label l2=new Label("r");
Label l3=new Label("x0");
Label l4=new Label("y0");
Label l8=new Label("算法选择:");
TextField t1 = new TextField();
TextField t2 = new TextField();
TextField t3 = new TextField();
Button b1 = new Button("确定");
CheckboxGroup p1=new CheckboxGroup();
Checkbox c1=new Checkbox("midPointCircle");
Checkbox c2=new Checkbox("bresenhanCircle");
Graphics h;
String s;
public static void main(String[] args)
{new CirclePaint().init();
}
public void init()
{
this.setBackground(Color.pink);
this.setTitle("模拟生成圆:");
this.setBounds(100,100,600,560);
this.setVisible(true);
this.setLayout(null);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent arg0)
{System.exit(0);}
});
lc.setBounds(10,40,100,20);
lc.setForeground(Color.white);
l2.setBounds(110, 40,20, 20);
t1.setBounds(130,40,60,20);
l3.setBounds(210, 40, 20, 20);
t2.setBounds(230,40,60,20);
l4.setBounds(310, 40, 20, 20);
t3.setBounds(330,40,60,20);
b1.setBounds(410,40,60,20);
b1.addActionListener(this);
l1.setBounds(305, 305,10, 10);
c1.setCheckboxGroup(p1);
c2.setCheckboxGroup(p1);
c1.setBounds(160,520, 140, 20);
c1.addItemListener(new S());
c2.setBounds(320,520, 140, 20);
c2.addItemListener(new S());
l8.setBounds(20,520, 140, 20);
this.add(l1);
this.add(l2);
this.add(l3);
this.add(l4);
this.add(lc);
this.add(t1);
this.add(t2);
this.add(t3);
this.add(b1);
this.add(c1);
this.add(c2);
this.add(l8);
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==b1)
{ r=Integer.parseInt(t1.getText());
x0=Integer.parseInt(t2.getText());
y0=Integer.parseInt(t3.getText());
repaint();
}
}
public void paint (Graphics g )
{
g.setColor(Color.black);
for(int i=0;i<21;i++)
g.drawLine(100,100+20*i,500,100+20*i);
for(int i=0;i<21;i++)
g.drawLine(100+20*i,100,100+20*i,500);
g.setColor(Color.green);
g.drawLine(520,300,515,298);
g.drawLine(520,300,515,302);
g.drawLine(300,80,298,85);
g.drawLine(300,80,302,85);
g.drawLine(80,300,520,300);
g.drawLine(300,80,300,520);
g.fillOval(299,299, 2,2);
g.setColor(Color.red);
g.drawOval(300+20*(x0-r), 300-20*(y0+r), 40*r, 40*r);
if(s=="midPointCircle") midPointCircle(g);
if(s=="bresenhanCircle")bresenhanCircle(g);
}
public void circlepoints (int x,int y,Graphics g)
{ g.setColor(Color.red);
g.fillOval(300+20*(x+x0),300-20*(y+y0) , 5, 5);
g.fillOval(300+20*(y+x0),300-20*(y0+x) , 5, 5);
g.fillOval(300+20*(y+x0),300+20*(x-y0) , 5, 5);
g.fillOval(300+20*(x0+x),300+20*(y-y0) , 5, 5);
g.fillOval(300-20*(x-x0), 300+20*(y-y0), 5, 5);
g.fillOval(300-20*(y-x0),300+20*(x-y0) , 5, 5);
g.fillOval(300-20*(y-x0),300-20*(x+y0) , 5, 5);
g.fillOval(300-20*(x-x0),300-20*(y+y0) , 5, 5);
}
public void midPointCircle(Graphics g)
{ int x,y,d;
x=0; y=r; d=1-r;
g.setColor(Color.red);
g.fillOval(300+20*x0,300-20*y0, 10,10);
try{ Thread.sleep(500);}catch(Exception e){}
circlepoints (x,y,g); //显示圆弧上的八个对称点
while(x<=y)
{ if(d<0) d+=2*x+3;
else
{ d+=2*(x-y)+5;
y=y-1;
}
x++;
circlepoints (x,y,g);
try{ Thread.sleep(500);}catch(Exception e){}
}
}
public void bresenhanCircle(Graphics g)
{
int x,y,d,d1,d2,direction;
x=0;
y=r;
d=2*(1-r);
g.setColor(Color.red);
g.fillOval(300+20*x0,300-20*y0, 10,10);
while(y>=0)
{ circlepoints (x,y,g);
try{ Thread.sleep(500);}catch(Exception e){}
if(d<0)
{
d1=2*(d+y)-1;
if(d1<0) direction=1;
else direction=2;
}
else if(d>0)
{
d2=2*(d-x)-1;
if(d2<=0) direction=2;
else direction=3;
}
else direction =2;
switch(direction)
{case 1:x++;
d+=2*x+1;
break;
case 2:x++;
y--;
d+=2*(x-y+1);
break;
case 3:y--;
d+= -2*y+1;
break;
}
}
}
class S implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{ Checkbox ch=(Checkbox)e.getSource();
if(ch==c1) s="midPointCircle";
if(ch==c2) s="bresenhanCircle";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -