Results 1 to 5 of 5
  1. #1
    xbox_nutter is offline Member
    Join Date
    Mar 2009
    Posts
    15
    Rep Power
    0

    Default is there anyone how can help me with image icons please?

    Hey
    I am just playing around with loading up images and seem to of hit a wall!

    Here is the main class
    Java Code:
    import javax.swing.JFrame;
    
    
    
    public class mainTests {
    
    	public static void main(String[] args) {
    		RandomStuff image;
    		
    		JFrame main = new JFrame("main");
    		main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		image = new RandomStuff();
    		main.getContentPane().add(image);
    		
    		main.setSize(300,300);
    		main.setVisible(true);
    	}
    }
    and here is the class i want to load an image from:
    Java Code:
    import java.awt.*;
    
    import javax.swing.*;
    
    public class RandomStuff extends JPanel{
    	
    	public void paintComponent(final Graphics2D g) {
    
    		Image test = new ImageIcon("C:\\Documents and Settings\\main.user\\My Documents\\My Pictures\\autobots_logo_sm.jpg").getImage();
    		g.drawImage(test,10,10,this);
    
    	}
    }
    Does anyone have any idea why this is not working please ?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    1. Never perform I/O and/or program logic in a painting method. Load the image in advance, possibly in the constructor.

    2. Constructing an ImageIcon only to obtain its Image is a really bad idea. Read the API for ImageIO.read(...). Or use the ImageIcon itself by setting it to a JLabel.

    db

  3. #3
    xbox_nutter is offline Member
    Join Date
    Mar 2009
    Posts
    15
    Rep Power
    0

    Default

    wicked thanks for the advise

    So in the situation what would you suggest?
    Thanks again!

  4. #4
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    method used in java tutorial
    Java Code:
    /** Returns an ImageIcon, or null if the path was invalid. */
    protected ImageIcon createImageIcon(String path,
                                               String description) {
        java.net.URL imgURL = getClass().getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL, description);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

  5. #5
    xbox_nutter is offline Member
    Join Date
    Mar 2009
    Posts
    15
    Rep Power
    0

    Default

    Thank you very much
    put me on the path to the light side
    :)

Similar Threads

  1. [SOLVED] Icons path
    By janpol1 in forum Eclipse
    Replies: 1
    Last Post: 03-03-2009, 12:31 PM
  2. How do I display a JList with icons and text?
    By aneesahamedaa in forum AWT / Swing
    Replies: 2
    Last Post: 09-04-2008, 08:49 AM
  3. Displaying a Button with Varying Icons
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 07:40 PM
  4. Replies: 0
    Last Post: 01-08-2008, 05:29 PM
  5. Moving icons on your desktop
    By Leprechaun in forum New To Java
    Replies: 3
    Last Post: 12-14-2007, 10:07 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
  •