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

    Default

    You still have not answered this question:
    What is the data type for the second argument for the Timer class constructor?
    I asked you to copy and paste here the contents of the documentation for that constructor.
    I see a link to the whole of the API doc.
    Please go to the API doc and copy and paste the API doc here for the constructor you are using.

    Its important that you know how to find and read the API doc for classes and their methods.

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

    Default

    I solved the problem by adding
    Java Code:
    implements java.awt.event.ActionListener
    now it compiles but doesn't work.... I think that the problem is in the IPApplet constructor
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.Timer;
    
    public class IPApplet implements  java.awt.event.ActionListener {
    
        public static void main(String[] args) {
    
    	java.awt.GridBagConstraints gridBagConstraints;
    
    	IPModel model = new IPModel();
    	IPView view = new IPView();
        JFrame frame = new JFrame();
    
    	frame.getContentPane().setLayout(new java.awt.GridBagLayout());
    
    	gridBagConstraints = new java.awt.GridBagConstraints();
    	gridBagConstraints.gridx = 0;
    	gridBagConstraints.gridy = 0;
    	gridBagConstraints.ipadx = 268;
    	gridBagConstraints.ipady = 128;
    	gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
    
        frame.getContentPane().add(view, gridBagConstraints);
    
        view.setModel(model);
    
        IPApplet applet = new IPApplet();
    
    	System.out.println("ciao");
    	}
    
    	public IPApplet() {
    			//Set up the timer that will perform the animation.
    			javax.swing.Timer timer = new javax.swing.Timer(timeStep, this);
    			timer.setCoalesce(false);
        timer.start();
    }
    
    /**
         * Called by timer to update the model and view.
         */
        public void actionPerformed(java.awt.event.ActionEvent e) {
            model.update(timeStep/1000.0);
            view.modelStateChanged();
        }
    
    
        // start the applet
        public void start() {
            timer.restart();
        }
    
        // stop the applet
        public void stop() {
            timer.stop();
        }
    
        // Variables declaration - do not modify//GEN-BEGIN:variables
        private IPView view;
        // End of variables declaration//GEN-END:variables
    
        private IPModel model;
    
        private javax.swing.Timer timer;
    
        /**
         * number of milliseconds per animation frame
         */
        private static int timeStep = 100;
    
    }

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

    Default

    Quote Originally Posted by Norm View Post
    You still have not answered this question:
    What is the data type for the second argument for the Timer class constructor?
    I asked you to copy and paste here the contents of the documentation for that constructor.
    I see a link to the whole of the API doc.
    Please go to the API doc and copy and paste the API doc here for the constructor you are using.

    Its important that you know how to find and read the API doc for classes and their methods.
    oh... I'm sorry, I was on the timer doc but I didn't noticed that the URL was the same for all the APIs..

    but I think that now with the 'implements' is right, now I have to understand where to put the constructor...

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

    Default

    but doesn't work
    Can you explain what happens?
    Add some printlns to show where the execution goes.

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

    Default

    it's like an infinite loop:
    [spoiler]
    Java Code:
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
    ad.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at IPApplet.actionPerformed(IPApplet.java:43)
            at javax.swing.Timer.fireActionPerformed(Timer.java:291)
            at javax.swing.Timer$DoPostEvent.run(Timer.java:221)
            at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
            at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
            at java.awt.EventQueue.access$000(EventQueue.java:84)
            at java.awt.EventQueue$1.run(EventQueue.java:602)
            at java.awt.EventQueue$1.run(EventQueue.java:600)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessCo
    ntrolContext.java:87)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
    ad.java:269)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
    java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
    ad.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at IPApplet.actionPerformed(IPApplet.java:43)
            at javax.swing.Timer.fireActionPerformed(Timer.java:291)
            at javax.swing.Timer$DoPostEvent.run(Timer.java:221)
            at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
            at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
            at java.awt.EventQueue.access$000(EventQueue.java:84)
            at java.awt.EventQueue$1.run(EventQueue.java:602)
            at java.awt.EventQueue$1.run(EventQueue.java:600)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessCo
    ntrolContext.java:87)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
    ad.java:269)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
    java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
    ad.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at IPApplet.actionPerformed(IPApplet.java:43)
            at javax.swing.Timer.fireActionPerformed(Timer.java:291)
            at javax.swing.Timer$DoPostEvent.run(Timer.java:221)
            at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
            at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
            at java.awt.EventQueue.access$000(EventQueue.java:84)
            at java.awt.EventQueue$1.run(EventQueue.java:602)
            at java.awt.EventQueue$1.run(EventQueue.java:600)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessCo
    ntrolContext.java:87)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
    ad.java:269)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
    java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
    ad.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at IPApplet.actionPerformed(IPApplet.java:43)
            at javax.swing.Timer.fireActionPerformed(Timer.java:291)
            at javax.swing.Timer$DoPostEvent.run(Timer.java:221)
            at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
            at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
            at java.awt.EventQueue.access$000(EventQueue.java:84)
            at java.awt.EventQueue$1.run(EventQueue.java:602)
            at java.awt.EventQueue$1.run(EventQueue.java:600)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessCo
    ntrolContext.java:87)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
    ad.java:269)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
    java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
    ad.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    [/spoiler]

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

    Default

    java.lang.NullPointerException
    at IPApplet.actionPerformed(IPApplet.java:43)
    There is a null variable referenced on line 43. Check to see what variable is null and then back track to see why it does not have a value. Check that you don't have "shadowed" variables: variables defined at two different levels with the inner one hiding the outer one.

    The repeating error messages is from your timer continuing to call the actionPerformed method.

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

    Default

    OK.. now I'm proceeding step by step.
    For now I'm tryng to make a class that update the system at a certain frequency
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    import javax.swing.Timer;
    
    public class TimerSample {
    
      private int timeStep = 100;
    
      public static void main(String args[]) {
        new JFrame().setVisible(true);
    
        IPModel model = new IPModel();
    
        ActionListener actionListener = new ActionListener() {
    		 public void actionPerformed(ActionEvent actionEvent) {
    		          System.out.println("Hello World Timer");
    		          //model.update(timeStep/1000.0);
    		          //model.toString();
            }
    	};
        Timer timer = new Timer(500, actionListener);
        timer.start();
      }
    
    }
    if I call the methods of IPModel inside the actionPerformed it gives me those errors:
    Java Code:
    TimerSample.java:19: non-static variable timeStep cannot be referenced from a st
    atic context
                              model.update(timeStep/1000.0);
                                           ^
    TimerSample.java:19: local variable model is accessed from within inner class; n
    eeds to be declared final
                              model.update(timeStep/1000.0);
                              ^
    TimerSample.java:20: local variable model is accessed from within inner class; n
    eeds to be declared final
                              model.toString();
                              ^
    3 errors
    How can I have the actionPerformed to execute those methods?
    Last edited by cas; 07-14-2011 at 11:16 AM.

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

    Default

    Create a constructor for the TimerSample class and move the code that uses the class variable: timeStep into the constructor.
    local variable model ... needs to be declared final
    Add the keyword final to the definition of the variable model.

  9. #29
    cas
    cas is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default

    Now it compiles
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.Timer;
    
    public class TimerSample2 implements ActionListener {
      Timer timer;
      IPModel model;
    
      public TimerSample2() {
        IPModel model = new IPModel();
    
        timer = new Timer(100, this);
        timer.start();
      }
    
      public static void main(String[] args) {
        new TimerSample2();
      }
    
      public void actionPerformed(ActionEvent e) {
        model.update(100/1000.0);
        System.out.println(model);
    
      }
    }
    but when executing it seems to do nothing..
    Java Code:
    C:\java>java TimerSample2
    
    C:\java>
    edit: putting
    System.out.println(model);

    outside the actionPerformed and into the main(), it prints the initial state of the model, so I suppose the the class is accessed succesfully and the problem is inside the actionPerformed method...
    Last edited by cas; 07-14-2011 at 02:22 PM.

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

    Default

    I think it is because the main() method is exiting back to the JVM and that ends the program. Try adding this statement just after the timer.start():

    try{Thread.sleep(1000);}catch(Exception x){} // Let thread get started before exiting

  11. #31
    cas
    cas is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default

    this time while running gives these messages:
    Java Code:
    C:\java>java TimerSample2
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at TimerSample2.actionPerformed(TimerSample2.java:23)
            at javax.swing.Timer.fireActionPerformed(Timer.java:291)
            at javax.swing.Timer$DoPostEvent.run(Timer.java:221)
            at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
            at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
            at java.awt.EventQueue.access$000(EventQueue.java:84)
            at java.awt.EventQueue$1.run(EventQueue.java:602)
            at java.awt.EventQueue$1.run(EventQueue.java:600)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessCo
    ntrolContext.java:87)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
    ad.java:269)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
    java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
    ad.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Commenting the line model.update();
    it works but i prints
    null
    null
    null

    as a result of System.out.println(model);
    just like model isn't instantiated...
    but, as I said before, if I put System.out.println(model); outside the consrtuctor i print the model variables correctly... I don't know why it doesn't work inside the actionPerformed method..

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

    Default

    just like model isn't instantiated...
    Which model variable are you talking about?
    Your program has two!!!
    One is given a value, the other is not.
    Which one is the action listener method seeing?

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

    Default

    just like model isn't instantiated...
    Which model variable are you talking about?
    Your program has two!!!
    One is given a value, the other is not.
    Which one is the action listener method seeing?

  14. #34
    cas
    cas is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default

    But I don't wanted to have two model variables... sorry I'm too inexperienced ^^
    I've declared IPModel model on the top of the code to solve these two compile time errors:
    Java Code:
    C:\java>javac TimerSample2.java
    TimerSample2.java:23: cannot find symbol
    symbol  : variable model
    location: class TimerSample2
        model.update(100/1000.0);
        ^
    TimerSample2.java:24: cannot find symbol
    symbol  : variable model
    location: class TimerSample2
        System.out.println(model);
                           ^
    2 errors
    This is probably why the last time it didn't execute, but now how can I solve correctly these two errors?

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

    Default

    The compiler can not find a definition of the variable: model that is in scope where you are trying to use it at lines 23 and 24.

    Did you define model at the class level so all the methods (except main()) can see it?

  16. #36
    cas
    cas is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Did you define model at the class level so all the methods (except main()) can see it?
    No, how can I do that?

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

    Default

    Your code in post#29 does define the variables: model and timer at the class level.
    The problem with that code was that you also defined a variable: model in the constructor.

    There is a difference between defining a variable and giving it a value:
    <data type> <variable> = ... // define a variable and give it a value
    <variable> = ... // give variable a value

  18. #38
    cas
    cas is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default

    I thought that defining: IP Model model; at the class level defines the variable and then with: IPModel model = new IPModel(); assigns a value to it.
    I really don't get what I've to do..

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

    Default

    Java Code:
     IPModel model =
    This statement defines a new variable. I showed examples of it at the end of post #39

  20. #40
    cas
    cas is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Java Code:
     IPModel model =
    This statement defines a new variable. I showed examples of it at the end of post #39
    Oh.. you're right, I put just model = new IPModel();
    and it works!!

    Just one question, why it doesn't execute without:
    try{Thread.sleep(1000);}
    catch(Exception x){}
    ?

Page 2 of 3 FirstFirst 123 LastLast

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
  •