Page 3 of 3 FirstFirst 123
Results 41 to 47 of 47
  1. #41
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    It does execute, but very quickly and exits before the thread gets going.
    Last edited by Norm; 07-14-2011 at 05:01 PM.

  2. #42
    cas
    cas is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default

    Now I have understood, so this is because the timer creates a thread but the main thread closes without waiting for the timer thread.

    Norm... thank you so much :)

  3. #43
    cas
    cas is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default

    Here I am again ^^
    Now I'm trying to make the visual part
    I have this IPView code:
    Java Code:
    import javax.swing.JPanel;
    import java.awt.Graphics;
    import java.awt.*;
    
    /**
     * IPView provides a view of IPModel
     * @author drew
     */
    public final class IPView extends JPanel {
        
        private IPModel model;
        
        /** Creates new form IPView */
        public IPView() {
            initComponents();
        }
        
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        private void initComponents() {//GEN-BEGIN:initComponents
    
            setLayout(new java.awt.BorderLayout());
    
            setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
        }//GEN-END:initComponents
        
        
        // Variables declaration - do not modify//GEN-BEGIN:variables
        // End of variables declaration//GEN-END:variables
        
        //  http://java.sun.com/docs/books/tutorial/uiswing/14painting/practice.html
        
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;      // in most cases (ours) this is a safe cast
            
            // get the dimensions of the JPanel so that we can scale everything properly
            Insets insets = getInsets();
            int viewWidth = getWidth() - insets.left - insets.right;
            int viewHeight = getHeight() - insets.top - insets.bottom;
            
            // scale the size of the model to the size of the view.
            // the constants are chosen for asthetics, but could be based on say
            // the masses of  the objects.
            // Note that for the view to be physiclly correct, l and x must be
            // correctly proportianal. ( FIXME - better explaination )
            
            int blockWidth = viewWidth/10;
            int blockHeight = viewHeight/10;
            int penMassRadius = blockHeight/2;
            
            double modelX =  0;
            double modelPhi = 0;
            int penLength = blockHeight * 3;
           
            //
            if (model != null) {
                modelX = model.getX();
                modelPhi = model.getPhi();
            }
            
            
            int blockX =  (int)(modelX * penLength + viewWidth/2);
            int blockY =  viewHeight/2;
            int penX =   (int)(blockX + penLength * Math.sin(modelPhi));
            int penY =   (int)(blockY + penLength * Math.cos(modelPhi) - blockHeight);
            
            
            // paint the block
            g2.setColor(Color.blue);
            g2.fill(new Rectangle(blockX - blockWidth/2, blockY - blockHeight ,blockWidth, blockHeight));
            
            
            // paint the pendulum mass
            g2.setColor(Color.green);
            g2.fillOval(penX-penMassRadius, penY-penMassRadius, 2*penMassRadius, 2*penMassRadius);
            
            // paint the rod
            g2.drawLine(blockX,blockY -blockHeight , penX, penY);
        }
        
        /**
         * associate a model with this view
         */    
        public void setModel(IPModel aModel) {
            model = aModel;
        }
        
        /**
         * call when a state change in the model requires an update to the view
         */    
        public void modelStateChanged() {
            this.repaint();
        }
    }
    This code should paint a rectangle and a pendulum above it, with their position according to the updated system state.

    Now I've to edit the previous code, that runs the simulation, to visualize the system.
    Java Code:
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.RenderingHints;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    public class IPApplication extends JPanel implements ActionListener {
      Timer timer;
      IPModel model;
      IPView view;
      int timeStep = 100;
    
      public IPApplication() {
        model = new IPModel();
        view = new IPView();
        view.setModel(model);
    
        timer = new Timer(100, this);
        timer.setCoalesce(false);
        timer.start();
      }
    
      public static void main(String[] args) {
    
        JFrame frame = new JFrame("Pendolo Inverso");
        frame.add(new IPApplication());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new java.awt.GridBagLayout());
        /*frame.gridBagConstraints = new java.awt.GridBagConstraints();
    	frame.gridBagConstraints.gridx = 0;
    	frame.gridBagConstraints.gridy = 0;
    	frame.gridBagConstraints.ipadx = 268;
    	frame.gridBagConstraints.ipady = 128;
    	frame.gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);*/
        frame.getContentPane().add(view, gridBagConstraints);
        //frame.setSize(420, 250);
        //frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    
    
      }
    
      public void actionPerformed(ActionEvent e) {
    
        //System.out.println("Hello World");
        model.update(timeStep/1000.0);
        System.out.println(model);
        view.modelStateChanged();
      }
    }
    Obviously it doesn't compile, but could you at leat tell me if I'm going in the right direction or I'm doing something wrong?

  4. #44
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Compile and execute it. Don't wait for someone to look at your code.

  5. #45
    cas
    cas is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default

    Now it compiles but don't display anything..
    Java Code:
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    //import java.awt.Rectangle;
    import java.awt.RenderingHints;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    import java.awt.*;
    
    public class IPApplication extends JPanel implements ActionListener {
      Timer timer;
      IPModel model;
      IPView view;
      int timeStep = 100;
    
      public IPApplication() {
        model = new IPModel();
        view = new IPView();
        view.setModel(model);
    
        timer = new Timer(timeStep, this);
        timer.setCoalesce(false);
        timer.start();
      }
    
      public static void main(String[] args) {
        JFrame frame = new JFrame("Pendolo Inverso");
    	frame.add(new IPApplication());
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(420, 250);
        frame.setVisible(true);
      }
    
      public void actionPerformed(ActionEvent e) {
    
        //System.out.println("Hello World");
        model.update(timeStep/1000.0);
        System.out.println(model);
        view.modelStateChanged();
      }
    }
    edit: actually it displays a window with nothing inside
    Last edited by cas; 07-14-2011 at 07:38 PM.

  6. #46
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Where in your code do you add anything to the panel that is being displayed?
    What do you expect to be displayed?
    For a test, Add a JLabel with some text to the panel.

  7. #47
    cas
    cas is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default

    Now it works!
    I had to add the IPView instance to the gui.

    This is the final code:
    Java Code:
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    //import java.awt.Rectangle;
    import java.awt.RenderingHints;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    import java.awt.*;
    
    public class IPApplication extends JPanel implements ActionListener {
      Timer timer;
      IPModel model;
      IPView view;
      int timeStep = 100;
    
      public IPApplication() {
    	setLayout(new BorderLayout());
    
        model = new IPModel();
    
        view = new IPView();
        view.setModel(model);
        add(view);
    
        timer = new Timer(timeStep, this);
        timer.setCoalesce(false);
        timer.start();
      }
    
      public static void main(String[] args) {
        JFrame frame = new JFrame("Pendolo Inverso");
    	frame.add(new IPApplication());
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(420, 250);
        frame.setVisible(true);
      }
    
      public void actionPerformed(ActionEvent e) {
    
        //System.out.println("Hello World");
        model.update(timeStep/1000.0);
        System.out.println(model);
        view.modelStateChanged();
      }
    }

Page 3 of 3 FirstFirst 123

Similar Threads

  1. applet working in applet viewer, not browser
    By plasticfood in forum Java Applets
    Replies: 4
    Last Post: 06-24-2011, 06:47 PM
  2. Java Applet to Applet communication
    By jsman in forum Java Applets
    Replies: 3
    Last Post: 04-05-2011, 02:23 PM
  3. Replies: 2
    Last Post: 09-29-2009, 03:33 PM
  4. Calling another applet on click of button in one applet
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 1
    Last Post: 02-19-2009, 12:54 PM
  5. Replies: 4
    Last Post: 06-08-2007, 06:15 AM

Posting Permissions

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