Page 1 of 3 123 LastLast
Results 1 to 20 of 47
  1. #1
    cas
    cas is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default Help with this applet

    Hi guys,
    I was try to compile the following code:
    Java Code:
    public class IPApplet extends javax.swing.JApplet
    implements  java.awt.event.ActionListener {
    
        /** Initializes the applet IPApplet */
        public void main() {
            initComponents();
    
            //create the model and associate it with the view
            model = new IPModel();
            //view.setModel(model);
    
            //Set up the timer that will perform the animation.
            timer = new javax.swing.Timer(timeStep, this);
            timer.setCoalesce(false);
            timer.start(); //Start the simulation.
        }
    
        /** This method is called from within the init() method 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
            java.awt.GridBagConstraints gridBagConstraints;
    
            view = new IPView();
    
            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);
            getContentPane().add(view, gridBagConstraints);
    
        }//GEN-END:initComponents
    
        /**
         * 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 int timeStep = 100;
    
    }
    It compile with no errors but when I launch the app it gives me this message:
    Exception in thread "main" java.lang.NoSuchMethodError: main

    I read that this problem is caused by the public void main(), it should be pubblic static void main(), but with the static main the compiler gives a lot of errors about non static variables that can't be referenced from a static context...
    I don't really know how to get this thing to work, please give me a hand.
    Last edited by JosAH; 07-10-2011 at 10:34 PM. Reason: fixed the [code] ... [/code] tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    You need to read about how to create and execute applets.
    Go to this site: The Really Big Index
    and Search for Applets for a good tutorial about applets.

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

    Default

    Quote Originally Posted by Norm View Post
    You need to read about how to create and execute applets.
    Go to this site: The Really Big Index
    and Search for Applets for a good tutorial about applets.
    Could be possible to modify that code and turn it in a non-applet program that can run on JVM?

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

    Default

    If it can help, I would like to write a program which simulate a cart pole system, just as explained in this site:
    drewk.net:Inverted Pendulum

    It provides all the source codes, but I don't want it to run as an applet on the browser but I want it to run as a normal java program.

    The code provided by the site is formed by three files:
    IPApplet.java which is the one I've posted at the top of the thread and it is the part of the program that launch the simulation,
    IPModel.java that simulate the dinamic of the sistem (this one works on JVM),
    IPView.java that prints the evolution of the system at each time step.

    IPModel once compiled works fine so I have only to edit the other two scripts so the whole program runs as a normal java program, non applet.

    edit: if this is not the right section could some moderator move this thread in the appropriate section?
    Last edited by cas; 07-11-2011 at 01:39 PM.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default

    Could be possible to modify that code and turn it in a non-applet program that can run on JVM?
    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    The conversion is trivial. All Swing top level windows (JFrame, JApplet, JDialog ...) implement RootPaneContainer and have a method getContentPane(). Leverage that.

    Here's another way, not one which I would recommend for a beginner. Download the jar from http://www.drewk.net/projects/ipendulum/ipapp.jar and run this after changing the file path to the location of the jar on your machine.
    Java Code:
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLClassLoader;
    import javax.swing.JApplet;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class Appletication {
    
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
    
          public void run() {
            new Appletication().makeUI();
          }
        });
      }
    
      public void makeUI() {
        try {
          URL[] urls = {new URL("file:///E:/Java/examples/ipapp.jar")};
          URLClassLoader loader = new URLClassLoader(urls);
          Class<?> clazz = loader.loadClass("IPApplet");
          JApplet applet = (JApplet) clazz.newInstance();
          applet.init();
    
          JFrame frame = new JFrame();
          frame.setContentPane(applet.getContentPane());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
        } catch (MalformedURLException ex) {
          ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
          ex.printStackTrace();
        } catch (InstantiationException ex) {
          ex.printStackTrace();
        } catch (IllegalAccessException ex) {
          ex.printStackTrace();
        }
      }
    }
    db

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

    Default

    Thank you very much DarrylBurke, but I would prefer to create a new script that uses directly the IPModel instead of using the code you've posted.

    For now I'm reading the tutorial, but since I've no experience in programming I'm encountering a lot of difficulties, if you could gently write a draft of the code it would be very useful to me.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default

    Like I said, that's trivial. Examine the applet code to see what's set and added to the component returned by getContentPane() and duplicate that for a JFrame.

    You'll probably also want to setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE). And in your first post, it looks like you've renamed init() to main(). The entry point of an application has the signature public static void main(String[] args) [or String... args)]. See my code for an example of what your main(...) method should do.

    db

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    To convert an applet to a application, you need to create a GUI platform like a JFrame to display the applet components in. Applet extends Panel so it is almost ready to convert just by changing it to extend JPanel and adding it to the JFrame.
    The code in the init() and start() methods should be moved into the constructor for the new class.

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

    Default

    Ok, thank you all,
    I'll try to follow your advises

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

    Default

    I've done in this way:
    Java Code:
    import javax.swing.JFrame;
    
    public class IPApplet {
    
        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);
    
    	//Set up the timer that will perform the animation.
    	timer = new javax.swing.Timer(timeStep, this);
    	timer.setCoalesce(false);
        timer.start();
    
    	System.out.println("ciao");
    	}
    
    /**
         * 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;
    
    }
    Inside the main method if I delete the last 3 raw (the ones with the timer) it compiles, but I don't know if it's correct, but with the timer it gives me an error on compile time.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    gives me an error
    Please copy and paste the full text of the error message here.

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

    Default

    Java Code:
    C:\java>javac IPApplet.java
    IPApplet.java:27: non-static variable this cannot be referenced from a static co
    ntext
            javax.swing.Timer timer = new javax.swing.Timer(timeStep, this);
                                                                      ^
    IPApplet.java:27: cannot find symbol
    symbol  : constructor Timer(int,IPApplet)
    location: class javax.swing.Timer
            javax.swing.Timer timer = new javax.swing.Timer(timeStep, this);
                                      ^
    2 errors

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Move the code out of the main() method into the class's constructor. There is no instance of an object (this refers to the current instance) in the main method.

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

    Default

    you mean the entire code or just the last 3 lines?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    The lines that use "this"

    I just noticed that you never create an instance of the IPApplet class.
    Last edited by Norm; 07-13-2011 at 06:30 PM. Reason: Comments on IPApplet class

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

    Default

    Java Code:
    import javax.swing.JFrame;
    
    public class IPApplet {
    
        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);
    
    	System.out.println("ciao");
    	}
    
    	public IPApplet() {
    			//Set up the timer that will perform the animation.
    			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;
    
    }
    In this way it gives only this error
    Java Code:
    C:\java>javac IPApplet.java
    IPApplet.java:31: cannot find symbol
    symbol  : constructor Timer(int,IPApplet)
    location: class javax.swing.Timer
                            timer = new javax.swing.Timer(timeStep, this);
                                    ^
    1 error

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Read the API doc for the Timer class constructor.
    What is the data type of the second argument?
    Is IPApplet that type?
    What do you need to do to the class to make it that type?

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

    Default

    Sorry I don't get it... Could you explain?

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    If you will copy and past the API doc for the Timer class constructor here, I will explain.

    Other things you should know:
    Do you know what an argument is?
    Do you know what a data type is?
    Do you know how to make your class have a specific data type?

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

    Default

    yes for the first two questions, no for the third.

    here is the doc for the swing timer API
    Java 2 Platform SE v1.4.2

Page 1 of 3 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, 07:47 PM
  2. Java Applet to Applet communication
    By jsman in forum Java Applets
    Replies: 3
    Last Post: 04-05-2011, 03:23 PM
  3. Replies: 2
    Last Post: 09-29-2009, 04: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, 01:54 PM
  5. Replies: 4
    Last Post: 06-08-2007, 07: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
  •