在Java小程序中绘制多边形
多边形是一个封闭图形,具有将一个顶点连接到另一个顶点的有限线段集。多边形由一组 (x, y) 坐标对组成,其中每一对都是多边形的顶点。多边形的边是在两个连续坐标对之间绘制的线,并且从第一对到最后一对绘制一条线段。
我们可以通过三种方式在Java小程序中绘制多边形:
- 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); } }
输出 :
- drawPolygon(Polygon p) :使用 Polygon 类的给定对象绘制多边形。
// 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); } }
输出 :
- drawLine(int x, int y, int x1, int 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]); } }
输出 :
注意:上述函数是Java.awt 包的一部分,属于Java.awt.Graphics 类。此外,这些代码可能无法在在线编译器中运行,请使用离线编译器。程序员可以根据需要更改 x 和 y 坐标