转载自(略作修改):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);
}
}
本文通过Java编程语言实现了一个图形界面,展示了如何绘制扇形、多边形及螺旋纹路,具体包括扇形的绘制、五边形多边形的创建与填充、以及螺旋纹路的生成过程。
606

被折叠的 条评论
为什么被折叠?



