曾经翻过一本《混沌与分形-科学的新疆界》的书,当时最初的印象就是使用程序脚本呈现出各种好看的图案,以及细微的起始值的差别会造成的截然不同的结果。
昨天又翻到了『细胞自动机』以及『生命游戏』这样的词,曾经在霍金的书里也看到过有一个计算机科学家用程序模拟物种演化的片段。细胞自动机正是当时的那个程序。
想用java的程序把它表现出来,这需要一点点的绘制平面图形的方法。这里使用的是awt和swing。具体内容可以参照《Java核心技术(卷1):基础知识》的第七章,处理2D图形那一节。大致的逻辑就是在框架Frame上放置组件Component,然后组件重写父类中的绘图方法。在绘图方法的参数中,会传入一个图形Graphics用来绘图。
这里的Graphics强制类型转换为它的子类Graphics2D,然后新建几何图形后,可以使用填充fill和画线draw方法把对应的色彩设置在图形上。这里的每个组件的子类对象只会调用绘图方法一次,要绘制新图形需要创建新的组件对象。
package yumu.chaos.lifegame.test; import javax.swing.JFrame; public class JFrameTest { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setTitle("Game Of Life"); frame.setLocation(300, 100); frame.setSize(700, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); System.out.println("JFrame created"); } }
运行结果如下:
JFrame created
4.获取屏幕的宽和高
package yumu.chaos.lifegame.test; import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JFrame; public class ToolkitTest { public static void main(String[] args) { Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension dimension = toolkit.getScreenSize(); int screenWidth = dimension.width; int screenHeight = dimension.height; System.out.println(screenWidth); System.out.println(screenHeight); JFrame frame = new JFrame(); frame.setTitle("Game Of Life"); frame.setSize(screenWidth/2, screenHeight/2); frame.setLocationByPlatform(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); System.out.println("JFrame created"); } }
运行结果如下:
1366 768 JFrame created
5.在组件上写HelloWorld
package yumu.chaos.lifegame.test; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.JComponent; import javax.swing.JFrame; public class JComponentTest extends JComponent { private static final int MESSAGE_X = 100; private static final int MESSAGE_Y = 100; private static final int DEFAULT_WIDTH = 400; private static final int DEFAULT_HEIGHT = 200; @Override protected void paintComponent(Graphics g) { g.drawString("Hello World", MESSAGE_X, MESSAGE_Y); } @Override public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); } public static void main(String[] args) { JComponent component = new JComponentTest(); JFrame frame = new JFrame(); frame.setLocation(400, 100); frame.setSize(600, 400); frame.add(component); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
运行结果如下:
6.绘制平面几何图形
package yumu.chaos.lifegame.test; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Rectangle2D; import javax.swing.JComponent; import javax.swing.JFrame; public class Graphics2DTest { public static void main(String[] args) { JFrame frame = new JFrame(); JComponent comp = new JComponentTest(); frame.add(comp); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } private static class JComponentTest extends JComponent { public static final int DEFAULT_WIDTH = 600; public static final int DEFAULT_HEIGHT = 400; @Override protected void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; double left = 100; double top = 50; double width = 400; double height = 250; Rectangle2D rect = new Rectangle2D.Double(left, top, width, height); g2.draw(rect); g2.setPaint(new Color(255, 255, 223)); g2.fill(rect); g2.setPaint(Color.BLACK); Ellipse2D ellipse = new Ellipse2D.Double(); ellipse.setFrame(rect); g2.draw(ellipse); double centerX = rect.getCenterX(); double centerY = rect.getCenterY(); double radius = rect.getHeight()/2; Ellipse2D circle = new Ellipse2D.Double(); circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY + radius); g2.draw(circle); double x1 = rect.getX(); double y1 = rect.getY(); double x2 = rect.getMaxX(); double y2 = rect.getMaxY(); Line2D line = new Line2D.Double(x1, y1, x2, y2); g2.setPaint(Color.BLUE); g2.draw(line); } @Override public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); } } }
运行结果如下:
7.测试随机数
package yumu.chaos.lifegame.test; import java.util.Random; public class RandomTest { public static void main(String[] args) { Random random = new Random(); for(int i = 0; i < 100; ++i){ boolean val = random.nextBoolean(); System.out.println(val); } } }
运行结果如下:
false true false false false true true true ……
混沌与分形-科学的新疆界 - 百度百科
康威生命游戏 - 维基百科
Java 2 用户界面 - www.ibm.com
Java核心技术(卷1):基础知识 - www.amazon.cn
请点击下方的『 关注我 』关注我!
本文地址: http://www.cnblogs.com/kodoyang/p/Chaos_Graphics2D_Awt_Swing.html
雨木阳子
2015年8月9日