GUI (JLabel) .JPEG not recegnized

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • momotaro
    Contributor
    • Sep 2006
    • 357

    GUI (JLabel) .JPEG not recegnized

    my code works fine with .GIF but not with .JPEG!!??
    jdk.6.0_02
    jre1.6.0_05
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Please explain. Even better, provide a SSCCE: http://mindprod.com/jgloss/sscce.html

    Comment

    • momotaro
      Contributor
      • Sep 2006
      • 357

      #3
      Originally posted by BigDaddyLH
      Please explain. Even better, provide a SSCCE: http://mindprod.com/jgloss/sscce.html
      Code:
      ort java.awt.BorderLayout;
      import javax.swing.ImageIcon;
      import javax.swing.JLabel;
      import javax.swing.JFrame;
      
      public class LabelDemo
      {
         public static void main( String args[] )
         {
      	  // Create a label with plain text
      	  JLabel northLabel = new JLabel( "North" );
      	  
      	  // create an icon from an image so we can put it on a JLabel
      	  ImageIcon labelIcon = new ImageIcon( "C:\\Users\\george bush\\Desktop\\ring_sat.GIF" );
      	  
      	  // create a label with an Icon instead of text
      	  JLabel centerLabel = new JLabel( labelIcon );
      	  
      	  // create another label with an Icon
      	  JLabel southLabel = new JLabel( labelIcon );
      	  
      	  // set the label to display text (as well as an icon)
      	  southLabel.setText( "South" );
      	  
      	   // create a frame to hold the labels
      	  JFrame application = new JFrame();
      	  
      	  application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      
      	  // add the labels to the frame; the second argument specifies
      	  // where on the frame to add the label 
      	  application.add( northLabel, BorderLayout.NORTH );
      	  application.add( centerLabel, BorderLayout.CENTER );	  
      	  application.add( southLabel, BorderLayout.SOUTH );	  
      
      	  application.setSize( 300, 300); // set the size of the frame
      	  application.setVisible( true ); // show the frame
         } // end main
      } // end class LabelDemo
      when I replace the GIF with the JPEG version the panel is empty but stillI candisplay text

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        [CODE=Java]import java.awt.*;
        import java.net.*;
        import javax.swing.*;

        public class ImageTest {
        public static void main(String[] args) {
        EventQueue.invo keLater(new Runnable(){
        public void run() {
        try {
        launchGUI();
        } catch (MalformedURLEx ception e) {
        e.printStackTra ce();
        }
        }
        });
        }

        static void launchGUI() throws MalformedURLExc eption {
        URL url = new URL("http://blogs.sun.com/jag/resource/JagHeadshot-small.jpg");
        JFrame f = new JFrame("ImageTe st");
        f.getContentPan e().add(new JLabel(new ImageIcon(url)) );
        f.pack();
        f.setDefaultClo seOperation(Win dowConstants.EX IT_ON_CLOSE);
        f.setLocationRe lativeTo(null);
        f.setVisible(tr ue);
        }
        }[/CODE]

        ImageIcon has no problems displaying JPEG images. Perhaps your path is wrong or the file is corrupted.

        Comment

        • momotaro
          Contributor
          • Sep 2006
          • 357

          #5
          thank you nice picture !
          but is there any difference if I use uper/lowercase?
          does the size of the image matter?
          PS: the code is pefectely working on netbeans but as weird as it can sounds it's not working using cmd

          Comment

          • momotaro
            Contributor
            • Sep 2006
            • 357

            #6
            sorry never mind its working now

            Comment

            • momotaro
              Contributor
              • Sep 2006
              • 357

              #7
              now I wanna resize the pic to the demesions of the frame? but can't find any method ??
              plz help

              Comment

              • BigDaddyLH
                Recognized Expert Top Contributor
                • Dec 2007
                • 1216

                #8
                Originally posted by momotaro
                now I wanna resize the pic to the demesions of the frame? but can't find any method ??
                plz help
                Did you look at the methods of Image?

                Comment

                • momotaro
                  Contributor
                  • Sep 2006
                  • 357

                  #9
                  Originally posted by BigDaddyLH
                  Did you look at the methods of Image?
                  there is a method called:
                  paintIcon(compo nent c, Graphics g, x, y);

                  but I don't know what c and g are or how to use them!

                  plz help

                  Comment

                  • BigDaddyLH
                    Recognized Expert Top Contributor
                    • Dec 2007
                    • 1216

                    #10
                    Originally posted by momotaro
                    there is a method called:
                    paintIcon(compo nent c, Graphics g, x, y);

                    but I don't know what c and g are or how to use them!

                    plz help
                    Try again: did you look at the methods of Image?

                    Comment

                    • momotaro
                      Contributor
                      • Sep 2006
                      • 357

                      #11
                      Originally posted by BigDaddyLH
                      Try again: did you look at the methods of Image?
                      thx i think I can figure it out from here ! :)
                      I was looking at the wrong place

                      Comment

                      Working...