View Single Post
  #2 (permalink)  
Old 04-30-2008, 10:12 PM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
Code:
// <applet code="MFApplet" width="500" height="500"></applet> // run this at the prompt with: >appletviewer MFApplet.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.Timer; import javax.swing.border.BevelBorder; public class MFApplet extends JApplet implements ActionListener{ public JPanel pane; CA_1D2S3N ca=new CA_1D2S3N (50); int timeStep=101; public JMenuBar menuBar; public JToolBar toolBar; JButton Tstart; JButton Tstop; public static Timer timer=new Timer(100,null); public void timerEvent(ActionEvent e){ ca.TimeStep(); ShowCurrentRow(ca.getcurrentRow(),++timeStep); if (timeStep>100) timer.stop(); } // public MFApplet() { // this is a constructor for an app public void init() { // applet construction code goes here menuBar = new JMenuBar(); JMenu formatMenu = new JMenu("Timer"); formatMenu.add("Start"); formatMenu.add("Stop"); menuBar.add(formatMenu); Tstart=new JButton("Start"); Tstop=new JButton("Stop"); toolBar = new JToolBar("Formatting"); toolBar.add("Start",Tstart); Tstart.addActionListener(this); formatMenu.addActionListener(this); timer.addActionListener(new Main_Frame_Timer_ActionAdapter(this)); toolBar.addSeparator(); pane = new JPanel(); pane.setPreferredSize(new Dimension(500, 500)); pane.setBorder(new BevelBorder(BevelBorder.LOWERED)); Dimension size = toolBar.getSize(); Dimension prefSize = toolBar.getPreferredSize(); System.out.printf("toolBar size = [%2d, %2d]%n" + " prefSize = [%2d, %2d]%n", size.width, size.height, prefSize.width, prefSize.height); toolBar.setMaximumSize(toolBar.getSize()); setJMenuBar(menuBar); getContentPane().add(toolBar, BorderLayout.NORTH); getContentPane().add(pane, BorderLayout.CENTER); } public void start() { // This method is called everytime an applet is restored. // To run this applet as an application move the code that // was here off to a main method. } public void stop() {} public void destroy() {} public void clear(){ Graphics g=pane.getGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, pane.getWidth(), pane.getHeight()); } public void ShowCurrentRow(byte[] row,int time){ Graphics g=pane.getGraphics(); for(int i=0;i<row.length;++i){ if(row[i]>0) g.fillRect(3*(10+i), 3*(time+5), 3, 3); } } public void actionPerformed(ActionEvent e) { if(e.getSource()==Tstart){ if(timer.isRunning()) timer.stop(); else{ if(timeStep>100){ ca.reset(); timeStep=0; clear(); ShowCurrentRow(ca.getcurrentRow(),timeStep); } timer.start(); } } } /** Convenience main to run applet as an aplication. */ public static void main(String[] args) { MFApplet applet = new MFApplet(); Dimension d = applet.getPreferredSize(); System.out.printf("applet prefSize = [%d, %d]%n", d.width, d.height); applet.setPreferredSize(new Dimension(500, 500)); JFrame frame = new JFrame("CA Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(applet); frame.pack(); applet.init(); frame.setVisible(true); } } class Main_Frame_Timer_ActionAdapter implements ActionListener{ MFApplet adaptee; public Main_Frame_Timer_ActionAdapter(MFApplet ad){ this.adaptee=ad; } public void actionPerformed(ActionEvent e) { // TODO add timer event adaptee.timerEvent(e); } } class CA_1D2S3N { private byte[][]rows; private int curRow=0,nextRow=1; private int rowSize=50; private int cellStates=2; private byte[] rules={0,1,1,1,1,0,0,0}; public CA_1D2S3N(){} public CA_1D2S3N(int rSize){ rowSize=rSize; rows=new byte[2][]; rows[curRow] =new byte[rowSize]; rows[nextRow] =new byte[rowSize]; reset(); } public int getRowSize(){ return rowSize; } public void reset(){ for(int i=curRow;i<=nextRow;++i){ for(int j=0;j<rowSize;j++){ rows[i][j]=0; } } rows[curRow][(int)(rowSize/2)]=1; } public void TimeStep(){ for(int i=1;i<rowSize-2;++i){ int state=Map(rows[curRow],i); rows[1][i]=rules[state]; } for(int i=0;i<rowSize;i++){ rows[0][i]=rows[1][i]; } } int Map(byte []row,int i){ int x=4*row[i-1] + 2*row[i] + 1*row[i+1]; return x; } byte[] getcurrentRow(){ return rows[curRow].clone(); } }
Note what happens to your graphics after the applet is minimized/restored or partially un/covered. This happens when you draw inside event code. To avoid
this do your drawing inside the JPanel "pane"s paintComponent method.
In java pseudocode:
Code:
... public void init() { ... pane = new Pseudo(); ... class Pseudo extends JPanel { // Use booleans or other member variables to control // the state of this component. Set up the drawing // code inside paintComponent so it can/will // draw the current state of this compoinent at any time. /** override this method for custom drawing */ protected void paintComponent(Graphics g) { super.paintComponent(g); // do custom drawing here. } }
Then you alter/control/set the state/member variables in this class from your applet event code and tell it to repaint.
Reply With Quote