Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Can't load image from folder. ImageIcon

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    47
    My Mood
    Amused
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Question Can't load image from folder. ImageIcon

    Hi everyone. I'm trying to load and image to a program but for some reason it's not working.

    Main code:
    package javdrawimagetst;
    import javax.swing.JFrame;
     
     
     
    public class JavDrawImagetst {
     
     
     
        public static void main(String[] args) {
            JFrame w = new JFrame("Images");
            w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            w.setSize(640, 480);
            Screen s = new Screen();
            w.add(s);
            w.setVisible(true);
     
     
     
        }
    }

    csecond class:
    package javdrawimagetst;
    import java.awt.*;
    import javax.swing.*;
     
     
     
     
     
    public class Screen extends JPanel{
     
        private Image youLost;
     
        public Screen(){
            loadImages();
        }
     
     
     
        public void loadImages(){
            youLost = new ImageIcon("You_lostPNG.png").getImage();
            repaint();
     
     
        }
     
     
     
        public void paintComponent(Graphics g){
            super.paintComponent(g);
     
            g.setColor(Color.RED);
            g.fillRect(32, 32, 65, 76);
            g.drawImage(youLost, 32, 54, this);
     
     
        }
     
    }

    The image is in the folder. Is it because I'm trying to draw on a paintcomponent?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,737
    My Mood
    Mellow
    Thanks
    233
    Thanked 709 Times in 695 Posts

    Default Re: Can't load image from foled. ImageIcon

    When does the image youLost get added to the JPanel?

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    47
    My Mood
    Amused
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Can't load image from folder. ImageIcon

    ahh, I forgot that, but how would you do that? I tryed this, to make it a label since JPanel doesn't seem to accept Image objects:
        ImageIcon i=new ImageIcon("You_lostPNG.png");
        Image scaleImage=i.getImage().getScaledInstance(70,70,Image.SCALE_DEFAULT);
        ImageIcon ii=new ImageIcon(scaleImage);
        JLabel pic=new JLabel(ii);
        panel.add(pic);

    But it didn't work.

Similar Threads

  1. image does not load to .jar file after been compile
    By azuma kazuma in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 26th, 2013, 08:34 AM
  2. How to put in a "new" image in ImageIcon JFrame ?
    By craigjlner in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 29th, 2013, 03:17 PM
  3. Help with ImageIcon
    By nivangerow in forum What's Wrong With My Code?
    Replies: 18
    Last Post: August 30th, 2011, 10:39 AM
  4. ImageIcon Erratically Loading URL Image
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 3rd, 2011, 03:05 PM
  5. Image Load
    By shadihrr in forum Java Theory & Questions
    Replies: 4
    Last Post: August 26th, 2010, 06:56 AM