user3760100
user3760100

Reputation: 685

drawImage() in Java, why isn't the image loading?

I have added the image in the src and bin directories and cross-checked that the name of the image file is correct

Here is the main class

import javax.swing.*;

public class apples
{
    public static void main(String args[])
    {
        JFrame frame = new JFrame();
        MyDrawPanel wid = new MyDrawPanel();
        frame.add(wid);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setSize(300,300);
    }
}

and here is the class that does the image adding part

import java.awt.*;
import javax.swing.*;

public class MyDrawPanel extends JPanel 
{
    public void paintComponent(Graphics g)
    {

        Image image = new ImageIcon("b.png").getImage();
        g.drawImage(image,20, 20, this);
    }
}

Upvotes: 1

Views: 169

Answers (4)

JLundhoo
JLundhoo

Reputation: 11

You're declaring a JFrame called frame and correctly declaring a class that inherits from Panel that can be drawn upon. The method paintComponent(Graphics G) in MyDrawPanel.Java is called upon every time the image needs to be rewritten.

I tested out your code in my own IDE and it works for me. I think that, as others also have suggested, that your picture needs to be dragged into your Eclipse IDE. Just drag-and-drop it into your Java-project.

Upvotes: 0

HassanBakri
HassanBakri

Reputation: 91

You have to add your image (or any file) in the main project file when you work with eclipse or other frameworks
and if you decides to specialize a specific folder in the project -to hold images for example- you can write Image image = new ImageIcon("src\\b.png").getImage();//replace the src with folder name

Or add the full (absolute)path

Upvotes: 0

mKorbel
mKorbel

Reputation: 109815

  1. frame.setVisible(true); should be last code line inside public static void main(String args[]), because you setSize to already visible JFrame (just torso contains only Toolbar with three Buttons)

  2. every Swing code lines in public static void main(String args[]) should be wrapped into invokeLater(), more info about in Oracle tutorial Initial Thread

  3. public class MyDrawPanel extends JPanel returns zero Dimension (0, 0) you have to override getPreferredSize for (inside) MyDrawPanel extends JPanel, use there new Dimension (300, 300) from frame.setSize(300,300); and then replace this code line (frame.setSize(300,300);) with frame.pack()

  4. Image image = new ImageIcon("b.png").getImage();

    a) don't to load any FileIO inside paintComponent, create this Object as local variable

    b) 1st code line inside paintComponent should be super.paintComponent() and without reason to be public, but protected (public void paintComponent(Graphics g))

    c) Dimension set in g.drawImage(image,20, 20, this); doesn't corresponding with frame.setSize(300,300);, for why reason is there empty space

    d) most important (as mentioned in comments) Image image = new ImageIcon("b.png").getImage(); isn't valid Java path

Upvotes: 2

Mohammed Rampurawala
Mohammed Rampurawala

Reputation: 3112

try to use getClass().getResource("b.png"); instead of simply giving the file name. Because it sometimes doesn't receive the image, so extract the path and resource.

Upvotes: 0

Related Questions