#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    22
    Rep Power
    0

    Drawing a picture.


    Hello once again. As you can understand from the title of this message I am having problems when I am trying to load an image . To be more precise it only shows me an empty Panel with no pictures what so ever. Here's the code.
    Code:
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.WindowConstants;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.awt.event.*;
    public class JavaApplication18 extends JFrame {
    
        public static void main(String[] args) throws IOException {
            JFrame frame = buildFrame();
    
          //  final BufferedImage image = ImageIO.read(new File("C:\\Users\\DSC_1313.JPEG"));
          //  JLabel pane= new JLabel(new ImageIcon(image));
            ImageIcon image= new ImageIcon("C:\\Users\\DSC_1313.jpeg");
            JLabel pane= new JLabel();
            pane.setIcon(image);
    pane.setBounds(0, 0, 50, 50);
            pane.setLayout(null);
            frame.setLayout(new GridLayout(1,1));
            frame.add(pane);
            frame.setVisible(true);
        }
    
    
        private static JFrame buildFrame() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.setSize(200, 200);
            frame.setVisible(true);
            
            
            return frame;
        }
    
    
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Check that the image exists and is at the correct location. The code works for me with a good image and path.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Location
    India
    Posts
    68
    Rep Power
    1
    Hello Uniflame, I was unable to fully understand the cause of your problem. Its certainly not your code because I was able to successfully compile and run it on my IDE (and by using my own files, of course!). I can solve your problem if you'll be a bit more specific, for example:

    What JDK version are you using (is it an SE, EE or ME)?
    What IDE you're using?
    etc.

    And I would suggest you to create your program as an Applet instead of a JFrame and then run it in a Java Compatible Web-Browser. You can also try loading your packages as a whole (such as 'import java.io.*;' instead of 'import java.io.IOException;' or something), though this'll slow down your execution but this will reduce the bugs to some extent (you can then again add that code after you found the root of your problem).

    And one more thing, don't post the code of your whole class at once, this makes reading and finding a solution more difficult, most guys will not be able to find a solution to your bug if you post your whole code at once. Post the code that 'is only' or 'as you think' is having a problem.

    Thanks,
    -HexMan
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    You can also try loading your packages as a whole (such as 'import java.io.*;' instead of 'import java.io.IOException;' or something), though this'll slow down your execution
    imports will NOT change program execution. The import statement is used by the compiler to find class definitions. What import is used has no effect on program execution.

    don't post the code of your whole class at once,
    It depends on where the problem is. If the OP knew where and what was causing the problem, he probably wouldn't have to post. I think more is better.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    22
    Rep Power
    0
    Well I have the latest version of java. I even created a new file in C drive and placed the picture inside. I read somewhere that the root of the problem could be that the picture is a JPEG one. I used a png one and it worked. So I am asking is there a way to make JPEG images to work?
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    jpg files work for me.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    22
    Rep Power
    0
    Another question . Is there a way to resize the image?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Location
    India
    Posts
    68
    Rep Power
    1
    So I am asking is there a way to make JPEG images to work?
    Well, what I used as the image in your code was a JPEG image, So I don't think I've an answer to this. You can try using other formats of images, the Image I/O supports GIF, BMP, JPEG and PNG (I use bmp in my programs).

    Another question . Is there a way to resize the image?
    Well....I'm not an expert in Java GUI programming but you can use Graphics2D in Java to re-size your image:

    Code:
    BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
    g.dispose();
    Thanks.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    22
    Rep Power
    0
    Originally Posted by hexman
    Well, what I used as the image in your code was a JPEG image, So I don't think I've an answer to this. You can try using other formats of images, the Image I/O supports GIF, BMP, JPEG and PNG (I use bmp in my programs).


    Well....I'm not an expert in Java GUI programming but you can use Graphics2D in Java to re-size your image:

    Code:
    BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
    g.dispose();
    Thanks.
    Well in my program I used ImageIcon instead of BufferedImage , where do I put the c path? Can you write me please the whole code with the path from c drive included?

IMN logo majestic logo threadwatch logo seochat tools logo