在Java小程序中绘制多边形

多边形是一个封闭的图形,有一组有限的线段将一个顶点连接到另一个顶点。多边形由一组(x,y)坐标对组成,每对坐标对都是多边形的顶点。多边形的边是在两个连续的坐标对之间绘制的线,从第一对到最后一对绘制一条线段。

null

我们可以通过三种方式在java applet中绘制多边形:

  1. drawPolygon(int[]x,int[]y,int numberofpoints): 使用给定的x和y点集绘制多边形。

    // Java program to draw polygon using
    // drawPolygon(int[] x, int[] y, int numberofpoints)
    // function
    import java.awt.*;
    import javax.swing.*;
    public class poly extends JApplet {
    // called when applet is started
    public void init()
    {
    // set the size of applet to 300, 300
    setSize( 200 , 200 );
    show();
    }
    // invoked when applet is started
    public void start()
    {
    }
    // invoked when applet is closed
    public void stop()
    {
    }
    public void paint(Graphics g)
    {
    // x coordinates of vertices
    int x[] = { 10 , 30 , 40 , 50 , 110 , 140 };
    // y coordinates of vertices
    int y[] = { 140 , 110 , 50 , 40 , 30 , 10 };
    // number of vertices
    int numberofpoints = 6 ;
    // set the color of line drawn to blue
    g.setColor(Color.blue);
    // draw the polygon using drawPolygon function
    g.drawPolygon(x, y, numberofpoints);
    }
    }

    
    

    输出: 图片[1]-在Java小程序中绘制多边形-yiteyi-C++库

  2. drawPolygon(多边形p): 使用多边形类的给定对象绘制多边形。

    // Java program to draw polygon
    // using drawPolygon(Polygon p)
    // function
    import java.awt.*;
    import javax.swing.*;
    public class poly extends JApplet {
    // called when applet is started
    public void init()
    {
    // set the size of applet to 300, 300
    setSize( 200 , 200 );
    show();
    }
    // invoked when applet is started
    public void start()
    {
    }
    // invoked when applet is closed
    public void stop()
    {
    }
    public void paint(Graphics g)
    {
    // x coordinates of vertices
    int x[] = { 10 , 30 , 40 , 50 , 110 , 140 };
    // y coordinates of vertices
    int y[] = { 140 , 110 , 50 , 40 , 30 , 10 };
    // number of vertices
    int numberofpoints = 6 ;
    // create a polygon with given x, y coordinates
    Polygon p = new Polygon(x, y, numberofpoints);
    // set the color of line drawn to blue
    g.setColor(Color.blue);
    // draw the polygon using drawPolygon
    // function using object of polygon class
    g.drawPolygon(p);
    }
    }

    
    

    输出: 图片[1]-在Java小程序中绘制多边形-yiteyi-C++库

  3. 抽绳(点x、点y、点x1、点y1): 在这种方法中,我们将用一条线段连接相邻顶点,并连接第一个和最后一个顶点。

    // Java code to draw a polygon
    // using  drawLine(int x, int y, int x1, int y1)
    // function
    import java.awt.*;
    import javax.swing.*;
    public class poly extends JApplet {
    // called when applet is started
    public void init()
    {
    // set the size of applet to 300, 300
    setSize( 200 , 200 );
    show();
    }
    // invoked when applet is started
    public void start()
    {
    }
    // invoked when applet is closed
    public void stop()
    {
    }
    public void paint(Graphics g)
    {
    // x coordinates of vertices
    int x[] = { 10 , 30 , 40 , 50 , 110 , 140 };
    // y coordinates of vertices
    int y[] = { 140 , 110 , 50 , 40 , 30 , 10 };
    // number of vertices
    int numberofpoints = 6 ;
    // set the color of line drawn to blue
    g.setColor(Color.blue);
    // join the adjacent vertices
    for ( int i = 0 ; i < numberofpoints - 1 ; i++)
    g.drawLine(x[i], y[i], x[i + 1 ], y[i + 1 ]);
    // join the first and last vertex
    g.drawLine(x[ 0 ], y[ 0 ], x[numberofpoints - 1 ], y[numberofpoints - 1 ]);
    }
    }

    
    

    输出: 图片[1]-在Java小程序中绘制多边形-yiteyi-C++库

注: 上述函数是java的一部分。awt包,属于java。awt。图形课。此外,这些代码可能不会在联机编译器中运行。请使用脱机编译器。程序员可以根据需要更改x和y坐标

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享