最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

用Java畫出五星紅旗

2022-05-19 17:48 作者:奪命小喵Official  | 我要投稿

文章目錄

  • 五星紅旗

  • 前言

    • 使用步驟:

    • 1.引入包

    • 2.創(chuàng)建畫布

    • 3.創(chuàng)建一個五角星形狀

    • 4.創(chuàng)建一個寬度為width的國旗

    • 5.畫大星星、小星星和旗面

  • 完整代碼


前言

一個富有民族自豪感的程序員用Java畫出了五星紅旗,在這里和大家分享。

使用步驟:

1.引入包

代碼如下:

import javax.swing.*;

import java.awt.geom.*;

import java.awt.*;

2.創(chuàng)建畫布

代碼如下:

public static void main(String[] args) {

? ? ?? JFrame jFrame = new JFrame("五星紅旗");

? ? ?? jFrame.getContentPane().add(new FiveStarFlag(600));

? ? ?? jFrame.pack();

?????? jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ?

? ???? jFrame.setLocationRelativeTo(null);

? ? ?? jFrame.setVisible(true); ??

}

3.創(chuàng)建一個五角星形狀

代碼如下:

public static void main(String[] args) {

? ? ?? JFrame jFrame = new JFrame("五星紅旗");

? ? ?? jFrame.getContentPane().add(new FiveStarFlag(600));

? ? ?? jFrame.pack(); ? ? ?

?????? jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ? ? ?

? ? ?? jFrame.setLocationRelativeTo(null);

? ? ?? jFrame.setVisible(true); ? ?

}

該五角星的中心坐標(biāo)為(sx,sy),中心到頂點的距離為radius,其中某個頂點與中心的連線的偏移角度為theta(弧度)

4.創(chuàng)建一個寬度為width的國旗

代碼如下:

public FiveStarFlag(int width) {

? ? ?? this.width = width / 3 * 3;

? ? ?? this.height = width / 3 * 2;

? ? ?? setPreferredSize(new Dimension(this.width, this.height)); ??

}

5.畫大星星、小星星和旗面

代碼如下:

@Override ? ?protected void paintComponent(Graphics g) {

? ? ?? Graphics2D graphics2D = (Graphics2D) g;

? ? ?? //畫旗面

? ? ?? graphics2D.setPaint(Color.RED);

? ? ?? graphics2D.fillRect(0, 0, width, height);

? ? ?? //畫大☆

? ? ?? double ox = height * maxX, oy = height * maxY;

? ? ?? graphics2D.setPaint(Color.YELLOW);

? ? ?? graphics2D.fill(createPentacle(ox, oy, height * maxR, -Math.PI / 2));

? ? ?? //畫小★

? ? ?? for (int i = 0; i < 4; i++) {

? ? ? ? ?? double sx = minX[i] * height, sy = minY[i] * height;

? ? ? ? ?? double theta = Math.atan2(oy - sy, ox - sx);

? ? ? ? ?? graphics2D.fill(createPentacle(sx, sy, height * minR, theta));

? ? ?? }

?? }

完整代碼如下

import javax.swing.*;

import java.awt.geom.*;

import java.awt.*;


public class FiveStarFlag extends JPanel {

?? public static void main(String[] args) {

? ? ?? JFrame jFrame = new JFrame("五星紅旗");

? ? ?? jFrame.getContentPane().add(new FiveStarFlag(600));

? ? ?? jFrame.pack();

? ? ?? jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

? ? ?? jFrame.setLocationRelativeTo(null);

? ? ?? jFrame.setVisible(true);

?? } ? ?

/** ? ? * 創(chuàng)建一個五角星形狀.

? ? * 該五角星的中心坐標(biāo)為(sx,sy),中心到頂點的距離為radius,其中某個頂點與中心的連線的偏移角度為theta(弧度) ? ? *

? ? * @return pentacle 一個☆ ? ? */

?? public static Shape createPentacle(double sx, double sy, double radius, double theta) {

? ? ?? final double arc = Math.PI / 5;

? ? ?? final double rad = Math.sin(Math.PI / 10) / Math.sin(3 * Math.PI / 10);

? ? ?? GeneralPath path = new GeneralPath();

? ? ?? path.moveTo(1, 0);

? ? ?? for (int i = 0; i < 5; i++) {

? ? ? ? ?? path.lineTo(rad * Math.cos((1 + 2 * i) * arc), rad * Math.sin((1 + 2 * i) * arc));

? ? ? ? ?? path.lineTo(Math.cos(2 * (i + 1) * arc), Math.sin(2 * (i + 1) * arc));

? ? ??

}

? ? ?? path.closePath();

? ? ?? AffineTransform atf = AffineTransform.getScaleInstance(radius, radius);

? ? ?? atf.translate(sx / radius, sy / radius);

? ? ?? atf.rotate(theta);

? ? ?? return atf.createTransformedShape(path);

?? }

?? private int width, height;

?? private double maxR = 0.15, minR = 0.05;

?? private double maxX = 0.25, maxY = 0.25;

?? private double[] minX = {0.50, 0.60, 0.60, 0.50};

?? private double[] minY = {0.10, 0.20, 0.35, 0.45};

?? /** ? ? * 創(chuàng)建一個寬度為width的國旗 ? ? */

?? public FiveStarFlag(int width) {

? ? ?? this.width = width / 3 * 3;

? ? ?? this.height = width / 3 * 2;

? ? ?? setPreferredSize(new Dimension(this.width, this.height));

?? } ??

@Override

?? protected void paintComponent(Graphics g) {

? ? ?? Graphics2D graphics2D = (Graphics2D) g;

? ? ?? //畫旗面

? ? ?? graphics2D.setPaint(Color.RED);

? ? ?? graphics2D.fillRect(0, 0, width, height);

? ? ?? //畫大☆ ? ? ??

double ox = height * maxX, oy = height * maxY;

? ? ?? graphics2D.setPaint(Color.YELLOW);

? ? ?? graphics2D.fill(createPentacle(ox, oy, height * maxR, -Math.PI / 2));

? ? ?? //畫小★

? ? ?? for (int i = 0; i < 4; i++) {

? ? ? ? ?? double sx = minX[i] * height, sy = minY[i] * height;

? ? ? ? ?? double theta = Math.atan2(oy - sy, ox - sx);

? ? ? ? ?? graphics2D.fill(createPentacle(sx, sy, height * minR, theta));

? ? ?? }

?? }

}


用Java畫出五星紅旗的評論 (共 條)

分享到微博請遵守國家法律
临潭县| 理塘县| 花垣县| 枣阳市| 西华县| 周至县| 灵石县| 新和县| 乌苏市| 祥云县| 屯昌县| 武夷山市| 乐清市| 衡水市| 兴海县| 仁布县| 工布江达县| 阿坝| 巫溪县| 东明县| 清河县| 五指山市| 图木舒克市| 囊谦县| 鹤庆县| 玉屏| 清徐县| 三穗县| 普陀区| 红河县| 杨浦区| 福泉市| 朝阳县| 鹿邑县| 礼泉县| 延吉市| 平武县| 沙河市| 信丰县| 成都市| 泾川县|