废话少说,上图:
源码(复制粘贴即可用):
自定义控件(MyPanel)绘制:
package my;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.Arc2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import javax.swing.JPanel;
public class MyPanel extends JPanel
{
public MyPanel()
{
}
@Override
protected void paintComponent(Graphics g)
{
int width = getWidth();
int height = getHeight();
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(Color.WHITE);
g2d.clearRect(0, 0, width, height);
// 10个顶点的坐标
// 注意:这里是以中心为原点、x轴向右、y轴向上的坐标系,标准圆半径为1
Point2D.Double pA = new Point2D.Double(0, 1);
Point2D.Double pB = new Point2D.Double(-0.95106,0.30902);
Point2D.Double pC = new Point2D.Double(-0.58779,-0.80902);
Point2D.Double pD = new Point2D.Double(0.58779,-0.80902);
Point2D.Double pE = new Point2D.Double(0.95106,0.30902);
Point2D.Double pF = new Point2D.Double(0,-0.38197);
Point2D.Double pG = new Point2D.Double(0.36328,-0.11804);
Point2D.Double pH = new Point2D.Double(0.22452,0.30902);
Point2D.Double pI = new Point2D.Double(-0.22452,0.30902);
Point2D.Double pJ = new Point2D.Double(-0.36328,-0.11804);
Point2D.Double[] points = { pA, pI, pB, pJ, pC, pF, pD, pG, pE, pH };
// 坐标转换 ( 指定目标正方形,在里面画一个五角星 )
int rW = 200;
int rH = 200;
Rectangle rect1 = new Rectangle((width - rW) / 2, (height - rH) / 2, rW, rH);
int rmW = 250;
int rmH = 250;
Rectangle rect2 = new Rectangle((width - rmW) / 2, (height - rmH) / 2, rmW, rmH);
Rectangle rect3 = new Rectangle((width - (rmW + 50)) / 2, (height - (rmH + 50)) / 2, rmW + 50, rmH + 50);
Rectangle rect4 = new Rectangle((width - (rmW + 100)) / 2, (height - (rmH + 100)) / 2, rmW + 100, rmH + 100);
int radius_x = rect1.width / 2;
int radius_y = rect1.width / 2;
for(Point2D.Double point : points)
{
point.x = rect1.getCenterX() + radius_x * point.x ;
point.y = rect1.getCenterY() - radius_y * point.y ; // 坐标反转
}
Path2D outline = new Path2D.Double();
//绘制起点坐标
outline.moveTo(points[0].x, points[0].y);
for(int i = 0; i < points.length; i ++)
//连线
outline.lineTo(points[i].x, points[i].y);
//收尾相连,闭口
outline.closePath();
//绘制四个大圆弧
Arc2D arc1 = new Arc2D.Double(rect1, 0, 360, Arc2D.OPEN);
Arc2D arc2 = new Arc2D.Double(rect2, 0, 360, Arc2D.OPEN);
Arc2D arc3 = new Arc2D.Double(rect3, 0, 360, Arc2D.OPEN);
Arc2D arc4 = new Arc2D.Double(rect4, 0, 360, Arc2D.OPEN);
//线型宽度为10
g2d.setStroke(new BasicStroke(10));
//填充颜色
g2d.setPaint(new Color(0x7c0202));
g2d.fill(arc4);
g2d.setPaint(Color.WHITE);
g2d.fill(arc3);
g2d.setPaint(new Color(0x7c0202));
g2d.fill(arc2);
g2d.setPaint(new Color(0x0c1f6f));
g2d.fill(arc1);
g2d.setPaint(new Color(0xb0b0b0));
g2d.fill(outline);
}
}
窗口MyFrame:
package my;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyFrame extends JFrame
{
public MyFrame(String title)
{
super(title);
// Content Pane
JPanel root = new MyPanel();
this.setContentPane(root);
root.setLayout(new BorderLayout());
}
}
程序入口:
package my;
import javax.swing.JFrame;
public class Swing2
{
private static void createGUI()
{
// JFrame指一个窗口,构造方法的参数为窗口标题
// 语法:因为MyFrame是JFrame的子类,所以可以这么写
JFrame frame = new MyFrame("Swing Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置窗口的其他参数,如窗口大小
frame.setSize(500, 500);
// 显示窗口
frame.setVisible(true);
}
public static void main(String[] args)
{
// 此段代码间接地调用了 createGUI(),具体原理在 Swing高级篇 里讲解
// 初学者先照抄此代码框架即可
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run()
{
createGUI();
}
});
}
}
源码:美国盾牌