import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DrawXian extends Applet{
private static final long serialVersionUID = 1L;
final double pai=3.14;
TextField X0;
TextField Y0;
TextField X1;
TextField Y1;
Label Lx0,Ly0,Lx1,Ly1;
Panel panel;
myPanel mypanel;
int xx0,yy0,xx1,yy1;
public class myPanel extends Panel{ 
public void update(Graphics g){
g.setColor(Color.red);
DrawLine(xx0,yy0,xx1,yy1,g);
}
private static final long serialVersionUID = 1L;

public void DrawPixel(int x,int y,Graphics g)
{
g.drawRect(x, y,0,0);
}
public void DrawLine(int x0,int y0,int x1,int y1,Graphics g){
if(x0==x1){
DrawLine3(x0,y0,x1,y1,g);return;
}
float k=(y1-y0)/(x1-x0);
if(0<=k&&k<=1){
DrawLine1(x0,y0,x1,y1,g);}
else if(k>1){
DrawLine3(x0,y0,x1,y1,g);
}else if(k<-1){
DrawLine4(x0,y0,x1,y1,g);
}else if(k>=-1&&k<=0){
DrawLine2(x0,y0,x1,y1,g);
}
}
public void DrawLine1(int x0,int y0,int x1,int y1,Graphics g){
int dx=x1-x0;
int dy=y1-y0;
int e=-dx;
int x,y;
if(x1>x0){
 x=x0;y=y0;
for(int i=0;i<=dx;i++){
DrawPixel(x,y,g);
x=x+1;
e=e+2*dy;
if(e>=0){
y=y+1;
e=e-2*dx;
}
}
}else{ e=dx;
x=x1;y=y1;
for(int i=0;i<=-dx;i++){
DrawPixel(x,y,g);
x=x+1;
e=e-2*dy;
if(e>=0){
y=y+1;
e=e+2*dx;
}
}
}}
public void DrawLine2(int x0,int y0,int x1,int y1,Graphics g){
int dx=x1-x0;
int dy=y1-y0;
int x,y;
int e=-dx;
if(x1>x0){
x=x0;y=y0;
for(int i=0;i<=dx;i++){
DrawPixel(x,y,g);
x=x+1;
e=e-2*dy;
if(e>=0){
y=y-1;
e=e-2*dx;
}
}}else{ e=dx;
x=x0;y=y0;
for(int i=0;i<=-dx;i++){
DrawPixel(x,y,g);
x=x-1;
e=e+2*dy;
if(e>=0){
y=y+1;
e=e+2*dx;
}
}
}
}
public void DrawLine3(int x0,int y0,int x1,int y1,Graphics g){
int dx=x1-x0;
int dy=y1-y0;
int e=-dy;
int x,y;
if(x1>=x0){
x=x0;y=y0;
for(int i=0;i<=dy;i++){
DrawPixel(x,y,g);
y=y+1;
e=e+2*dx;
if(e>=0){
x=x+1;
e=e-2*dy;
}
}}
//些处也有X1=X0的情况,使前面当X1=X0时起终点位置交换没关系.(前面处理X1=X0的情况是调用DrawLine3的)
if(x1<=x0){ e=dy;
x=x1;y=y1;
for(int i=0;i<=-dy;i++){
DrawPixel(x,y,g);
y=y+1;
e=e-2*dx;
if(e>=0){
x=x+1;
e=e+2*dy;
}
}
}
}
public void DrawLine4(int x0,int y0,int x1,int y1,Graphics g){
int dx=x1-x0;
int dy=y1-y0;
int e=-dy;
int x,y;
if(x1>x0){ e=dy;
x=x0;y=y0;
for(int i=0;i<=-dy;i++){
DrawPixel(x,y,g);
y=y-1;
e=e+2*dx;
if(e>=0){
x=x+1;
e=e+2*dy;
}
}}else{
x=x1;y=y1;
for(int i=0;i<=dy;i++){
DrawPixel(x,y,g);
y=y-1;
e=e-2*dx;
if(e>=0){
x=x+1;
e=e-2*dy;
}
}
}
}
}
public void init(){
setSize(500,500);
setLayout(new BorderLayout());
Lx0=new Label("X0");Ly0=new Label("Y0");Lx1=new Label("X1");Ly1=new Label("Y1");
panel=new Panel();
panel.add(Lx0);
X0=new TextField("0");panel.add(X0);
panel.add(Ly0);
Y0=new TextField("0");panel.add(Y0);
panel.add(Lx1);
X1=new TextField("50");panel.add(X1);
panel.add(Ly1);
Y1=new TextField("50");panel.add(Y1);
add("South",panel);
mypanel=new myPanel();

add("Center",mypanel);
Button b1=new Button("画线");panel.add(b1);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
xx0=Integer.parseInt(X0.getText());
yy0=Integer.parseInt(Y0.getText());
xx1=Integer.parseInt(X1.getText());
yy1=Integer.parseInt(Y1.getText());
mypanel.repaint();
}
});
Button b2=new Button("线画圆");panel.add(b2);
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
xx0=250;yy0=250;
for(double i=0.0;i<2*pai;i=i+pai/6.0){

yy1=(int)(250+Math.round((float)(100*Math.sin(i))));
xx1=(int)(250+Math.round((float)(100*Math.cos(i))));
mypanel.repaint();
System.out.println(""+xx1+" "+yy1); }
}

});
}


}我点那个"线画圆"按钮时为什么线不会以30度角转动画一圈啊,为什么只画最后一条线?前面的画线都可以保留以前画过的,为什么画圆就不可以了?圆心是在(250,250) "System.out.println(""+xx1+" "+yy1);"  这句是用来显示终点坐标的.每循环一次都有终点坐标说明for是做完了的,但我想看到线画圆的所有线,而不是只有最后一条显示.怎么改啊?我在线等的...

解决方案 »

  1.   

    初略看了下,感觉问题处在你的那些DrawLine方法,逻辑上不太正确。
      

  2.   

    DrawLine不会错的,我8种情况都试过,就是后面
    for(double i=0.0;i<2*pai;i=i+pai/6.0){
                        
                        yy1=(int)(250+Math.round((float)(100*Math.sin(i))));
                        xx1=(int)(250+Math.round((float)(100*Math.cos(i))));
                        mypanel.repaint();
                        System.out.println(""+xx1+" "+yy1);                }
    这个for语句线画圆有问题,我还做了一个画圆的,是半径从0到R填充圆的,但只画单个圆就行,用for画0到R填充就不行,为什么啊?
    下面是画圆程序:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.applet.*;
    public class DrawYuan extends Applet{
    private static final long serialVersionUID = 1L;
    int x0,y0,r;
    TextField X0;
    TextField Y0;
    TextField R;
    Label Lx0,Ly0,Lr;
    Panel panel;
    myPanel mypanel;

    public class myPanel extends Panel{
    private static final long serialVersionUID = 1L;
    public void update(Graphics g){
    g.setColor(Color.red);
    DrawCircle(x0,y0,r,g);


    }
    public void DrawPixel(int x,int y,Graphics g)
    {
    g.drawRect(x, y,0,0);
    }
    public void DrawCircle(int x0,int y0,int r,Graphics g){
    int delta,delta1,delta2,direction;
    int x=0,y=r;
    delta=2*(1-r);
    while(y>=0){
    DrawPixel(x0+x,y0+y,g);
    DrawPixel(x0-x,y0+y,g);
    DrawPixel(x0-x,y0-y,g);
    DrawPixel(x0+x,y0-y,g);
    if(delta<0){
    delta1=2*(delta+y)-1;
    if(delta1<=0) direction=1;
    else direction=2;
    }else if(delta>0){
    delta2=2*(delta-x)-1;
    if(delta2<=0) direction=2;
    else direction=3;
    }else direction=2;
    switch(direction){
    case 1: x++;delta+=2*x+1;break;
    case 2: x++;y--;delta+=2*(x-y+1);break;
    case 3: y--;delta+=(-2*y+1);break;
    }
    }
    }
    }
    public void init(){
    setSize(500,500);
    setLayout(new BorderLayout());
    Lx0=new Label("X0");Ly0=new Label("Y0");Lr=new Label("R");
    panel=new Panel();
    panel.add(Lx0);
    X0=new TextField("100");panel.add(X0);
    panel.add(Ly0);
    Y0=new TextField("100");panel.add(Y0);
    panel.add(Lr);
    R=new TextField("50");panel.add(R);
    add("South",panel);
    mypanel=new myPanel();

    add("Center",mypanel);
    Button b1=new Button("画圆");panel.add(b1);
    b1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    x0=Integer.parseInt(X0.getText());
    y0=Integer.parseInt(Y0.getText());
    r=Integer.parseInt(R.getText());
    mypanel.repaint();
    }
    });
    Button b2=new Button("填充圆");panel.add(b2);
    b2.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {

    for(int i=0,t=r;i<=t;i++){
    r=i;
    mypanel.repaint(); }
    }

    });
    }
    }麻烦大家帮我看下啊,谢谢了.