当前位置:网站首页 > Java教程 > 正文

红旗java教程



 1 package cn.hq.szxy;  2  3 import javax.swing.*;  4 import java.awt.geom.*;  5 import java.awt.*;  6  7 /  8  *  9  * @author HQ 10  * @date 2018/10/1 11 */ 12 public class FiveStarFlag extends JPanel { 13 14 public static void main(String[] args) { 15 JFrame jFrame = new JFrame("五星红旗"); 16 jFrame.getContentPane().add(new FiveStarFlag(600)); 17  jFrame.pack(); 18  jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 19 jFrame.setLocationRelativeTo(null); 20 jFrame.setVisible(true); 21  } 22 23 / 24  * 创建一个五角星形状. 25  * 该五角星的中心坐标为(sx,sy),中心到顶点的距离为radius,其中某个顶点与中心的连线的偏移角度为theta(弧度) 26  * 27  * @return pentacle 一个☆ 28 */ 29 public static Shape createPentacle(double sx, double sy, double radius, double theta) { 30 final double arc = Math.PI / 5; 31 final double rad = Math.sin(Math.PI / 10) / Math.sin(3 * Math.PI / 10); 32 GeneralPath path = new GeneralPath(); 33 path.moveTo(1, 0); 34 for (int i = 0; i < 5; i++) { 35 path.lineTo(rad * Math.cos((1 + 2 * i) * arc), rad * Math.sin((1 + 2 * i) * arc)); 36 path.lineTo(Math.cos(2 * (i + 1) * arc), Math.sin(2 * (i + 1) * arc)); 37  } 38  path.closePath(); 39 AffineTransform atf = AffineTransform.getScaleInstance(radius, radius); 40 atf.translate(sx / radius, sy / radius); 41  atf.rotate(theta); 42 return atf.createTransformedShape(path); 43  } 44 45 private int width, height; 46 private double maxR = 0.15, minR = 0.05; 47 private double maxX = 0.25, maxY = 0.25; 48 private double[] minX = {0.50, 0.60, 0.60, 0.50}; 49 private double[] minY = {0.10, 0.20, 0.35, 0.45}; 50 51 / 52  * 创建一个宽度为width的国旗 53 */ 54 public FiveStarFlag(int width) { 55 this.width = width / 3 * 3; 56 this.height = width / 3 * 2; 57 setPreferredSize(new Dimension(this.width, this.height)); 58  } 59 60  @Override 61 protected void paintComponent(Graphics g) { 62 Graphics2D graphics2D = (Graphics2D) g; 63 64 //画旗面 65  graphics2D.setPaint(Color.RED); 66 graphics2D.fillRect(0, 0, width, height); 67 68 //画大☆ 69 double ox = height * maxX, oy = height * maxY; 70  graphics2D.setPaint(Color.YELLOW); 71 graphics2D.fill(createPentacle(ox, oy, height * maxR, -Math.PI / 2)); 72 73 //画小★ 74 for (int i = 0; i < 4; i++) { 75 double sx = minX[i] * height, sy = minY[i] * height; 76 double theta = Math.atan2(oy - sy, ox - sx); 77 graphics2D.fill(createPentacle(sx, sy, height * minR, theta)); 78  } 79  } 80 }

版权声明


相关文章:

  • java装修教程2025-02-10 08:58:03
  • 永中java 教程2025-02-10 08:58:03
  • java扫描仪教程2025-02-10 08:58:03
  • 安装java 16完整教程2025-02-10 08:58:03
  • java web start 教程2025-02-10 08:58:03
  • 小包小包java版安装教程2025-02-10 08:58:03
  • java深度教程2025-02-10 08:58:03
  • java基础教程402025-02-10 08:58:03
  • java教程排行榜2025-02-10 08:58:03
  • java c语言教程2025-02-10 08:58:03