/* * Copyright (C) Andrew Dolgert 1999 under Gnu Public License * http://www.gnu.org/copyleft/gpl.html * * NessApplet.java * This applet shows a toy model of nonequilibrium charges moving on * a torus (wrapping around the edges). The model says that all particles * move equally likely left and right but half slightly prefer to move * up and half slightly prefer to move down. The model is similar to that * of Beate Schmittman and Royce Zia of Virginia Tech Physics, but it does * not include the possibility and up and down particle can exchange charge * (switch places). * * Drew Dolgert September 1999 */ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.*; /* * This class is a container for the canvas that does all the work. * It is called the NessApplet after Non-Equilibrium Steady States. */ public class NessApplet extends Applet implements ActionListener { // There is a kind of Asian slot machine I remember where balls // fell against pegs. That is the name of the class that represents // the board and balls on it. Pacinco board; Button stopButton, resetButton; static final String sResource = "NessStrings"; String sStart, sStop, sReset; public void init() { String sParam; int nBalls, nBoardSizeX,nBoardSizeY; double dWeight, dMobility; ResourceBundle rb; // The resource bundle contains strings in various languages. // See the CollisionStrings*.properties files. String sUseRB = this.getParameter("international"); rb = null; if ((sUseRB == null) || !sUseRB.equalsIgnoreCase("no")) { String sLang = this.getParameter("language"); String sCountry = this.getParameter("country"); try { if (sLang == null) rb = ResourceBundle.getBundle(sResource, Locale.getDefault()); else { Locale thisloc; if (sCountry == null) thisloc = new Locale(sLang,""); else thisloc = new Locale(sLang,sCountry); rb = ResourceBundle.getBundle(sResource,thisloc); } } catch (java.lang.ClassFormatError e) { System.out.println("ClassFormatError: "+e.getMessage()); rb = null; } catch (MissingResourceException e) { System.out.println("Missing Resource: "+e.getMessage()); rb = null; } } if (rb!=null) { try { sStart = rb.getString("start"); sStop = rb.getString("stop"); sReset = rb.getString("reset"); } catch (MissingResourceException e) { System.out.println("Missing Resource: "+e.getMessage()); sStart = "Start"; sStop = "Stop"; sReset = "Reset"; } } else { sStart = "Start"; sStop = "Stop"; sReset = "Reset"; } // Load parameters needed for creating the two main panels. // Start with default colors. sParam = this.getParameter("backgroundcolor"); Color bgColor = this.pickColor(sParam,Color.white); sParam = this.getParameter("upcolor"); Color upColor = this.pickColor(sParam,Color.red); sParam = this.getParameter("downcolor"); Color downColor = this.pickColor(sParam,Color.blue); // Move to x and y size of board. sParam = this.getParameter("boardsizex"); if (sParam != null) { try { nBoardSizeX = Integer.parseInt(sParam); } catch (NumberFormatException e) { nBoardSizeX = 40; System.out.println("boardsize should be an integer size of a side of the board."); } } else nBoardSizeX = 40; sParam = this.getParameter("boardsizey"); if (sParam != null) { try { nBoardSizeY = Integer.parseInt(sParam); } catch (NumberFormatException e) { nBoardSizeY = 40; System.out.println("boardsize should be an integer size of a side of the board."); } } else nBoardSizeY = 40; // How about the total number of balls? sParam = this.getParameter("nballs"); if (sParam != null) { try { nBalls = Integer.parseInt(sParam); } catch (NumberFormatException e) { nBalls = (int)(nBoardSizeX*nBoardSizeY*0.25); System.out.println("nballs should be an integer number of balls on the board."); } } else nBalls = (int)(nBoardSizeX*nBoardSizeY*0.25); // The weight expresses the driving field. It is explained in more detail // in the Pacinco class. sParam = this.getParameter("weight"); if (sParam != null) { try { dWeight = Double.valueOf(sParam).doubleValue(); } catch (NumberFormatException e) { dWeight = 0.4; System.out.println("weight is a number between -1 and +1 which measures driving force."); } } else dWeight = 0.4; // The mobility is the average percentage of balls that get to move each turn. sParam = this.getParameter("mobility"); if (sParam != null) { try { dMobility = Double.valueOf(sParam).doubleValue(); } catch (NumberFormatException e) { dMobility = 0.6; System.out.println("mobility is the percentage of balls that move each step."); } } else dMobility = 0.6; // Now create the board. this.setLayout(new BorderLayout()); Panel p1 = new Panel(new GridLayout(1,2)); board = new Pacinco(nBoardSizeX,nBoardSizeY,nBalls); // ave Velocity, Mass Ratio board.setBGColor(bgColor); board.setUpColor(upColor); board.setDownColor(downColor); board.setWeight(dWeight); board.setMobility(dMobility); // Threadsleep is how long the routine should pause between iterations. sParam = this.getParameter("threadsleep"); if (sParam != null) { try { int threadsleep = Integer.parseInt(sParam); board.setThreadSleep(threadsleep); } catch (NumberFormatException e) { System.out.println("Threadsleep should be an integer number of milliseconds to sleep."); } } p1.add(board); this.add(p1,"Center"); Panel p2 = new Panel(); stopButton = new Button(sStart); stopButton.setActionCommand("start"); stopButton.addActionListener(this); p2.add(stopButton); resetButton = new Button(sReset); resetButton.setActionCommand("reset"); resetButton.addActionListener(this); p2.add(resetButton); this.add(p2,"South"); } // When someone clicks on anything in the applet, // this method gets the call and notifies proper authorities. public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("reset")) { board.reset(); } else if (e.getActionCommand().equals("stop")) { stopButton.setLabel(sStart); stopButton.setActionCommand("start"); stopButton.getParent().invalidate(); stopButton.getParent().validate(); board.ustop(); // Tell the board the user requested a stop. } else if (e.getActionCommand().equals("start")) { stopButton.setLabel(sStop); stopButton.setActionCommand("stop"); stopButton.getParent().invalidate(); stopButton.getParent().validate(); board.ustart(); // Tell the board the user requested a start. } } public String getAppletInfo() { return "NessApplet v. 1.0. Written by Drew Dolgert.\n"+ "Copyright (C) Andrew Dolgert ajd2m@physics.virginia.edu 1999\n"+ "Under Gnu Public License http://www.gnu.org/copyleft/gpl.html"; } static final String[][] parameterInfo = { {"international","yes or no [yes]", "Setting this to \"no\" turns off internationalization "+ "in case your browser cannot handle it."}, {"language","two letter language code", "EN for English, FR for French, etc."}, {"country","two letter country code", "CN for China, IT for Italy. Not required."}, {"boardsizex","integer width of board [40]","20 is small. 200 is a decent size."}, {"boardsizey","integer height of board [40]","20 is small. 200 is a decent size."}, {"nballs","integer [400]","Number of balls in grid."}, {"weight","double between -1 and 1 [0.4]","0 is no applied current. 1 (or -1) is maximum."}, {"mobility","double between 0 and 1 [0.6]","Percentage of balls that move each turn."}, {"upcolor","string color","black,blue,cyan,darkGray,gray,green,lightGray,magenta,orange,pink,red,white,yellow"}, {"downcolor","string color","black,blue,cyan,darkGray,gray,green,lightGray,magenta,orange,pink,red,white,yellow"}, {"backgroundcolor","string color","black,blue,cyan,darkGray,gray,green,lightGray,magenta,orange,pink,red,white,yellow"}, {"threadsleep","integer [20]","Number of milliseconds the gas thread should sleep."}, }; public String[][] getParameterInfo() { return parameterInfo; } public void astart() { board.astart(); } public void stop() { board.astop(); } private Color pickColor(String sParam, Color defaultColor) { if (sParam == null) return defaultColor; if (sParam.equalsIgnoreCase("black")) return Color.black; if (sParam.equalsIgnoreCase("blue")) return Color.blue; if (sParam.equalsIgnoreCase("cyan")) return Color.cyan; if (sParam.equalsIgnoreCase("darkGray")) return Color.darkGray; if (sParam.equalsIgnoreCase("gray")) return Color.gray; if (sParam.equalsIgnoreCase("green")) return Color.green; if (sParam.equalsIgnoreCase("lightGray")) return Color.lightGray; if (sParam.equalsIgnoreCase("magenta")) return Color.magenta; if (sParam.equalsIgnoreCase("orange")) return Color.orange; if (sParam.equalsIgnoreCase("pink")) return Color.pink; if (sParam.equalsIgnoreCase("red")) return Color.red; if (sParam.equalsIgnoreCase("yellow")) return Color.yellow; if (sParam.equalsIgnoreCase("white")) return Color.white; return defaultColor; } public static void main(String args[]) { Applet applet = new NessApplet(); Frame f = new Frame("NESS Applet"); f.add("Center",applet); f.setSize(500,300); f.setBackground(Color.lightGray); f.show(); applet.init(); applet.start(); } }