Some graphics algorithms

Source: Internet
Author: User

The algorithm draws an ellipse, a circle, and an arc.

 Bresenhanm midpoint algorithm draws an elliptic,

 

Ellipsepanel. Java

 

Package com. Deng. ellipse;

Import java. AWT. color;
Import java. AWT. dimension;
Import java. AWT. graphics;

Import javax. Swing. jpanel;

Public class ellipsepanel extends jpanel {
Private int A, B;
Public ellipsepanel (int A, int B)
{
Super ();
Int temp;
This. A =;
This. B = B;
If (A <B)
{
Temp =;
A = B;
B = temp;
}
Setbackground (color. Black );
Setpreferredsize (New Dimensions (800,600 ));
Repaint ();

}
Public ellipsepanel ()
{
Super ();
This. A = 10;
This. B = 5;
Setbackground (color. Black );
Setpreferredsize (New Dimensions (800,600 ));
Repaint ();
}
// Mom, there is an error when this function name is added with "S!
// There Is A paintcomponents in the container parent class, and I cannot draw any image after heavy loading.
// Then the paintcomponent function does not know where it was reloaded.
// This function name is often written incorrectly.
@ Override
Public void paintcomponent (Graphics g)
{
// Todo auto-generated method stub
Super. paintcomponent (g );
G. setcolor (color. Red );
Midbresenhamellipse (g );
 
 
}
Public void midbresenhamellipse (Graphics g)
{
Int X, Y;
Float D1, D2;
Int basex, basey; // basic coordinates, with the center of the Panel as the coordinate origin
 
X = 0;
Y = B;
D1 = (float) (B * B + A * a * (-B + 0.25 ));
G. setcolor (color. Red );
// Draw coordinates
Basex = (INT) (This. getwidth ()/2 );
Basey = (INT) (This. getheight ()/2 );
G. drawline (0, basey, this. getwidth (), basey );
G. drawline (basex, 0, basex, this. getheight ());
 
// Draw four basic points, which are symmetric points derived from a point.
G. filloval (basex + X, basey + Y, 5, 5 );
G. filloval (basex-X, basey-y, 5, 5 );
G. filloval (basex-X, basey + Y, 5, 5 );
G. filloval (basex + X, basey-y, 5, 5 );
 
While (B * B * (x + 1) <A * a * (y-0.5 ))
{
If (d1 <= 0)
{
D1 = D1 + B * B * (2 * x + 3 );
X ++;
}
Else
{
D1 = D1 + (B * (2 * x + 3) + A * a * (-2 * Y + 2 ));
X ++;
Y --;

}
G. filloval (basex + X, basey + Y, 5, 5 );
G. filloval (basex-X, basey-y, 5, 5 );
G. filloval (basex-X, basey + Y, 5, 5 );
G. filloval (basex + X, basey-y, 5, 5 );
} // While ends part
 
D2 = (float) (B * B * (x + 0.5) * (x + 0.5) + A * a * (Y-1) * (Y-1) -A * a * B * B );

While (Y> 0 ){

If (D2 <= 0)
{
D2 = d2 + B * B * (2 * x + 2) + A * a * (-2 * Y + 3 );
X ++;
Y --;
}
Else
{
D2 = d2 + A * a * (-2 * Y + 3 );
Y --;
}
G. filloval (basex + X, basey + Y, 5, 5 );
G. filloval (basex-X, basey-y, 5, 5 );
G. filloval (basex-X, basey + Y, 5, 5 );
G. filloval (basex + X, basey-y, 5, 5 );
}
 
} // Midbresemham ends

}

 

Bresenhamellipsedriver. Java

 

Package com. Deng. ellipse;

Import javax. Swing. jframe;

Public class bresenhamellipsedriver {
Public static void main (string [] ARGs ){
Jframe frambresenhamellipse = new jframe ("bresenmham ellipse ");
Frambresenhamellipse. setdefaultcloseoperation (jframe. exit_on_close );
Ellipsepanel panelellipse = new ellipsepanel (200,100 );
Frambresenhamellipse. getcontentpane (). Add (panelellipse );
Frambresenhamellipse. Pack ();
Frambresenhamellipse. setvisible (true );
}

}

Bresemham Circle Algorithm

Bresenhamcicledriver. Java

 

Package com. Deng. bresenham;

Import javax. Swing. jframe;

Public class bresenhamcicledriver {
Public static void main (string [] ARGs ){
Jframe framecicleframe = new jframe ("bresenham cicel ");
Framecicleframe. setdefaultcloseoperation (jframe. exit_on_close );
Bresenhamciclepanel Panel = new bresenhamciclepanel (100 );
Framecicleframe. getcontentpane (). Add (panel );
Framecicleframe. Pack ();
Framecicleframe. setvisible (true );
}

}

 

Bresenhamciclepanel. Java

Package com. Deng. bresenham;
// Using bresenham to dra a cicle
Import java. AWT. color;
Import java. AWT. dimension;
Import java. AWT. graphics;

Import javax. Swing. jpanel;

Import com. Deng. cicle. eightpoint;

Public class bresenhamciclepanel extends jpanel {
Private int R;
// Construction
Public bresenhamciclepanel (int r)
{
Super ();
This. r = r; // forget this sentence, so the radius of the circle is 0. How can I say there is no arc?
Setbackground (color. Black );
Setpreferredsize (New Dimensions (800,600 ));
Repaint ();

}
Public bresenhamciclepanel ()
{
This. r = 10;
}
// Draw cicle
@ Override
Public void paintcomponent (Graphics g)
{
// Todo auto-generated method stub
Super. paintcomponent (g );
G. setcolor (color. Red );
Int X, Y, D;
X = 0;
Y = This. R;
D = 1-this.r;
 
While (x <Y ){

Paineightpoint (G, x, y );
// Repaint ();
If (d <0)
D = d + 2 * x + 3;
Else
{
D = d + 2 * (x-y) + 5;
Y --;
}
X ++;

}
}
Public void paineightpoint (Graphics g, int X, int y) // This is the eight-point circle method.
{
Int basex, basey;
G. setcolor (color. Red );
Basex = (INT) (This. getwidth ()/2 );
Basey = (INT) (This. getheight ()/2 );
G. drawline (0, basey, this. getwidth (), basey );
G. drawline (basex, 0, basex, this. getheight ());


// (X, y) x Y series points
G. filloval (basex + X, basey + Y, 5, 5 );
// Eight vertices. In any case, the previous polar coordinates start with basex. Otherwise, the (Yx) series vertices will run incorrectly and are wrong in the past.
// (-X, y)
G. filloval (basex-X, basey + Y, 5, 5 );

// (-X,-y)
G. filloval (basex-X, basey-y, 5, 5 );
// (X,-y)
G. filloval (basex + X, basey-y, 5, 5 );


// (Y, x) Yx Series
G. filloval (basex + Y, basey + X, 5 );

// (-Y, X)
G. filloval (basex-y, basey + X, 5, 5 );


G. filloval (basex-y, basey-X, 5, 5 );
 

G. filloval (basex + Y, basey-X, 5, 5 );


}
 
}

8-Point Circle Algorithm

Eightpoint. Java

Package com. Deng. cicle;

Import java. AWT. color;
Import java. AWT. dimension;
Import java. AWT. graphics;

Import javax. Swing. jpanel;

Public class eightpoint extends jpanel
{
 
Private int X, Y;

// Construction
Public eightpoint (int x, int y)
{
Super ();
This. setbackground (color. Black );
This. setpreferredsize (new dimension (800,600 ));
This. x = X;
This. Y = y;
Repaint ();

} // Construction ends
 
Public void paintcomponent (Graphics g) // The function name is incorrect.
{
Super. paintcomponent (g );
G. setcolor (color. Red );
Eight (G, x, y );

} // Paint graphic function ends

Public void eight (Graphics g, int X, int y)
{

Int basex, basey;
G. setcolor (color. Red );
Basex = (INT) (This. getwidth ()/2 );
Basey = (INT) (This. getheight ()/2 );
G. drawline (0, basey, this. getwidth (), basey );
G. drawline (basex, 0, basex, this. getheight ());


// (X, y) x Y series points
G. filloval (basex + X, basey + Y, 5, 5 );
// Eight vertices. In any case, the preceding polar coordinates start with basex. Otherwise, the (Yx) vertices may run incorrectly.
// (-X, y)
G. filloval (basex-X, basey + Y, 5, 5 );

// (-X,-y)
G. filloval (basex-X, basey-y, 5, 5 );
// (X,-y)
G. filloval (basex + X, basey-y, 5, 5 );


// (Y, x) Yx Series
G. filloval (basex + Y, basey + X, 5 );

// (-Y, X)
G. filloval (basex-y, basey + X, 5, 5 );


G. filloval (basex-y, basey-X, 5, 5 );
 

G. filloval (basex + Y, basey-X, 5, 5 );


}
}

Cicledriver. Java

Package com. Deng. cicle;

Import javax. Swing. jframe;

Public class cicledriver
{
Public static void main (string [] ARGs)
{
Jframe frame = new jframe ("circle and elliptic ");
Frame. setdefaclocloseoperation (jframe. exit_on_close );
Eightpoint eightpanel = new eightpoint (10, 18 );
Frame. getcontentpane (). Add (eightpanel );
Frame. Pack ();
Frame. setvisible (true );
}

}

Draw an arc

Arcpanel. Java

Package com. Deng. Arc;
// Algorithm for generating arcs dynamically, which requires a large amount of Computing
Import java. AWT. color;
Import java. AWT. dimension;
Import java. AWT. graphics;
Import javax. Swing. jpanel;

Public class arcpanel extends jpanel
{
Private int R;
Public arcpanel (int r)
{
Super ();
// Setd (jframe. exit_on_close );
This. r = R;
Setpreferredsize (New Dimensions (800,600 ));
Setbackground (color. Black );
Repaint ();
} // Construction ends

// Reload a method of the parent class, with a 1/8 arc
@ Override
Public void paintcomponent (Graphics g) // the value of "paint" is not "print ".
{
// Todo auto-generated method stub
Super. paintcomponent (g );
G. setcolor (color. Red );
Int X1 = 0;
Int X2;
Int Y2;
Do
{
X2 = X1 + 1;
Y2 = (INT) math. Round (math. SQRT (R * r-x2 * x2 ));
G. fillrect (X2, Y2, 1, 1 );
X1 = x2;

} While (X1 <(R ));
/// (Math. SQRT (2 )));
 
} // Paint ends
 
 
}

Arcdriver. Java

Package com. Deng. Arc;

Import javax. Swing. jframe;

Public class arcdriver {
Public static void main (string [] ARGs ){
Jframe framearc = new jframe ("eight arc ");
Framearc. setdefaclocloseoperation (jframe. exit_on_close );
Arcpanel Panel = new arcpanel (500 );
Framearc. getcontentpane (). Add (panel );
Framearc. Pack ();
Framearc. setvisible (true );
}

}

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.