曾經翻過一本《混沌與分形-科學的新疆界》的書,當時最初的印象就是使用程序腳本呈現出各種好看的圖案,以及細微的起始值的差別會造成的截然不同的結果。
昨天又翻到了『細胞自動機』以及『生命游戲』這樣的詞,曾經在霍金的書里也看到過有一個計算機科學家用程序模擬物種演化的片段。細胞自動機正是當時的那個程序。


想用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.運行結果如下:
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"); } }運行結果如下:
1366768JFrame created
5.在組件上寫HelloWorldpackage 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); } }}運行結果如下:
falsetruefalsefalsefalsetruetruetrue……
參照:混沌與分形-科學的新疆界 - 百度百科康威生命游戲 - 維基百科Java 2 用戶界面 - www.ibm.comJava核心技術(卷1):基礎知識 - www.amazon.cn
請點擊下方的『關注我』關注我!
本文地址:http://m.survivalescaperooms.com/kodoyang/p/Chaos_Graphics2D_Awt_Swing.html
雨木陽子
2015年8月15日
新聞熱點
疑難解答