10

I have a JPanel into a JFrame. I loaded a picture on the JPanel but its shown just a part of the picture: This is the part of the code where i did it:

JPanel panelImg = new JPanel()
{
    public void paintComponent(Graphics g)
    {
        Image img = new ImageIcon("Welcome.png").getImage();
        Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size);
        setLayout(null);
        g.drawImage(img, 0, 0, null);
    }
};
mainFrame.add(panelImg);

So this is how it looks like:

Picture 1

The complete picture looks like this:

enter image description here

Is there a way to scale the picture to the JFrames size? Thanks in advance

David Kroukamp
  • 35,635
  • 13
  • 75
  • 131
  • 2
    Have a look at some of these answers: http://stackoverflow.com/a/12660146/1133011 and here http://stackoverflow.com/a/12996718/1133011 they both show how to display and scale images on `JPanel`/`JFrame`. You will just have to scale it now to frame size via `getWidth()` and `getHeight()` (+1 to @trashgod and @MadProgrammer) – David Kroukamp Oct 23 '12 at 20:53

5 Answers5

14

You want the drawImage() that scales to the target container. See the article cited here for alternatives. For example,

g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
Community
  • 1
  • 1
trashgod
  • 200,320
  • 28
  • 229
  • 974
13

First of all, I wouldn't be loading the image inside the paintComponent method, this method is call repeatedly (and some times in quick succession), you don't want to do anything that takes time to execute or consumes resources unnecessarily

Check out Java: maintaining aspect ratio of JPanel background image for suggestions on filling/fitting images to a given area

Community
  • 1
  • 1
MadProgrammer
  • 336,120
  • 22
  • 219
  • 344
2
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;

public class ImagePanel extends JPanel {

    Image image;

    public void setBackground(Image image) {
        this.image = image;
    }

    @Override
    public void paintComponent(Graphics G) {
        super.paintComponent(G);
        G.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null);
    }

}

Then use the ImagePanel object method SetBackground like

imagePanel1.SetBackGround(ImageIO.read(new File("extensions/images/background.jpg")));
Eng.Fouad
  • 111,301
  • 67
  • 311
  • 403
  • 2
    A JPanel is opaque by default. So your implementation of paintComponent potentially violates the its opaqueness contract (if the image isn't completedly non-transparent). To fix, either call super.paintComponent or set the opaque property to false. BTW: Please learn java naming conventions and stick to them. – kleopatra Jul 03 '13 at 13:13
  • sorry what do you mean by naming conventions !! :/ – Bishoy Basily Aug 29 '13 at 06:37
  • how about typing _java naming conventions_ into the search field of your faviourite search engine .. ? Or read the corresponding chapter in the tutorial referenced in the swing tag wiki. Or ... anyway: there are rules how to _name_ classes, fields, methods, everything: you violated the method-naming one by starting it with an upper case latter. – kleopatra Aug 29 '13 at 07:21
  • should i remove this answer?! :D :D – Bishoy Basily Aug 29 '13 at 07:43
  • edit and rename (to something that doesn't accidentally override a super method :-) – kleopatra Aug 29 '13 at 07:47
1

you can try this :

Image img = new ImageIcon(ImageIO.read(new File("welcome.png"))
                               .getScaledInstance(WIDTH, HEIGHT, Image.SCALE_SMOOTH)));
Eng.Fouad
  • 111,301
  • 67
  • 311
  • 403
AvB
  • 171
  • 2
  • 12
1

Its Easy. Follow this example,

public class BasePanel extends JPanel {

ImageIcon backImage;
public BasePanel() {
    backImage = new ImageIcon("src/images/welcome.png");
}

@Override
protected void paintComponent(Graphics g) {
    BufferedImage scaledImage = getScaledImage();
    super.paintComponent(g);
    g.drawImage(scaledImage, 0, 0, null);
}

private BufferedImage getScaledImage(){
    BufferedImage image = new BufferedImage(getWidth(),getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = (Graphics2D) image.createGraphics();
    g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY));
    g2d.drawImage(backImage.getImage(), 0, 0,getWidth(),getHeight(), null);

    return image;
}

}
javatar
  • 1,294
  • 1
  • 17
  • 24