Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Image won't load

  1. #1
    jim.f.hurley is offline Member
    Join Date
    Sep 2012
    Location
    MI
    Posts
    6
    Rep Power
    0

    Exclamation Image won't load

    When I run this code, I am attempting to print an image on the screen, however when I run it all I get is a blank screen. Does anyone know what I might be doing wrong?

    <I use Net Beans>

    Java Code:
    package imgloadtest;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    
    
    public class Main extends JFrame {
        
        Image hero;
        
        public Main(){
            //load images
            ImageIcon i = new ImageIcon("C:/Users/Jim/Documents/Animations/Testing/Hero_Punching/output 01.png");
            hero = i.getImage();
            
            //define gui
            setTitle("Image Load Test");
            setSize(800, 500);
            setResizable(false);
            setVisible(true);
            setBackground(Color.LIGHT_GRAY);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            
        }
        
        @Override
        public void paintComponents(Graphics g) {
            g.drawImage(hero, 400, 250, this);
            
            repaint();
        }
        
        public static void main(String[] args) {
            
            Main main = new Main();
            
        }
        
    }
    Last edited by jim.f.hurley; 10-03-2012 at 02:50 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Image won't load

    Read BB Code List - Java Programming Forum and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Image won't load

    You're overriding the wrong method, in the wrong Component, and also possibly maxing out your processor with a repaint() call in a painting method.

    Go through this Tutorial Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db
    David Kroukamp likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    jim.f.hurley is offline Member
    Join Date
    Sep 2012
    Location
    MI
    Posts
    6
    Rep Power
    0

    Default Re: Image won't load

    I had already done a tutorial or two on how to paint an image (or anything in the graphics class for that matter) onto the screen, and this was my attempt to do it myself. I had already read tutorials trying to fix it and couldn't figure it out, so I was wondering if anybody knew why it wasn't working in this instance and I could study accordingly. I don't quite understand what you mean when you say "You're overriding the wrong method, in the wrong Component, and also possibly maxing out your processor with a repaint() call in a painting method." Could you elaborate?

  5. #5
    jim.f.hurley is offline Member
    Join Date
    Sep 2012
    Location
    MI
    Posts
    6
    Rep Power
    0

    Default Re: Image won't load

    Also, I tried removing the override annotation already, and the repaint method, and it didn't fix anything.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Image won't load

    Let's take it one at a time.

    What painting method do you override in the posted code?
    What painting method is overridden in the samples in the tutorial I linked?

    Oh, and the @Override annotation does NOT determine whether a method is overridden or not. The annotation provides compile-time checking that the method does indeed override an inherited method.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    jim.f.hurley is offline Member
    Join Date
    Sep 2012
    Location
    MI
    Posts
    6
    Rep Power
    0

    Default Re: Image won't load

    I read it and still can't figure out why it won't load. Do you know what's wrong with it?

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Image won't load

    What painting method do you override in the posted code?
    What painting method is overridden in the samples in the tutorial I linked?
    If you have changed things, pot the new code.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Load image from jar
    By sublixt in forum Java 2D
    Replies: 2
    Last Post: 10-17-2011, 12:15 AM
  2. Error: cannot load image .
    By iswan in forum Java Applets
    Replies: 1
    Last Post: 09-29-2011, 02:26 AM
  3. Load Image before the Application
    By saber210 in forum AWT / Swing
    Replies: 3
    Last Post: 08-05-2011, 09:44 PM
  4. [SOLVED] Load image into a JSP
    By jazz2k8 in forum New To Java
    Replies: 0
    Last Post: 05-08-2008, 11:33 AM
  5. Help with load image
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 07:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •