Results 1 to 6 of 6
  1. #1
    tremon is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Unhappy image on JInternalFrame

    hi everyone,

    I'm working on a project java for school with JInternalFrames in a JDesktopFrame.
    I want to add a picture to a JInternalFrame and after some google searches I made a class ImagePanel that extends JPanel

    Java Code:
    class ImagePanel extends JPanel
    {
    //path of image
    private String path;
    
    //image object
    private Image img;
    public ImagePanel(String path) throws IOException
    {
    //save path
    this.path = path;
    
    //load image
    img = ImageIO.read(new File(path));
    
    }
    //override paint method of panel
    public void paint(Graphics g)
    {
    //draw the image
    if( img != null)
    g.drawImage(img,0,0, this);
    }
    
    }
    now I made a ImagePanel in my program
    Java Code:
    private ImagePanel backImage;
    I tried to add it to my JInternalFrame frmHome
    Java Code:
    try {
                backImage = new ImagePanel("D:/users/2930231/Documents/My Dropbox/School Kevin/Java/Groepswerk/Groepswerk/src/groepswerk/theglobe.png");
                
                frmHome.add(backImage);
                backImage.setVisible(true);
                backImage.paint(backImage.getGraphics());
            } catch (IOException ex) {
                Logger.getLogger(Groepswerk.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    Now when I run it, I can't see an image on my frmHome.
    I think i don't understand the paint function of the ImagePanel.

    Can someone help me? :confused:

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    You don't have to call the paint method directly- in fact, you absolutely shouldn't. It is called automatically by Java. If you're calling getGraphics() on a Component, that's a sure sign that you're doing something wrong.

    Actually, you should really be using paintComponent() instead of paint().
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    tremon is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default

    thank you for your reply, but I still don't get the Graphics g parameter in the paintComponents function? how should i use it?

    I got my JInternalframe frmHome on my JDesktopPane

    and I want the ImagePanel backImage on my frmHome...

    Java Code:
    initComponents();
            try {
                backImage = new ImagePanel("D:/users/2930231/Documents/My Dropbox/School Kevin/Java/Groepswerk/Groepswerk/src/groepswerk/theglobe.png");           
                frmHome.add(backImage);
                backImage.setVisible(true);
                backImage.paintComponents(???????);
            } catch (IOException ex) {
                Logger.getLogger(Groepswerk.class.getName()).log(Level.SEVERE, null, ex);
            }

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    No, you don't have to call the paintComponent() - not paintComponents() - method directly either. It should be invoked automatically when the program draws that Component.

    If you really want to tell the Component to call paintComponent, use the repaint() method.

    Read this: Painting in AWT and Swing
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    tremon is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default

    okay, I tried something else now, adding the ImagePanel to the jDesktopFrame and making my homescreen invisible for now :)
    I also added the image to my package resources, and still I get the same exception

    Java Code:
    try {
                backImage = new ImagePanel("/groepswerk/achtergrond.jpg");
                frmHome.setVisible(false);
                backImage.setVisible(true);
                backImage.setBounds(0, 0, 470, 500);
                jDesktopPane1.add(backImage, javax.swing.JLayeredPane.DEFAULT_LAYER);
           
            } catch (IOException ex) {
                Logger.getLogger(Groepswerk.class.getName()).log(Level.SEVERE, null, ex);
            }
    however,
    Java Code:
    backImage = new ImagePanel("/groepswerk/achtergrond.jpg");
    throws a

    SEVERE: null
    javax.imageio.IIOException: Can't read input file! :confused:

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    Quote Originally Posted by tremon View Post
    SEVERE: null
    javax.imageio.IIOException: Can't read input file! :confused:
    We can't really help you with that part. Are you sure your file is located where you're telling your program it's located? Have you spelled everything correctly? Have you used the correct case?

    Edit- I think that paths beginning with a '/' are interpreted as being absolute. I think you're trying to use a relative path instead. What happens if you get rid of that first '/'?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. jInternalFrame
    By kiranjava in forum Advanced Java
    Replies: 1
    Last Post: 05-20-2010, 12:04 PM
  2. JInternalFrame Help
    By collin389 in forum New To Java
    Replies: 2
    Last Post: 03-16-2010, 12:23 AM
  3. JInternalFrame Help
    By collin389 in forum AWT / Swing
    Replies: 1
    Last Post: 03-15-2010, 09:24 PM
  4. Refeshing image in JInternalFrame
    By MasterDeveloper in forum AWT / Swing
    Replies: 7
    Last Post: 04-05-2009, 12:34 PM
  5. Problem with JInternalFrame
    By hameem in forum AWT / Swing
    Replies: 1
    Last Post: 12-11-2008, 04:23 PM

Tags for this Thread

Posting Permissions

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