TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
用java画个星条旗
- public class DrawFlag
- {
- public static void main(java.lang.String[] args) throws java.io.IOException
- {
- DrawFlag.drawFlag();
- }
- public static void drawFlag() throws java.io.IOException
- {
- java.awt.image.BufferedImage bufferedImage =
- new java.awt.image.BufferedImage(420, 221, java.awt.image.BufferedImage.TYPE_INT_RGB);
- java.awt.Graphics gp = bufferedImage.getGraphics();
- java.awt.Color color = new java.awt.Color(0);
- for (int i = 0; i < 13; i++)
- {
- if (i % 2 == 0)
- {
- color = new java.awt.Color(204, 0, 0);
- }
- else
- {
- color = new java.awt.Color(255, 255, 255);
- }
- gp.setColor(color);
- gp.fillRect(0, 17 * i, 420, 17);
- }
- color = new java.awt.Color(0, 0, 102);
- gp.setColor(color);
- gp.fillRect(0, 0, 168, 119);
- color = new java.awt.Color(255, 255, 255);
- gp.setColor(color);
- int left = 0;
- int top = 0;
- for (int i = 0; i < 9; i++)
- {
- top = (i + 1) * 12;
- if (i % 2 == 0)
- {
- left = -15;
- for (int j = 0; j < 6; j++)
- {
- left = left + 28;
- DrawFlag.DrawPentacle(gp, (double) left, (double) top, (double) 8, 0);
- }
- }
- else
- {
- left = -2;
- for (int j = 0; j < 5; j++)
- {
- left = left + 28;
- DrawFlag.DrawPentacle(gp, (double) left, (double) top, (double) 8, 0);
- }
- }
- }
- color = new java.awt.Color(0, 0, 0);
- gp.setColor(color);
- java.awt.Font font = new java.awt.Font("宋体", java.awt.Font.CENTER_BASELINE, 12);
- gp.setFont(font);
- gp.drawString("这些星好像有点怪", 300, 200);
- javax.imageio.ImageIO.write(bufferedImage, "jpg", new java.io.File("flag.jpg"));
- }
- public static void DrawPentacle(java.awt.Graphics gp, double x, double y, double r, int a)
- {
- int A;
- double R, X, Y;
- double[] _X = new double[11];
- double[] _Y = new double[11];
- int[] _x = new int[3];
- int[] _y = new int[3];
- a = a + 90;
- R =Math.sin(Math.toRadians((double) 18)) * r / Math.sin(Math.toRadians((double) 126));
- for (int i = 0; i < 5; i++)
- {
- A = a + 72 * i;
- X = R * java.lang.Math.cos(java.lang.Math.toRadians((double) A)) + x;
- Y = R * java.lang.Math.sin(java.lang.Math.toRadians((double) A)) + y;
- _X[i * 2] = X;
- _Y[i * 2] = Y;
- A = a + 72 * i + 36;
- X = r * java.lang.Math.cos(java.lang.Math.toRadians((double) A)) + x;
- Y = r * java.lang.Math.sin(java.lang.Math.toRadians((double) A)) + y;
- _X[i * 2 + 1] = X;
- _Y[i * 2 + 1] = Y;
- }
- _X[10] = x;
- _Y[10] = y;
- for (int i = 0; i < 5; i++)
- {
- _x[0] = DrawFlag.round(_X[i * 2], 0);
- _x[1] = DrawFlag.round(_X[i * 2 + 1], 0);
- _x[2] = DrawFlag.round(_X[10], 0);
- _y[0] = DrawFlag.round(_Y[i * 2], 0);
- _y[1] = DrawFlag.round(_Y[i * 2 + 1], 0);
- _y[2] = DrawFlag.round(_Y [10], 0);
- gp.fillPolygon(_x, _y, 3);
- _x[0] = DrawFlag.round(_X[i * 2 + 1], 0);
- _x[1] = DrawFlag.round(_X[(i == 4) ? (0) : (i * 2 + 2)], 0);
- _x[2] = DrawFlag.round(_X[10], 0);
- _y[0] = DrawFlag.round(_Y[i * 2 + 1], 0);
- _y[1] = DrawFlag.round(_Y[(i == 4) ? (0) : (i * 2 + 2)], 0);
- _y[2] = DrawFlag.round(_Y[10], 0);
- gp.fillPolygon(_x, _y, 3);
- }
- }
- public static int round(double d, int scale)
- {
- if (scale < 0)
- {
- scale = 0;
- }
- java.math.BigDecimal b = new java.math.BigDecimal(java.lang.Double.toString(d));
- java.math.BigDecimal one = new java.math.BigDecimal("1");
- return b.divide(one, scale, java.math.BigDecimal.ROUND_HALF_UP).intValue();
- }
- }
复制代码
源码下载:http://file.javaxxz.com/2014/11/6/235614828.zip |
|