help on image slider

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fatfatpopo
    New Member
    • Jun 2007
    • 16

    #1

    help on image slider

    Anyone who understand the codes below, can you please kindly explain to me by adding comments.. thanks in advance

    Code:
    // <applet code="ImageSlider" width="400" height="400"></applet>
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class ImageSlider extends JApplet implements ChangeListener {
    BufferedImage[] images;
    JLabel label;
    
    public void init() {
    images = getImages();
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(getLabelComponent());
    getContentPane().add(getSlider(), "Last");
    }
    
    public void stateChanged(ChangeEvent e) {
    JSlider slider = (JSlider)e.getSource();
    int index = slider.getValue() -1;
    label.setIcon(new ImageIcon(images[index]));
    }
    
    private JScrollPane getLabelComponent() {
    label = new JLabel(new ImageIcon(images[0]));
    label.setHorizontalAlignment(JLabel.CENTER);
    return new JScrollPane(label);
    }
    
    private JSlider getSlider() {
    JSlider slider = new JSlider(1, 4, 1);
    slider.setPaintTicks(true);
    slider.setPaintLabels(true);
    slider.setMajorTickSpacing(1);
    slider.setSnapToTicks(true);
    slider.addChangeListener(this);
    return slider;
    }
    
    private BufferedImage[] getImages() {
    BufferedImage[] images = new BufferedImage[4];
    for(int j = 0; j < images.length; j++) {
    try {
    String path = "images/t" + (j+1) + ".gif";
    images[j] = ImageIO.read(new File(path));
    } catch(IOException e) {
    System.out.println("Read error: " + e.getMessage());
    }
    }
    return images;
    }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by fatfatpopo
    Anyone who understand the codes below, can you please kindly explain to me by adding comments.. thanks in advance

    Code:
    // <applet code="ImageSlider" width="400" height="400"></applet>
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class ImageSlider extends JApplet implements ChangeListener {
    BufferedImage[] images;
    JLabel label;
    
    public void init() {
    images = getImages();
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(getLabelComponent());
    getContentPane().add(getSlider(), "Last");
    }
    
    public void stateChanged(ChangeEvent e) {
    JSlider slider = (JSlider)e.getSource();
    int index = slider.getValue() -1;
    label.setIcon(new ImageIcon(images[index]));
    }
    
    private JScrollPane getLabelComponent() {
    label = new JLabel(new ImageIcon(images[0]));
    label.setHorizontalAlignment(JLabel.CENTER);
    return new JScrollPane(label);
    }
    
    private JSlider getSlider() {
    JSlider slider = new JSlider(1, 4, 1);
    slider.setPaintTicks(true);
    slider.setPaintLabels(true);
    slider.setMajorTickSpacing(1);
    slider.setSnapToTicks(true);
    slider.addChangeListener(this);
    return slider;
    }
    
    private BufferedImage[] getImages() {
    BufferedImage[] images = new BufferedImage[4];
    for(int j = 0; j < images.length; j++) {
    try {
    String path = "images/t" + (j+1) + ".gif";
    images[j] = ImageIO.read(new File(path));
    } catch(IOException e) {
    System.out.println("Read error: " + e.getMessage());
    }
    }
    return images;
    }
    }
    Why not read an Applet and a Swing tutorial, then you can even comment it yourself.

    Comment

    • fatfatpopo
      New Member
      • Jun 2007
      • 16

      #3
      Originally posted by r035198x
      Why not read an Applet and a Swing tutorial, then you can even comment it yourself.
      I had read it, but still don't quit understand.So im here to see anyone who can give me any assistance.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by fatfatpopo
        I had read it, but still don't quit understand.So im here to see anyone who can give me any assistance.
        Which part do you not understand then?

        Comment

        • fatfatpopo
          New Member
          • Jun 2007
          • 16

          #5
          Originally posted by r035198x
          Which part do you not understand then?

          Do you have any tutorial guide to recommend for all these codes? Other than javasun.. Thanks

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by fatfatpopo
            Do you have any tutorial guide to recommend for all these codes? Other than javasun.. Thanks
            Sun's swing tutorial is the best one for it and that's the only one I'll recommend.

            Comment

            • fatfatpopo
              New Member
              • Jun 2007
              • 16

              #7
              Originally posted by r035198x
              Which part do you not understand then?
              I don't really understand the code below.. But i know is setting the layout in the applet.

              Code:
              getContentPane().setLayout(new BorderLayout());
              getContentPane().add(getLabelComponent());
              getContentPane().add(getSlider(), "Last");

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by fatfatpopo
                I don't really understand the code below.. But i know is setting the layout in the applet.

                Code:
                getContentPane().setLayout(new BorderLayout());
                getContentPane().add(getLabelComponent());
                getContentPane().add(getSlider(), "Last");
                The first line sets the layout to be BorderLayout, the next two lines add two things first a JScrollPane and then a JSlider. Both of these are discussed in the swing tutorial link that I gave you. The methods getLabelCompone nt() and getSlider() which reaturn these components are there in the ImageSlider applet that you have.

                Comment

                • fatfatpopo
                  New Member
                  • Jun 2007
                  • 16

                  #9
                  Originally posted by r035198x
                  The first line sets the layout to be BorderLayout, the next two lines add two things first a JScrollPane and then a JSlider. Both of these are discussed in the swing tutorial link that I gave you. The methods getLabelCompone nt() and getSlider() which reaturn these components are there in the ImageSlider applet that you have.

                  I would like to ask regarding "getLabelCompon ent() ", how i know what name should i put after "get"

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by fatfatpopo
                    I would like to ask regarding "getLabelCompon ent() ", how i know what name should i put after "get"
                    It depends on what you've called the method in the class. See my reply #8 again.

                    Comment

                    • fatfatpopo
                      New Member
                      • Jun 2007
                      • 16

                      #11
                      Originally posted by r035198x
                      It depends on what you've called the method in the class. See my reply #8 again.
                      Kindly tell me what is going on in the codes below

                      Code:
                      for(int j = 0; j < images.length; j++) {
                                  try {
                                      String path = "images/t" + (j+1) + ".gif";
                                      images[j] = ImageIO.read(new File(path));
                                  } catch(IOException e) {
                                      System.out.println("Read error: " + e.getMessage());

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by fatfatpopo
                        Kindly tell me what is going on in the codes below

                        Code:
                        for(int j = 0; j < images.length; j++) {
                                    try {
                                        String path = "images/t" + (j+1) + ".gif";
                                        images[j] = ImageIO.read(new File(path));
                                    } catch(IOException e) {
                                        System.out.println("Read error: " + e.getMessage());
                        Dry run it on a piece of paper with sample values and tell us what you think it's doing.

                        Comment

                        • fatfatpopo
                          New Member
                          • Jun 2007
                          • 16

                          #13
                          Originally posted by r035198x
                          Dry run it on a piece of paper with sample values and tell us what you think it's doing.

                          Can anyone tell me, how come im not able to view it at the browser? Thanks alot..

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by fatfatpopo
                            Can anyone tell me, how come im not able to view it at the browser? Thanks alot..
                            Did you manage to run it on some simple editor like Textpad?
                            How are you running it on the browser and what does it do when you run it?

                            Comment

                            • fatfatpopo
                              New Member
                              • Jun 2007
                              • 16

                              #15
                              Originally posted by r035198x
                              Did you manage to run it on some simple editor like Textpad?
                              How are you running it on the browser and what does it do when you run it?

                              Do you know what does the error mean?
                              Below is the error message shown in Java Console:


                              java.security.A ccessControlExc eption: access denied (java.io.FilePe rmission images\t1.gif read)
                              at java.security.A ccessControlCon text.checkPermi ssion(Unknown Source)
                              at java.security.A ccessController .checkPermissio n(Unknown Source)
                              at java.lang.Secur ityManager.chec kPermission(Unk nown Source)
                              at java.lang.Secur ityManager.chec kRead(Unknown Source)
                              at java.io.File.ca nRead(Unknown Source)
                              at javax.imageio.I mageIO.read(Unk nown Source)
                              at ImageSlider.get Images(ImageSli der.java:48)
                              at ImageSlider.ini t(ImageSlider.j ava:15)
                              at sun.applet.Appl etPanel.run(Unk nown Source)
                              at java.lang.Threa d.run(Unknown Source)

                              Comment

                              Working...