1

where is the problem. It does not show image properly.

default.png = a busy cat*********

what i see = a busy cat*********

I dont think there is a problem with png. It is also in my src and I refreshed it on eclipse.

codes:

import java.awt.*;

import javax.swing.*;

public class Main {

public static void main(String[] args) {


    JFrame jf = new JFrame();

    jf.setTitle( "test");
    jf.setLayout( new FlowLayout());
    jf.setSize(350, 450);
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    jf.add(new Panel());

}

}

panel:

import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;


public class Panel extends JPanel {

// PROPERTIES

public ImageIcon icon;

// CONSTRUCTORS

public Panel() {

    icon = new ImageIcon(this.getClass().getResource("default.png"));
}

public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    g.drawImage(icon.getImage(), 0, 0, null);

}


}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
funky-nd
  • 637
  • 1
  • 9
  • 25

2 Answers2

1

This answer addresses my criticism of the answer by the OP. The example incorporates the advice added in various comments.

enter image description here

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

import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.border.LineBorder;

public class Main {

    public static void main(String[] args) throws Exception {
        URL url = new URL("http://i.imgur.com/o0E0aGD.png");
        BufferedImage bi = ImageIO.read(url);
        JFrame jf = new JFrame();

        jf.setTitle("test");
        jf.setLayout(new FlowLayout());
        //jf.setSize(350, 450); just pack()
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jf.add(new BackgroundImagePanel(bi));

        jf.pack();
        jf.setMinimumSize(jf.getSize());
        jf.setVisible(true);
    }
}

class BackgroundImagePanel extends JPanel {

    public Image img;

    // CONSTRUCTOR
    public BackgroundImagePanel(Image img) {
        this.img = img;
        this.setBorder(new LineBorder(Color.RED, 2));
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, this);
        // g.drawImage(img, 0, 0, getWidth(), getHeight(), this);  // Better!
    }

    @Override
    public Dimension getPreferredSize() {
        Dimension d = super.getPreferredSize();
        int w = d.width>img.getWidth(this) ? d.width : img.getWidth(this);
        int h = d.height>img.getHeight(this) ? d.height : img.getHeight(this);

        return new Dimension(w, h);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

I solved this by adding at Panel class constructor:

setPreferredSize(new Dimension( 500, 500 ));
Pang
  • 9,564
  • 146
  • 81
  • 122
funky-nd
  • 637
  • 1
  • 9
  • 25
  • 1
    probably setting size calls g.repaint, which you didn't. could you check if g.repaint() after g.drawImage() would work? – zubergu Jun 01 '15 at 10:19
  • 1
    See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) The correct approach in this case is to have the `Panel` (better named `ImagePanel` or `BackgoundImagePanel` BTW) **`@Override`** the `getPreferredSize()` method and return (at minimum) the size of the image. Then after adding it & all other components to the frame, call `pack()` on the frame & it will be the exact size needed. – Andrew Thompson Jun 01 '15 at 12:18
  • 1
    .. or, as suggested by @StanislavL, if not adding any other graphics elements, paints or components in addition to the image, just drop the custom component completely and show the image in a `JLabel`. – Andrew Thompson Jun 01 '15 at 12:21