drawpolygons.java

来自「java 课件与资料 java 课件与资料 java 课件与资料 java 课件」· Java 代码 · 共 57 行

JAVA
57
字号
// Fig. 12.21: DrawPolygons.java
// Drawing polygons.
import java.awt.*;
import javax.swing.*;

public class DrawPolygons extends JFrame {

   // set window's title bar String and dimensions
   public DrawPolygons()
   {
      super( "Drawing Polygons" );

      setSize( 275, 230 );
      setVisible( true );
   }

   // draw polygons and polylines
   public void paint( Graphics g )
   {
      super.paint( g );  // call superclass's paint method

      int xValues[] = { 20, 40, 50, 30, 20, 15 };
      int yValues[] = { 50, 50, 60, 80, 80, 60 };
      Polygon polygon1 = new Polygon( xValues, yValues, 6 );

      g.drawPolygon( polygon1 );

      int xValues2[] = { 70, 90, 100, 80, 70, 65, 60 };
      int yValues2[] = { 100, 100, 110, 110, 130, 110, 90 };

      g.drawPolyline( xValues2, yValues2, 7 );

      int xValues3[] = { 120, 140, 150, 190 };
      int yValues3[] = { 40, 70, 80, 60 };

      g.fillPolygon( xValues3, yValues3, 4 );

      Polygon polygon2 = new Polygon();
      polygon2.addPoint( 165, 135 );
      polygon2.addPoint( 175, 150 );
      polygon2.addPoint( 270, 200 );
      polygon2.addPoint( 200, 220 );
      polygon2.addPoint( 130, 180 );

      g.fillPolygon( polygon2 );

   } // end method paint

   // execute application
   public static void main( String args[] )
   {
      DrawPolygons application = new DrawPolygons();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   }

} // end class DrawPolygons

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?