Computernerd
Computernerd

Reputation: 7766

Unclear image added to specific JPanel

Background info : I have a JFrame with a gridlayout of (5,7) and am trying to add an image to the cell grid of (0,0)

I was following this guide on adding image to a specific JPanel .

To show a image in a JPanel cell:

  1. List item
  2. Put the chip image into an ImageIcon
  3. Put that ImageIcon into a JLabel via JLabel's setIcon(chipIcon) method
  4. Add the JLabel to the JPanel via the add(someLabel) method -- and the JPanel now will display the image.

Problem

The image is added to the JPanel but it seems the image is enlarged alot and then added to the JPanel .

Reality

enter image description here

Expectations

enter image description here

Compilable source code

package testing;

import java.io.*;
import java.util.*;
import java.security.*;
import javax.xml.bind.DatatypeConverter;
import java.lang.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;


public class Testing extends JPanel 
{

    public static class TestPanel extends JPanel
{
    TestPanel()
    {
        int i = 5; // num of rows
        int j = 7; // num of cols

        PanelHolder = new JPanel[i][j];
        setLayout(new GridLayout(i,j));

        for (int m=0 ; m<i;m++)
        {
            for(int n=0;n<j;n++)
            {
                PanelHolder[m][n] = new JPanel();
                add(PanelHolder[m][n]);
            }
        }





        ImageIcon ic1 = new ImageIcon("./src/testing/Ace_Diamond_1_1.png");
        JLabel jl1 = new JLabel();
        jl1.setIcon(ic1);
        PanelHolder[0][0].add(jl1);



    }
      JPanel [][] PanelHolder;

    JLabel DeckLabel;
    JPanel DeckPanel;
    ImageIcon Deckimg;

    private BufferedImage image;
    BufferedImage image2;

}


public static class ImagePanel extends JPanel
{
    ImagePanel(BufferedImage image)
    {
        this.i = image;

    }


    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        g.drawImage(i,0,0, getWidth(), getHeight(), this);



    }


    BufferedImage i;
}




    public static void main(String[] args) 
    {
        TestPanel tp = new TestPanel();

        JFrame frame = new JFrame();

        frame.add(tp);
        frame.pack();
        frame.setVisible(true);


    }


}

Download the image here

Note: For some unknown reason , you have to clean and rebuilt the solution for any changes you made else it will stick to the old code

Upvotes: 0

Views: 76

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

The problem most likely is, you card image size (691x963) is too large for your cell. This will make you main frame at least 3455 pixels wide and 6741 pixels high...:P

You will either need to downsize the original image or scale it dynamically, for example...

BufferedImage card = ImageIO.read(getClass().getResource("/Ace_Diamond_1_1.png"));
Image scaledInstance = card.getScaledInstance(100, -1, Image.SCALE_SMOOTH);
JLabel jl1 = new JLabel(new ImageIcon(scaledInstance));

I should point out that getScaledInstance is not the best choice when it comes to scaling images, see The Perils of Image.getScaledInstance() for a more detailed discussion and check out Quality of Image after resize very low -- Java for an alternative approach

Upvotes: 1

Related Questions