Results 1 to 3 of 3
Thread: Simulating Brownian Motion
- 11-09-2007, 08:25 AM #1
Member
- Join Date
- Nov 2007
- Posts
- 1
- Rep Power
- 0
Simulating Brownian Motion
Hi! I'm making an applet to simulate Brownian motion, which works just fine.
However, I have this problem with adding an extra feature. The main class
looks like this:
Model keeps track of individual particles, and makes them move.Java Code:public class BrownAnimation extends Applet implements MouseListener{ boolean clicked=false; int xborder=400; int yborder=400; int x; int y; Model m; View v; Simulation sim; Manipulation manic; public void init(){ addMouseListener(this); /* m=new Model(200,200,xborder,yborder); v=new View(m); sim=new Simulation(m,v); manic=new Manipulation(m); add(v); add(manic); */ } public void mouseClicked(MouseEvent e) { if(clicked==false) { x=e.getX(); y=e.getY(); m=new Model(x,y,xborder,yborder); v=new View(m); sim=new Simulation(m,v); manic=new Manipulation(m); add(v); add(manic); clicked=true; } } }
View draws up the graphical components.
Manipulation is a set of buttons to manipulate particle velocity.
Simulation clocks it all.
The Model constructor takes four parameters: the coordinates for the particle
"ground zero", and the limits of the "beaker". If a particle hits the beaker wall,
it will stop.
This all works just fine using the code segment I've commented away.
However, I tried to add an extra feature with the mouseListenet:
The ability to choose a "ground zero" by clicking. Now, the applet
displays nothing, although it loads, and reacts to clicking
(I let it print out a line to the terminal when clicked).
Could you please help me solve this problem?
This is a school project, and they're not helping me.....
PS: The forum screws up my indentation for some reason. Sorry about that.Last edited by JavaBean; 11-09-2007 at 09:26 AM. Reason: Code placed inside [code] block.
- 11-09-2007, 09:27 AM #2
If you place your codes inside [code] tag, your indentation wont screw up..PS: The forum screws up my indentation for some reason. Sorry about that.
- 11-11-2007, 08:53 PM #3
The default layout manager for Applet is FlowLayout - this from it being an extension of the Panel class.
System.out.println(new Applet().getLayout().getClass().getName());
When you add components to a FlowLayout it may not be able to show them at their preferred size (which is what it tries to do). So your added components may be offscreen or collapsed.
What to do?
An elegant solution is to change the model in your View and Manipulation class instances so you don't have to swap/change components.
If this is not possible/desirable then try removing the components from the applet, adding the new ones and validating the layout. Calling validate on a Container tells it we have changed something in one or more of its children that will affect the layout and it needs to do a new layout for proper display. In the JComponent (Swing vs. AWT) class we have a similar method revalidate.Java Code:v.setModel(m); manic.setModel(m);
Another option is to set the layout for the applet to BorderLayout. You could add your view to the center section and your button component to the south section using the proper constraints (see BorderLayout api). And you would still call removeAll and validate in the event code.Java Code:public void mouseClicked(MouseEvent e) { if(clicked==false) { x=e.getX(); y=e.getY(); m=new Model(x,y,xborder,yborder); v=new View(m); sim=new Simulation(m,v); manic=new Manipulation(m); removeAll(); add(v); add(manic); validate(); clicked=true; } }


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks