> drawing polygons in java ~ Online tutorial

drawing polygons in java

Drawing Polygons and Polylines
  • polygen are multisided shapes. Polylines are a series of connected points.
  • Graphics methods for drawing polygons and polylines
  • The program of draws polygons and polylines using the methods and constructors
    Lines 18 through 20 create two int arrays and use them to specify the points for
    Polygon poly1.
  • The Polygon constructor call at line 20 receives array xValues,
    which contains the x-coordinate of each point, array yValues, which contains the y-coordinate
    of each point, and 6 (the number of points in the polygon). Line 22 displays poly1
    by passing it as an argument to Graphics method drawPolygon.
public void drawPolygon( int xPoints[], int yPoints[],
int points )
Draws a polygon. The x-coordinate of each point is specified in the xPoints
array and the y-coordinate of each point is specified in the yPoints array. The
last argument specifies the number of points. This method draws a closed polygon—
even if the last point is different from the first point.

public void drawPolygon( Polygon p )
Draws the specified closed polygon.

public void fillPolygon( int xPoints[], int yPoints[],
int points )
Draws a solid polygon. The x-coordinate of each point is specified in the
xPoints array and the y-coordinate of each point is specified in the yPoints
array. The last argument specifies the number of points. This method draws a
closed polygon—even if the last point is different from the first point.

public void fillPolygon( Polygon p )
Draws the specified solid polygon. The polygon is closed.

public Polygon()
// Polygon class Constructs a new polygon object. The polygon does not contain any points.

public Polygon( int xValues[], int yValues[], // Polygon class
int numberOfPoints )
Constructs a new polygon object. The polygon has numberOfPoints sides,
with each point consisting of an x-coordinate from xValues and a y-coordinate from yValues

public void drawPolyline( int xPoints[], int yPoints[],
int points )
Draws a series of connected lines. The x-coordinate of each point is specified in
the xPoints array and the y-coordinate of each point is specified in the
yPoints array. The last argument specifies the number of points. If the last
point is different from the first point, the polyline is not closed.

Program

2 // Drawing polygons

3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
67
public class DrawPolygons extends JFrame {
8 public DrawPolygons()
9 {
10 super( "Drawing Polygons" );
11
12 setSize( 275, 230 );
13 show();
14 }
15
16 public void paint( Graphics g )
17 {
18 int xValues[] = { 20, 40, 50, 30, 20, 15 };
19 int yValues[] = { 50, 50, 60, 80, 80, 60 };
20 Polygon poly1 = new Polygon( xValues, yValues, 6 );
21
22 g.drawPolygon( poly1 );

24 int xValues2[] = { 70, 90, 100, 80, 70, 65, 60 };
25 int yValues2[] = { 100, 100, 110, 110, 130, 110, 90 };
26
27 g.drawPolyline( xValues2, yValues2, 7 );
28
29 int xValues3[] = { 120, 140, 150, 190 };
30 int yValues3[] = { 40, 70, 80, 60 };
31
32 g.fillPolygon( xValues3, yValues3, 4 );
33
34 Polygon poly2 = new Polygon();
35 poly2.addPoint( 165, 135 );
36 poly2.addPoint( 175, 150 );
37 poly2.addPoint( 270, 200 );
38 poly2.addPoint( 200, 220 );
39 poly2.addPoint( 130, 180 );
40
41 g.fillPolygon( poly2 );
42 }
43
44 public static void main( String args[] )
45 {
46 DrawPolygons app = new DrawPolygons();
47
48 app.addWindowListener(
49 new WindowAdapter() {
50 public void windowClosing( WindowEvent e )
51 {
52 System.exit( 0 );
53 }
54 }
55 );
56 }
57 }











Buy It Now


Please Give Us Your 1 Minute In Sharing This Post!
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Powered By: BloggerYard.Com

0 comments: