Running a swing application

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #31
    Originally posted by Polar
    The images folder i had stored it under the testing folder but is not working.. how should i go about it?

    error message:

    Directory of C:\testing

    06/16/2007 03:31 PM <DIR> .
    06/16/2007 03:31 PM <DIR> ..
    06/16/2007 03:19 PM <DIR> components
    06/16/2007 10:05 AM 211 Slider.htm
    06/16/2007 10:01 AM 7,100 SliderDemo2.jav a
    04/27/2007 11:35 AM 4,955 T0.gif
    04/27/2007 11:35 AM 4,747 T1.gif
    04/27/2007 11:35 AM 4,660 T10.gif
    04/27/2007 11:35 AM 4,775 T11.gif
    04/27/2007 11:35 AM 3,917 T12.gif
    04/27/2007 11:35 AM 5,000 T13.gif
    04/27/2007 11:35 AM 4,301 T2.gif
    04/27/2007 11:35 AM 4,672 T3.gif
    04/27/2007 11:35 AM 4,787 T4.gif
    04/27/2007 11:35 AM 3,926 T5.gif
    04/27/2007 11:35 AM 5,015 T6.gif
    04/27/2007 11:35 AM 4,935 T7.gif
    04/27/2007 11:35 AM 4,759 T8.gif
    04/27/2007 11:35 AM 4,305 T9.gif
    16 File(s) 72,065 bytes
    3 Dir(s) 28,550,381,568 bytes free

    C:\testing>
    If you had actually read the source code you had seen that the images are
    expected in a subdirectory 'images'; a subdirectory of subdirectory 'components'
    that is. btw, the entire thing has nothing to do with applets so you can remove
    that .htm file.

    kind regards,

    Jos

    Comment

    • Polar
      New Member
      • Jun 2007
      • 26

      #32
      Originally posted by r035198x
      Look at your code and see where you are getting the images from. Like I said it's the path you give in the code that is used to look for the images.
      how should i type in the command prompt if it is under the testing directory?

      Comment

      • Polar
        New Member
        • Jun 2007
        • 26

        #33
        Originally posted by JosAH
        If you had actually read the source code you had seen that the images are
        expected in a subdirectory 'images'; a subdirectory of subdirectory 'components'
        that is. btw, the entire thing has nothing to do with applets so you can remove
        that .htm file.

        kind regards,

        Jos
        so jos, you mean inside the component folder, there is two .class file and image folder is it?

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #34
          Originally posted by Polar
          how should i type in the command prompt if it is under the testing directory?
          No matter how you type it on the command, the path in the source code is the one that's used to locate the images.

          Comment

          • Polar
            New Member
            • Jun 2007
            • 26

            #35
            Originally posted by Polar
            so jos, you mean inside the component folder, there is two .class file and image folder is it?


            Thank you very much!!!!! especially to r035198x and JosAH.. thanks for bearing my lots and lots of question!!!! thanks alot

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #36
              Originally posted by Polar
              Thank you very much!!!!! especially to r035198x and JosAH.. thanks for bearing my lots and lots of question!!!! thanks alot
              Don't forget to read that Java tutorial from the start.

              Edit: And oh, I'll have to rename this thread for Jos and all the other purits ..

              Comment

              • Polar
                New Member
                • Jun 2007
                • 26

                #37
                Originally posted by Polar
                Thank you very much!!!!! especially to r035198x and JosAH.. thanks for bearing my lots and lots of question!!!! thanks alot


                one more question... SliderDemo2 is not an applet? how come one of the member at the other forum told mi it is an applet and he can view it at his browser? Thanks alot friends!!!!!!!! !

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #38
                  Originally posted by Polar
                  one more question... SliderDemo2 is not an applet? how come one of the member at the other forum told mi it is an applet and he can view it at his browser? Thanks alot friends!!!!!!!! !
                  They could have changed it into an applet first (It's possible) or they could have been lying to you.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #39
                    Originally posted by Polar
                    one more question... SliderDemo2 is not an applet? how come one of the member at the other forum told mi it is an applet and he can view it at his browser? Thanks alot friends!!!!!!!! !
                    That's what I have been telling you all the time. If you read the source code
                    (I googled for it and found it) you'd see that your SliderDemo2 class doesn't
                    extende from the (J)Applet class so it can never be a (J)Applet. Maybe that
                    'member from the other forum' changed the demo him/herself, who knows?

                    kind regards,

                    Jos

                    ps. you should read more carefully; really; take it from me; you miss a lot of
                    precious information if you don't.

                    Comment

                    • Polar
                      New Member
                      • Jun 2007
                      • 26

                      #40
                      Originally posted by r035198x
                      They could have changed it into an applet first (It's possible) or they could have been lying to you.

                      I have created a html file myself, other than that i have added in the "import java.applet.App let;" and "public class HelloWorldApple t extends Applet {" into the SliderDemo2.jav a.. Do you know what else should i add ,in other to get SliderDemo2 to work as an applet? thanks friend

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #41
                        Originally posted by Polar
                        I have created a html file myself, other than that i have added in the "import java.applet.App let;" and "public class HelloWorldApple t extends Applet {" into the SliderDemo2.jav a.. Do you know what else should i add ,in other to get SliderDemo2 to work as an applet? thanks friend
                        Read the *JApplet* tutorial first. Don't try to make applets unless you've read a tutorial about them. Forget your slider demo for now and concentrate on reading about applets. After that you can come back to your demo and see if you can play around with it.

                        Comment

                        • Polar
                          New Member
                          • Jun 2007
                          • 26

                          #42
                          Originally posted by r035198x
                          Read the *JApplet* tutorial first. Don't try to make applets unless you've read a tutorial about them. Forget your slider demo for now and concentrate on reading about applets. After that you can come back to your demo and see if you can play around with it.

                          ok thanks.. i will keep that in mind.. actually i have been rushing from application to applet because my school doesnt give us so much time to do so much read up, they want us to read up a little and expect us to do magic..

                          Comment

                          • Polar
                            New Member
                            • Jun 2007
                            • 26

                            #43
                            Originally posted by Polar
                            ok thanks.. i will keep that in mind.. actually i have been rushing from application to applet because my school doesnt give us so much time to do so much read up, they want us to read up a little and expect us to do magic..

                            This is my SliderDemo Applet code:

                            // <applet code="SliderDem o2Applet" width="300" height="300"></applet>
                            import java.awt.*;
                            import java.awt.event. *;
                            import javax.swing.*;
                            import javax.swing.eve nt.*;
                            import java.util.Hasht able;

                            public class SliderDemo2Appl et extends JApplet
                            implements ActionListener,
                            ChangeListener {
                            //Set up animation parameters.
                            static final int FPS_MIN = 0;
                            static final int FPS_MAX = 30;
                            static final int FPS_INIT = 15; //initial frames per second
                            int frameNumber = 0;
                            int NUM_FRAMES = 14;
                            ImageIcon[] images = new ImageIcon[NUM_FRAMES];
                            int delay;
                            Timer timer;
                            boolean frozen = false;

                            //This label uses ImageIcon to show the doggy pictures.
                            JLabel picture;

                            public void init() {
                            delay = 1000 / FPS_INIT;

                            //Create the slider.
                            JSlider framesPerSecond = new JSlider(JSlider .VERTICAL,
                            FPS_MIN, FPS_MAX, FPS_INIT);
                            framesPerSecond .addChangeListe ner(this);
                            framesPerSecond .setMajorTickSp acing(10);
                            framesPerSecond .setPaintTicks( true);

                            //Create the label table.
                            Hashtable<Integ er, JLabel> labelTable =
                            new Hashtable<Integ er, JLabel>();
                            //PENDING: could use images, but we don't have any good ones.
                            labelTable.put( new Integer( 0 ),
                            new JLabel("Stop") );
                            //new JLabel(createIm ageIcon("images/stop.gif")) );
                            labelTable.put( new Integer( FPS_MAX/10 ),
                            new JLabel("Slow") );
                            //new JLabel(createIm ageIcon("images/slow.gif")) );
                            labelTable.put( new Integer( FPS_MAX ),
                            new JLabel("Fast") );
                            //new JLabel(createIm ageIcon("images/fast.gif")) );
                            framesPerSecond .setLabelTable( labelTable);

                            framesPerSecond .setPaintLabels (true);
                            framesPerSecond .setBorder(
                            BorderFactory.c reateEmptyBorde r(0,0,0,10));

                            //Create the label that displays the animation.
                            picture = new JLabel();
                            picture.setHori zontalAlignment (JLabel.CENTER) ;
                            picture.setAlig nmentX(Componen t.CENTER_ALIGNM ENT);
                            picture.setBord er(BorderFactor y.createCompoun dBorder(
                            BorderFactory.c reateLoweredBev elBorder(),
                            BorderFactory.c reateEmptyBorde r(10,10,10,10)) );
                            updatePicture(0 ); //display first frame

                            //Put everything together.
                            Container cp = getContentPane( );
                            cp.setLayout(ne w BorderLayout()) ;
                            cp.add(framesPe rSecond, BorderLayout.LI NE_START);
                            cp.add(picture, BorderLayout.CE NTER);
                            ((JComponent)cp ).setBorder(Bor derFactory.crea teEmptyBorder(1 0,10,10,10));

                            //Set up a timer that calls this object's action handler.
                            timer = new Timer(delay, this);
                            timer.setInitia lDelay(delay * 7); //We pause animation twice per cycle
                            //by restarting the timer
                            timer.setCoales ce(true);
                            }

                            public void start() {
                            startAnimation( );
                            }

                            public void stop() {
                            stopAnimation() ;
                            }
                            public void stateChanged(Ch angeEvent e) {
                            JSlider source = (JSlider)e.getS ource();
                            if (!source.getVal ueIsAdjusting() ) {
                            int fps = (int)source.get Value();
                            if (fps == 0) {
                            if (!frozen) stopAnimation() ;
                            } else {
                            delay = 1000 / fps;
                            timer.setDelay( delay);
                            timer.setInitia lDelay(delay * 10);
                            if (frozen) startAnimation( );
                            }
                            }
                            }

                            public void startAnimation( ) {
                            //Start (or restart) animating!
                            timer.start();
                            frozen = false;
                            }

                            public void stopAnimation() {
                            //Stop the animating thread.
                            timer.stop();
                            frozen = true;
                            }

                            //Called when the Timer fires.
                            public void actionPerformed (ActionEvent e) {
                            //Advance the animation frame.
                            if (frameNumber == (NUM_FRAMES - 1)) {
                            frameNumber = 0;
                            } else {
                            frameNumber++;
                            }

                            updatePicture(f rameNumber); //display the next picture

                            if ( frameNumber==(N UM_FRAMES - 1)
                            || frameNumber==(N UM_FRAMES/2 - 1) ) {
                            timer.restart() ;
                            }
                            }

                            /** Update the label to display the image for the current frame. */
                            protected void updatePicture(i nt frameNum) {
                            //Get the image if we haven't already.
                            if (images[frameNumber] == null) {
                            images[frameNumber] = createImageIcon ("images/doggy/T"
                            + frameNumber
                            + ".gif");
                            }

                            //Set the image.
                            if (images[frameNumber] != null) {
                            picture.setIcon (images[frameNumber]);
                            } else { //image not found
                            picture.setText ("image #" + frameNumber + " not found");
                            }
                            }

                            /** Returns an ImageIcon, or null if the path was invalid. */
                            protected static ImageIcon createImageIcon (String path) {
                            java.net.URL imgURL = SliderDemo2Appl et.class.getRes ource(path);
                            if (imgURL != null) {
                            return new ImageIcon(imgUR L);
                            } else {
                            System.err.prin tln("Couldn't find file: " + path);
                            return null;
                            }
                            }

                            private static void createAndShowGU I() {
                            //Create and set up the window.
                            JFrame frame = new JFrame("SliderD emo2Applet");
                            frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);

                            //Create and set up the content pane.
                            SliderDemo2Appl et applet = new SliderDemo2Appl et();
                            applet.init();
                            // JApplet is a top-level container and is opaque
                            frame.setConten tPane(applet);

                            //Display the window.
                            frame.pack();
                            frame.setVisibl e(true);
                            applet.startAni mation();
                            }

                            public static void main(String[] args) {
                            //Schedule a job for the event-dispatching thread:
                            //creating and showing this application's GUI.
                            javax.swing.Swi ngUtilities.inv okeLater(new Runnable() {
                            public void run() {
                            createAndShowGU I();
                            }
                            });
                            }
                            }


                            it showed the same error message regarding my class not found.. i had typed this down" java -classpath . components.Slid erDemo2Applet" but still not working? what should i do?

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #44
                              Originally posted by Polar
                              This is my SliderDemo Applet code:

                              // <applet code="SliderDem o2Applet" width="300" height="300"></applet>
                              import java.awt.*;
                              import java.awt.event. *;
                              import javax.swing.*;
                              import javax.swing.eve nt.*;
                              import java.util.Hasht able;

                              public class SliderDemo2Appl et extends JApplet
                              implements ActionListener,
                              ChangeListener {
                              //Set up animation parameters.
                              static final int FPS_MIN = 0;
                              static final int FPS_MAX = 30;
                              static final int FPS_INIT = 15; //initial frames per second
                              int frameNumber = 0;
                              int NUM_FRAMES = 14;
                              ImageIcon[] images = new ImageIcon[NUM_FRAMES];
                              int delay;
                              Timer timer;
                              boolean frozen = false;

                              //This label uses ImageIcon to show the doggy pictures.
                              JLabel picture;

                              public void init() {
                              delay = 1000 / FPS_INIT;

                              //Create the slider.
                              JSlider framesPerSecond = new JSlider(JSlider .VERTICAL,
                              FPS_MIN, FPS_MAX, FPS_INIT);
                              framesPerSecond .addChangeListe ner(this);
                              framesPerSecond .setMajorTickSp acing(10);
                              framesPerSecond .setPaintTicks( true);

                              //Create the label table.
                              Hashtable<Integ er, JLabel> labelTable =
                              new Hashtable<Integ er, JLabel>();
                              //PENDING: could use images, but we don't have any good ones.
                              labelTable.put( new Integer( 0 ),
                              new JLabel("Stop") );
                              //new JLabel(createIm ageIcon("images/stop.gif")) );
                              labelTable.put( new Integer( FPS_MAX/10 ),
                              new JLabel("Slow") );
                              //new JLabel(createIm ageIcon("images/slow.gif")) );
                              labelTable.put( new Integer( FPS_MAX ),
                              new JLabel("Fast") );
                              //new JLabel(createIm ageIcon("images/fast.gif")) );
                              framesPerSecond .setLabelTable( labelTable);

                              framesPerSecond .setPaintLabels (true);
                              framesPerSecond .setBorder(
                              BorderFactory.c reateEmptyBorde r(0,0,0,10));

                              //Create the label that displays the animation.
                              picture = new JLabel();
                              picture.setHori zontalAlignment (JLabel.CENTER) ;
                              picture.setAlig nmentX(Componen t.CENTER_ALIGNM ENT);
                              picture.setBord er(BorderFactor y.createCompoun dBorder(
                              BorderFactory.c reateLoweredBev elBorder(),
                              BorderFactory.c reateEmptyBorde r(10,10,10,10)) );
                              updatePicture(0 ); //display first frame

                              //Put everything together.
                              Container cp = getContentPane( );
                              cp.setLayout(ne w BorderLayout()) ;
                              cp.add(framesPe rSecond, BorderLayout.LI NE_START);
                              cp.add(picture, BorderLayout.CE NTER);
                              ((JComponent)cp ).setBorder(Bor derFactory.crea teEmptyBorder(1 0,10,10,10));

                              //Set up a timer that calls this object's action handler.
                              timer = new Timer(delay, this);
                              timer.setInitia lDelay(delay * 7); //We pause animation twice per cycle
                              //by restarting the timer
                              timer.setCoales ce(true);
                              }

                              public void start() {
                              startAnimation( );
                              }

                              public void stop() {
                              stopAnimation() ;
                              }
                              public void stateChanged(Ch angeEvent e) {
                              JSlider source = (JSlider)e.getS ource();
                              if (!source.getVal ueIsAdjusting() ) {
                              int fps = (int)source.get Value();
                              if (fps == 0) {
                              if (!frozen) stopAnimation() ;
                              } else {
                              delay = 1000 / fps;
                              timer.setDelay( delay);
                              timer.setInitia lDelay(delay * 10);
                              if (frozen) startAnimation( );
                              }
                              }
                              }

                              public void startAnimation( ) {
                              //Start (or restart) animating!
                              timer.start();
                              frozen = false;
                              }

                              public void stopAnimation() {
                              //Stop the animating thread.
                              timer.stop();
                              frozen = true;
                              }

                              //Called when the Timer fires.
                              public void actionPerformed (ActionEvent e) {
                              //Advance the animation frame.
                              if (frameNumber == (NUM_FRAMES - 1)) {
                              frameNumber = 0;
                              } else {
                              frameNumber++;
                              }

                              updatePicture(f rameNumber); //display the next picture

                              if ( frameNumber==(N UM_FRAMES - 1)
                              || frameNumber==(N UM_FRAMES/2 - 1) ) {
                              timer.restart() ;
                              }
                              }

                              /** Update the label to display the image for the current frame. */
                              protected void updatePicture(i nt frameNum) {
                              //Get the image if we haven't already.
                              if (images[frameNumber] == null) {
                              images[frameNumber] = createImageIcon ("images/doggy/T"
                              + frameNumber
                              + ".gif");
                              }

                              //Set the image.
                              if (images[frameNumber] != null) {
                              picture.setIcon (images[frameNumber]);
                              } else { //image not found
                              picture.setText ("image #" + frameNumber + " not found");
                              }
                              }

                              /** Returns an ImageIcon, or null if the path was invalid. */
                              protected static ImageIcon createImageIcon (String path) {
                              java.net.URL imgURL = SliderDemo2Appl et.class.getRes ource(path);
                              if (imgURL != null) {
                              return new ImageIcon(imgUR L);
                              } else {
                              System.err.prin tln("Couldn't find file: " + path);
                              return null;
                              }
                              }

                              private static void createAndShowGU I() {
                              //Create and set up the window.
                              JFrame frame = new JFrame("SliderD emo2Applet");
                              frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);

                              //Create and set up the content pane.
                              SliderDemo2Appl et applet = new SliderDemo2Appl et();
                              applet.init();
                              // JApplet is a top-level container and is opaque
                              frame.setConten tPane(applet);

                              //Display the window.
                              frame.pack();
                              frame.setVisibl e(true);
                              applet.startAni mation();
                              }

                              public static void main(String[] args) {
                              //Schedule a job for the event-dispatching thread:
                              //creating and showing this application's GUI.
                              javax.swing.Swi ngUtilities.inv okeLater(new Runnable() {
                              public void run() {
                              createAndShowGU I();
                              }
                              });
                              }
                              }


                              it showed the same error message regarding my class not found.. i had typed this down" java -classpath . components.Slid erDemo2Applet" but still not working? what should i do?
                              1.) You forgot the code tags.
                              2.) You haven't read that applet tutorial, have you?

                              Comment

                              • Polar
                                New Member
                                • Jun 2007
                                • 26

                                #45
                                Originally posted by r035198x
                                1.) You forgot the code tags.
                                2.) You haven't read that applet tutorial, have you?

                                not code tag..i didnt copy the whole folder of images.. i just went in to copy the doggys images..Thanks

                                Comment

                                Working...