Java填充多边形相关API

来源:互联网 发布:sketch中文破解版 mac 编辑:程序博客网 时间:2024/05/16 09:09

转载自(略作修改):http://www.java2s.com/CN/Code/Java/2D-Graphics-GUI/Fillapolygon.htm

package RefMaterials;import java.awt.Color;import java.awt.Graphics;import java.awt.Polygon;import javax.swing.JFrame;import javax.swing.JPanel;public class FillPolyPanel extends JPanel {private static final long serialVersionUID = 8729489490886222075L;public void paintComponent(Graphics g) {/** * 1.绘制扇形~ */super.paintComponent(g);int centerX = 50;int centerY = 100;int radius = 40;int angle = 30;Color c = g.getColor();g.setColor(Color.green);/** * centerX-radius:左上角的x坐标~ * centerY-radius:左上角的y坐标 * 2*radius:圆的宽度 * 2*radius:圆的高度 * angle:起始角度 * 360-2*angle:扇形的扇区度数~ */g.fillArc(centerX - radius, centerY - radius, 2 * radius, 2 * radius, angle, 360 - 2 * angle);/** * 2.绘制多边形,这个就是我所需要的东西了~ */Polygon p = new Polygon();centerX = 150;for (int i = 0; i < 5; i++)p.addPoint((int) (centerX + radius * Math.cos(i * 2 * Math.PI / 5)),(int) (centerY + radius * Math.sin(i * 2 * Math.PI / 5)));//g.fillPolygon(p);g.drawPolygon(p);/** * 3.绘制螺旋状的纹路~ */p = new Polygon();centerX = 250;for (int i = 0; i < 360; i++) {double t = i / 360.0;p.addPoint((int) (centerX + radius * t * Math.cos(8 * t * Math.PI)),(int) (centerY + radius * t * Math.sin(8 * t * Math.PI)));}g.fillPolygon(p);g.setColor(c);}public static void main(String[] args) {JFrame frame = new JFrame();frame.setTitle("FillPoly");frame.setSize(300, 200);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().add(new FillPolyPanel());frame.setVisible(true);}}


原创粉丝点击