Circle
Last name:
Student ID:
Class:
Date: 2012-4
Lab: Circle
Lab content:
I am using the MidPoint Circle Method (improved (2) algorithm)
Core algorithms:
// Draw 8 symmetric points
Public
Static voidDraweightpoint (IntX,IntY, graphics g)
{
G. filloval (Offsetx+ X,Offsety+ Y,Pointsize,Pointsize);
G. filloval (Offsetx+ X,Offsety-2 *R+ (Offsety-Y ),Pointsize,Pointsize);
G. filloval (Offsetx-3 *R+ (Offsetx-X ),Offsety+ Y,Pointsize,Pointsize);
G. filloval (Offsetx-3 *R+ (Offsetx-X ),Offsety-2 *R+ (Offsety-Y ),Pointsize,Pointsize);
G. filloval (Offsetx+ Y,Offsety+ X,Pointsize,Pointsize);
G. filloval (Offsetx+ Y,Offsety-2 *R+ (Offsety-X ),Pointsize,Pointsize);
G. filloval (Offsetx-3 *R+ (Offsetx-Y ),Offsety+ X,Pointsize,Pointsize);
G. filloval (Offsetx-3 *R+ (Offsetx-Y ),Offsety-2 *R+ (Offsety-X ),Pointsize,Pointsize);
}
// The core algorithm of the MidPoint Circle
Public
StaticvoidDrawcircle (Graphics g ){
IntX, Y, D;
X = 0;
Y =R;
IntDeltax = 3;
IntDeltay = 2-R-R;
D = 1-R;
Draweightpoint(X, y, G );
While(X <= y ){
If(D <0 ){
D + = deltax;
Deltax + = 2;
}Else{
D + = deltax + deltay;
Deltax + = 2;
Deltay + = 2;
Y --;
}
X ++;
Draweightpoint(X, y, G );
}
Experiment results :()
Program source code:
PackageCs0904.no200942094;
ImportJava. AWT. graphics;
ImportJavax. Swing. jframe;
ImportJavax. Swing. jpanel;
Public
ClassDrawcircleExtendsJpanel {
Private
Static int Framewidth= 600;
Private
Static int Frameheight= 600;
Private
Static int Offsetx= 300,Offsety= 200;
// Coordinates of the start point and focus
// Radius
Private
Static int R= 100;
// Define the vertex size
Private
Static int Pointsize= 5;
// Calculate a cell for every 20 cells
Private
Static intTurnbigger (IntTemp ){
ReturnTemp * 20;
}
Public
Static voidMain (string [] ARGs ){
Jframeframe =NewJframe ("drawline ");
Frame. getcontentpane (). Add (NewDrawcircle ());
Frame. setdefaclocloseoperation (jframe.Exit_on_close);
Frame. setsize (Framewidth,Frameheight);
Frame. setlocation (200,10 );
Frame. setvisible (True);
}
// Draw 8 symmetric points
Public
Static voidDraweightpoint (IntX,IntY, graphics g)
{
G. filloval (Offsetx+ X,Offsety+ Y,Pointsize,Pointsize);
G. filloval (Offsetx+ X,Offsety-2 *R+ (Offsety-Y ),Pointsize,Pointsize);
G. filloval (Offsetx-3 *R+ (Offsetx-X ),Offsety+ Y,Pointsize,Pointsize);
G. filloval (Offsetx-3 *R+ (Offsetx-X ),Offsety-2 *R+ (Offsety-Y ),Pointsize,Pointsize);
G. filloval (Offsetx+ Y,Offsety+ X,Pointsize,Pointsize);
G. filloval (Offsetx+ Y,Offsety-2 *R+ (Offsety-X ),Pointsize,Pointsize);
G. filloval (Offsetx-3 *R+ (Offsetx-Y ),Offsety+ X,Pointsize,Pointsize);
G. filloval (Offsetx-3 *R+ (Offsetx-Y ),Offsety-2 *R+ (Offsety-X ),Pointsize,Pointsize);
}
Public
Static voidDrawcircle (Graphics g ){
IntX, Y, D;
X = 0;
Y =R;
IntDeltax = 3;
IntDeltay = 2-R-R;
D = 1-R;
Draweightpoint(X, y, G );
While(X <= y ){
If(D <0 ){
D + = deltax;
Deltax + = 2;
}Else{
D + = deltax + deltay;
Deltax + = 2;
Deltay + = 2;
Y --;
}
X ++;
Draweightpoint(X, y, G );
}
G. drawstring ("", x + 20, Y + 20 );
}
Protected
VoidPaintcomponent (graphicsg ){
Super. Paintcomponent (g );
For(IntI = 0; I <Frameheight; I + =Turnbigger(1 )){
G. drawline (0, I,Framewidth, I );
}
For(IntI = 0; I <Framewidth; I + =Turnbigger(1 )){
G. drawline (I, 0, I,Frameheight);
}
Drawcircle(G );
}
}