1

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);
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3760100
  • 679
  • 1
  • 9
  • 20
  • 4
    try adding @Override before your overriden method, your IDE should alert you is it is not overriden (due to typo, ...). Also call `super.paintComponent(g)` unless you have good reason not to. – kajacx Jun 20 '14 at 12:27
  • 4
    An instance of `MyDrawPanel` will have preferred size of 0x0 pixels. Your image is drawn outside of its bounding rect. – Oleg Estekhin Jun 20 '14 at 12:28
  • 5
    For better performance, do not make a `new ImageIcon("b.png")` in `paintComponent(Graphics g)`, create an `Image` attribute in `MyDrawPanel` – NiziL Jun 20 '14 at 12:28
  • You can also read this tutorial: http://zetcode.com/tutorials/javaswingtutorial/painting/ , or any other. – kajacx Jun 20 '14 at 12:30
  • 1
    Hopefully this [thread](http://stackoverflow.com/a/9866659/1057230), be able to help in this direction somewhat, especially the last link, if you doing it manually :-) – nIcE cOw Jun 20 '14 at 13:16

4 Answers4

2
  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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

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.

Mohammed Rampurawala
  • 3,033
  • 2
  • 22
  • 32
0

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

0

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.

JLundhoo
  • 11
  • 3