Java学习:使用Graphics2D类画的简易土星图案
画好的图案:Saturn.png
作图代码:Saturn_20150215.java(java版本:1.8.0_25)
import java.awt.image.BufferedImage; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class Saturn_20150215 { public static void main(String[] args) { int width = 900; int height = 500; String savepath = "D:\\Saturn.png"; BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = bi.createGraphics(); g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //画背景 g2d.setPaint(Color.BLACK); g2d.fillRect(0, 0, width, height); //图片标题 g2d.setPaint(Color.WHITE); g2d.setFont(new Font("Monospaced", Font.BOLD, 36)); g2d.drawString("SATURN BY TSYBIUS 2015/02/25", 50, 50); g2d.rotate(-1 * Math.PI / 10); //光环的上半环 for(int i = 0; i < 10; i++) { g2d.setStroke(new BasicStroke(5 - i / 2)); g2d.setPaint(new Color(255, 97 + i * 5, 0)); g2d.drawArc(80 + i * 9, 250 + i * 2, 530 - i * 18, 200 - i * 4, 0, 180); g2d.drawArc(80 - i * 9, 250 - i * 2, 530 + i * 18, 200 + i * 4, 0, 180); } //星球 g2d.setPaint(new Color(205, 113, 0)); g2d.fillOval(190, 210, 300, 300); //光环的下半环 for(int i = 0; i < 10; i++) { g2d.setStroke(new BasicStroke(5 - i / 2)); g2d.setPaint(new Color(255, 97 + i * 5, 0)); g2d.drawArc(80 + i * 9, 250 + i * 2, 530 - i * 18, 200 - i * 4, 180, 180); g2d.drawArc(80 - i * 9, 250 - i * 2, 530 + i * 18, 200 + i * 4, 180, 180); } g2d.rotate(Math.PI / 10); //绘制各个卫星 g2d.setStroke(new BasicStroke(5)); g2d.setPaint(Color.WHITE); //g2d.drawLine(0, height / 2, width, height / 2); //g2d.drawLine(width / 2, 0, width / 2, height); g2d.setFont(new Font("Monospaced", Font.BOLD, 16)); g2d.fillOval(700, 270, 10, 10); g2d.fillOval(720, 260, 10, 10); g2d.drawString("Janus & Epimetheus", 700, 255); g2d.fillOval(600, 370, 10, 10); g2d.drawString("Mimas", 600, 360); g2d.fillOval(300, 450, 10, 10); g2d.drawString("Enceladus", 300, 440); g2d.fillOval(100, 170, 10, 10); g2d.drawString("Tethy", 100, 160); g2d.fillOval(80, 155, 10, 10); g2d.drawString("Dione", 80, 145); g2d.fillOval(30, 460, 10, 10); g2d.drawString("Rhea", 30, 450); g2d.fillOval(830, 470, 10, 10); g2d.drawString("Titan", 830, 460); g2d.fillOval(880, 10, 10, 10); g2d.drawString("Hyperon", 820, 35); g2d.dispose(); //保存图片 try { ImageIO.write(bi, "PNG", new File(savepath)); } catch (IOException e) { e.printStackTrace(); } } }
END
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。