Drawing a nice circle in Java

后端 未结 5 887
生来不讨喜
生来不讨喜 2021-02-09 18:06

I\'m using Java Graphics and I keep getting \"ugly\" circles.

Here\'s what my Java program makes \"enter

5条回答
  •  孤城傲影
    2021-02-09 18:54

    I used drawPolygon method to draw circle by generating array of most of the points on circumference of circle with proposed radius. Code:

               import java.awt.*;
               import java.applet.*;
    
                /*
                     */
    
              public class OnlyCircle extends Applet{
    
                public void paint(Graphics g){
    
    
                  int r=200;//radius
                  int x1=250;//center x coordinate
                  int y1=250;//center y coordinate
                  double x2,y2;
                  double a=0;
                  double pi=3.14159;
                  int count=0; 
                  int i=0;
                  int f=0;
                  int[] x22=new int[628319];
                  int[] y22=new int[628319];
    
                 while(a<=2*pi&&i<628319&&f<628319)
                      {
                       double k=Math.cos(a);
                       double l=Math.sin(a);
                         x2=x1+r*k;
                         y2=y1+r*l;
                      x22[i]=(int)x2;
                      y22[f]=(int)y2;
                       i++;
                       f++;
                       a+=0.00001;
                      }
                   int length=x22.length;
                   g.drawPolygon(x22,y22,length);
                     }
                   }
    

提交回复
热议问题