看过电影全方位无死角的感觉很好,我就画一面国旗表达一下此刻的心情吧。
package com.gzz;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import javax.swing.JFrame;
public class RedFlag extends JFrame {
private static final long serialVersionUID = 1L;
private static final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
private static final double maxR = 0.15, minR = 0.05;
private static final double maxX = 0.25, maxY = 0.25;
private static final double[] minX = { 0.50, 0.60, 0.60, 0.50 };
private static final double[] minY = { 0.10, 0.20, 0.35, 0.45 };
public static void main(String[] args) {
RedFlag redFlag = new RedFlag();
redFlag.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.exit(0);
}
});
redFlag.setUndecorated(true);
redFlag.setExtendedState(JFrame.MAXIMIZED_BOTH);
redFlag.setVisible(true);
}
@Override
public void paint(Graphics g) {
Graphics2D graphics2D = (Graphics2D) g;
graphics2D.setPaint(Color.RED);
graphics2D.fillRect(0, 0, screen.width, screen.height);// 画旗面
double ox = screen.height * maxX, oy = screen.height * maxY;
graphics2D.setPaint(Color.YELLOW);
graphics2D.fill(createPentacle(ox, oy, screen.height * maxR, -Math.PI / 2));// 画大☆
for (int i = 0; i < 4; i++) {// 画小★
double sx = minX[i] * screen.height, sy = minY[i] * screen.height;
double theta = Math.atan2(oy - sy, ox - sx);
graphics2D.fill(createPentacle(sx, sy, screen.height * minR, theta));
}
}
/**
* 创建一个五角星形状. 该五角星的中心坐标为(sx,sy),中心到顶点的距离为radius,其中某个顶点与中心的连线的偏移角度为theta(弧度)
*/
private 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);
}
}
五星红旗
这里用了大量三角函数,弧度,角度,平面坐标系,为了教会上小学三年级的儿子画国旗,于是我把程序简化了一下。
package com.gzz.common.base;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
public class RedFlag2 extends JFrame {
private static final long serialVersionUID = 1L;
private static final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
private static final int scale = screen.width / 50;
public static void main(String[] args) {
RedFlag2 redFlag = new RedFlag2();
redFlag.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.exit(0);// 单击退出
}
});
redFlag.setUndecorated(true);// 隐藏标题栏
redFlag.setExtendedState(JFrame.MAXIMIZED_BOTH);// 最大化
redFlag.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
redFlag.setVisible(true);
}
@Override
public void paint(Graphics g) {
Graphics2D graphic = (Graphics2D) g;
graphic.setPaint(Color.RED);
graphic.fillRect(0, 0, screen.width, screen.height);// 画旗面
graphic.setPaint(Color.YELLOW);
// 画大星
graphic.setFont(new Font("宋体", Font.BOLD, scale * 8));
graphic.drawString("★", scale * 4, scale * 11);
// 画小星
graphic.setFont(new Font("宋体", Font.BOLD, scale * 3));
graphic.drawString("★", scale * 11, scale * 4);
graphic.setFont(new Font("宋体", Font.BOLD, scale * 3));
graphic.drawString("★", scale * 14, scale * 8);
graphic.setFont(new Font("宋体", Font.BOLD, scale * 3));
graphic.drawString("★", scale * 14, scale * 12);
graphic.setFont(new Font("宋体", Font.BOLD, scale * 3));
graphic.drawString("★", scale * 11, scale * 16);
}
}
网友评论