📄 cca.java
字号:
package ca;// Simple Java class for visualization of cellular automata.// Juha Haataja, CSC, Finland.// E-mail: Juha.Haataja@csc.fi.import java.applet.*;import java.awt.*;import java.awt.event.*;// Container for the CA image dataclass CA_Canvas extends Canvas implements Runnable { private int width = -1; private int height = -1; private int gridwidth=1; private int number,number2; private int type=0; private TotalisticCA CA = null; private int[] config = null; private Color[] coloring = null; private Image ca_picture = null; public CA_Canvas(int k, int r, long nr,int type1) { CA = new TotalisticCA(k, r, nr,type1); type=type1; coloring = get_colors(k); } public void reinit(int k, int r, long nr,int type1) { CA = new TotalisticCA(k, r, nr,type1); type=type1; coloring = get_colors(k); } private Color[] get_colors(int n) { Color[] result = new Color[n]; float hue; int i; result[0] = Color.white; result[1] = Color.black; if (n > 2) { for (i = 2; i < n; i++) { hue = (i-2.0F)/(n-1.0F); result[i] = Color.getHSBColor(hue,1.0F,1.0F); } } return result; } public void run() { Graphics g; Dimension d = size(); int[] ca_line; width = d.width; number= (int)(width/gridwidth); height = d.height; number2=(int)(height/gridwidth); config = CA.config_init(number); ca_picture = createImage(width,height); g = ca_picture.getGraphics(); // Draw the background g.setColor(coloring[0]); g.fillRect(0, 0, width, height); // Draw the initial configuration for (int i = 0; i < number; i++) { g.setColor(coloring[config[i]]); g.fillRect(i*gridwidth, 0, gridwidth, gridwidth); } show_picture(); // Compute and draw rest of the picture ca_line = config; for (int j = 1; j <number2; j++) { ca_line = CA.ca_next(ca_line,type); for (int i = 0; i < number; i++) { g.setColor(coloring[ca_line[i]]); g.fillRect(i*gridwidth, j*gridwidth, gridwidth, gridwidth); } show_picture(); } // Start sliding the picture up try { while (true) { g.copyArea(0,0,width,height,0,-gridwidth); ca_line = CA.ca_next(ca_line,type); for (int i = 0; i < number; i++) { g.setColor(coloring[ca_line[i]]); g.fillRect(i*gridwidth, height-gridwidth, gridwidth, gridwidth); } show_picture(); Thread.sleep(100); } } catch (InterruptedException e) { } } synchronized void show_picture() { Graphics gp = this.getGraphics(); if (gp != null && ca_picture != null) { // Dimension d = size(); // Image picture; // picture = ca_picture.getScaledInstance(d.width,d.height,1); Image picture = ca_picture; gp.drawImage(picture,0,0,this); gp.dispose(); } } public void paint(Graphics g) { show_picture(); }}// Applet code for the visualizationpublic class CCA extends Applet { boolean isStandalone = false; private int default_k = 2; private int default_r = 1; private long default_nr = 18; private int default_type=0; private int k, r,type; private long nr; private boolean layout_ready = false; private Thread drawing_thread = null; private CA_Canvas ca_canvas; Choice states_choice, radius_choice,type_choice; TextField rule_field; public CCA() { } public void init() { if (drawing_thread != null) drawing_thread = null; try { k = Integer.parseInt(getParameter("k")); } catch (NumberFormatException e) { k = default_k; } try { r = Integer.parseInt(getParameter("r")); } catch (NumberFormatException e) { r = default_r; } try { nr = Long.parseLong(getParameter("nr")); } catch (NumberFormatException e) { nr = default_nr; } type=0; if (k < 2) k = default_k; if (r < 1) r = default_r; if (nr < 0) nr = default_nr; if (layout_ready == false) { GridBagLayout grid_layout = new GridBagLayout(); GridBagConstraints constr; setLayout(grid_layout); Panel ca_panel = new Panel(); ca_panel.setLayout(new GridLayout(1,0)); ca_canvas = new CA_Canvas(k, r, nr,type); ca_panel.add("Center",ca_canvas); constr = new GridBagConstraints(); constr.fill = GridBagConstraints.BOTH; constr.insets = new Insets(0, 0, 0, 0); constr.weightx = 1.0; constr.weighty = 1.0; constr.gridwidth = GridBagConstraints.REMAINDER; grid_layout.setConstraints(ca_panel, constr); add(ca_panel); Label states_label = new Label("状态数:"); constr = new GridBagConstraints(); constr.fill = GridBagConstraints.BOTH; constr.insets = new Insets(2, 4, 2, 4); constr.weightx = 0.1; constr.weighty = 0.0; constr.gridx = 1; constr.gridy = 2; grid_layout.setConstraints(states_label, constr); add(states_label); states_choice = new Choice(); states_choice.addItem("2"); states_choice.addItem("3"); states_choice.addItem("4"); states_choice.addItem("5"); states_choice.addItem("6"); states_choice.addItem("7"); states_choice.addItem("8"); states_choice.addItem("9"); states_choice.addItem("10"); states_choice.select(k-2); constr = new GridBagConstraints(); constr.fill = GridBagConstraints.BOTH; constr.insets = new Insets(2, 4, 2, 4); constr.weightx = 0.3; constr.weighty = 0.0; constr.gridx = 2; constr.gridy = 2; grid_layout.setConstraints(states_choice, constr); add(states_choice); Label radius_label = new Label("半径:"); constr = new GridBagConstraints(); constr.fill = GridBagConstraints.BOTH; constr.insets = new Insets(2, 4, 2, 4); constr.weightx = 0.1; constr.weighty = 0.0; constr.gridx = 3; constr.gridy = 2; grid_layout.setConstraints(radius_label, constr); add(radius_label); radius_choice = new Choice(); radius_choice.addItem("1"); radius_choice.addItem("2"); radius_choice.addItem("3"); radius_choice.addItem("4"); radius_choice.addItem("5"); radius_choice.select(r-1); constr = new GridBagConstraints(); constr.fill = GridBagConstraints.BOTH; constr.insets = new Insets(2, 4, 2, 4); constr.weightx = 0.3; constr.weighty = 0.0; constr.gridx = 4; constr.gridy = 2; grid_layout.setConstraints(radius_choice, constr); add(radius_choice); Label type_label = new Label("类型:"); constr = new GridBagConstraints(); constr.fill = GridBagConstraints.BOTH; constr.insets = new Insets(2, 4, 2, 4); constr.weightx = 0.1; constr.weighty = 0.0; constr.gridx = 1; constr.gridy = 3; grid_layout.setConstraints(type_label, constr); add(type_label); type_choice = new Choice(); type_choice.addItem("长编码"); type_choice.addItem("短编码"); constr = new GridBagConstraints(); constr.fill = GridBagConstraints.BOTH; constr.insets = new Insets(2, 4, 2, 4); constr.weightx = 0.3; constr.weighty = 0.0; constr.gridx = 2; constr.gridy = 3; grid_layout.setConstraints(type_choice, constr); add(type_choice); Label rule_label = new Label("编码:"); constr = new GridBagConstraints(); constr.fill = GridBagConstraints.BOTH; constr.insets = new Insets(2, 4, 2, 4); constr.weightx = 0.1; constr.weighty = 0.0; constr.gridx = 5; constr.gridy = 2; grid_layout.setConstraints(rule_label, constr); add(rule_label); rule_field = new TextField(""+nr, 6); constr = new GridBagConstraints(); constr.fill = GridBagConstraints.BOTH; constr.insets = new Insets(2, 4, 2, 4); constr.weightx = 0.3; constr.weighty = 0.0; constr.gridx = 6; constr.gridy = 2; constr.gridwidth = GridBagConstraints.REMAINDER; grid_layout.setConstraints(rule_field, constr); add(rule_field); Button stop_button = new Button("Stop"); constr = new GridBagConstraints(); constr.fill = GridBagConstraints.BOTH; constr.insets = new Insets(2, 4, 2, 4); constr.weightx = 0.3; constr.weighty = 0.0; constr.gridx = 3; constr.gridy = 3; constr.gridwidth = 2; grid_layout.setConstraints(stop_button, constr); add(stop_button); Button restart_button = new Button("Restart"); constr = new GridBagConstraints(); constr.fill = GridBagConstraints.BOTH; constr.insets = new Insets(2, 4, 2, 4); constr.weightx = 0.3; constr.weighty = 0.0; constr.gridx = 5; constr.gridy = 3; constr.gridwidth = 2; grid_layout.setConstraints(restart_button, constr); add(restart_button); validate(); layout_ready = true; } drawing_thread = new Thread(ca_canvas); drawing_thread.setPriority(Thread.MIN_PRIORITY); drawing_thread.start(); } public void start() { if (drawing_thread != null) drawing_thread.resume(); } public void stop() { if (drawing_thread != null) drawing_thread.suspend(); } public boolean mouseDown(Event evt, int x, int y) { if (drawing_thread != null) drawing_thread = null; drawing_thread = new Thread(ca_canvas); drawing_thread.setPriority(Thread.MIN_PRIORITY); drawing_thread.start(); return true; } public boolean action(Event evt, Object arg) { if (evt.target instanceof Button) { if (arg.equals("Restart")) { if (drawing_thread != null) drawing_thread = null; drawing_thread = new Thread(ca_canvas); drawing_thread.setPriority(Thread.MIN_PRIORITY); drawing_thread.start(); } else if (arg.equals("Stop")) if (drawing_thread != null) { drawing_thread.stop(); drawing_thread = null; } } else if (evt.target == states_choice) { int new_k; new_k = Integer.parseInt(states_choice.getSelectedItem()); if (new_k != k) { if (drawing_thread != null) { drawing_thread.stop(); drawing_thread = null; } k = new_k; ca_canvas.reinit(k, r, nr,type); drawing_thread = new Thread(ca_canvas); drawing_thread.setPriority(Thread.MIN_PRIORITY); drawing_thread.start(); } } else if (evt.target == radius_choice) { int new_r; new_r = Integer.parseInt(radius_choice.getSelectedItem()); if (new_r != r) { if (drawing_thread != null) { drawing_thread.stop(); drawing_thread = null; } r = new_r; ca_canvas.reinit(k, r, nr,type); drawing_thread = new Thread(ca_canvas); drawing_thread.setPriority(Thread.MIN_PRIORITY); drawing_thread.start(); } } else if (evt.target ==type_choice) { int new_type; new_type = type_choice.getSelectedIndex(); if (new_type != type) { if (drawing_thread != null) { drawing_thread.stop(); drawing_thread = null; } type = new_type; ca_canvas.reinit(k, r, nr,type); drawing_thread = new Thread(ca_canvas); drawing_thread.setPriority(Thread.MIN_PRIORITY); drawing_thread.start(); } } else if (evt.target == rule_field) { long new_nr; new_nr = Long.parseLong(rule_field.getText()); if (new_nr != nr) { if (drawing_thread != null) { drawing_thread.stop(); drawing_thread = null; } nr = new_nr; ca_canvas.reinit(k, r, nr,type); drawing_thread = new Thread(ca_canvas); drawing_thread.setPriority(Thread.MIN_PRIORITY); drawing_thread.start(); } } return true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -