Results 1 to 3 of 3
  1. #1
    ixhabbaba is offline Member
    Join Date
    Nov 2007
    Posts
    1
    Rep Power
    0

    Default 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:

    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;
            }
        }
    
    }
    Model keeps track of individual particles, and makes them move.
    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 10:26 AM. Reason: Code placed inside [code] block.

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    PS: The forum screws up my indentation for some reason. Sorry about that.
    If you place your codes inside [code] tag, your indentation wont screw up..

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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.
    Java Code:
    v.setModel(m);
    manic.setModel(m);
    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:
    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;
        }
    }
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •