How to fit an image to JPanel

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 2155 Views - Last Post: 09 May 2012 - 03:48 PM Rate Topic: -----

#16 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8017
  • View blog
  • Posts: 31,124
  • Joined: 06-March 08

Re: How to fit an image to JPanel

Posted 09 May 2012 - 08:41 AM

put
imagePanel.revalidate();
imagePanel.repaint();

after the if/else
so it will apply in both case (when you add(label) or you add(img)

Worst case put a println() in the paint() method of ImageLabel to see that it is effectively called and print the getWidth() and getHeight()

You can also ImageLabel constructor to receive the ImageIcon as parameter instead of the file name as you already have the Icon.

Don't see why it shouldn't work

This post has been edited by pbl: 09 May 2012 - 08:42 AM

Was This Post Helpful? 1
  • +
  • -

#17 Chario0z  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-April 12

Re: How to fit an image to JPanel

Posted 09 May 2012 - 12:40 PM

Well, it doesn't seem to work with reValidate() and repaint() after if/else-statement.

I put a System.out.println in the paint() method, but I don't get any output... I tried
public void paint( Graphics g ) {
		System.out.println("Test");
		super.paint( g ) ;
		g.drawImage(image,  0 , 0 , getWidth() , getHeight() , imageObserver);
		System.out.println("Test2");
	}


Didn't get the output.. What is wrong with this method?
Was This Post Helpful? 0
  • +
  • -

#18 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8017
  • View blog
  • Posts: 31,124
  • Joined: 06-March 08

Re: How to fit an image to JPanel

Posted 09 May 2012 - 12:51 PM

Should be called... the only thing I can see is that the ImageLabel is not visible (overload by another component)

Will have to see your code setBackground(Color.RED) of your imagePanel to see wher it is displayed. Is there any other component in it ?
Why do you removeAll() on it ? Can something else be in there ?
I would put the both label in the JPanel and set them setVisible(false) and then just toggle their visibility in your method.

Modify imageLabel() with a setter to change the displayed image if you require it
Was This Post Helpful? 1
  • +
  • -

#19 Chario0z  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-April 12

Re: How to fit an image to JPanel

Posted 09 May 2012 - 01:09 PM

I don't have any other components. But I've problem with my printlines, whenever I run, it goes twice.
Also one println to test gives me two outputs. Maybe that's my problem? I don't understand why it goes twice..

I can set the background color to red on JPanel.

I have to removeAll(), or the same pictures stays for every selection on the JList.

I'm also not sure if this is right:
ImageLabel img = new ImageLabel(B)/>; // where b is my ImageIcon sent as parameter

If I use
img.setIcon(B)/>; 

I can see the picture, but it's the same size. It doesn't fill the whole JPanel..
Was This Post Helpful? 0
  • +
  • -

#20 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8017
  • View blog
  • Posts: 31,124
  • Joined: 06-March 08

Re: How to fit an image to JPanel

Posted 09 May 2012 - 01:28 PM

It is normal it is printed twice, when the GUI is displayed the first time, you can't beleive the number of times certain componants can be repainted :)

You added your set() method but does it does like the constructor getImage() and reset the imageObserver ?
Was This Post Helpful? 1
  • +
  • -

#21 Chario0z  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-April 12

Re: How to fit an image to JPanel

Posted 09 May 2012 - 01:39 PM

I didn't add any set() methods. I thought since the class extends JLabel, I could use label.setIcon to add the picture, but this time with the class..
Was This Post Helpful? 0
  • +
  • -

#22 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8017
  • View blog
  • Posts: 31,124
  • Joined: 06-March 08

Re: How to fit an image to JPanel

Posted 09 May 2012 - 01:47 PM

Ya :)
And will display it as a small image like a JLabel does
You want to hack this behavior, this is why we cheat and write our own that draws an Image not an ImageIcon
you will have to:

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.ImageObserver;

import javax.swing.ImageIcon;
import javax.swing.*;

class ImageLabel extends JLabel {
	Image image;
	ImageObserver imageObserver; 
	
	ImageLabel(String path) {
		ImageIcon icon = new ImageIcon(path);
		image = icon.getImage();
		imageObserver = icon.getImageObserver();
	}

        void set(ImageIcon icon) {
             image = icon.getImage();
             imageObserver = icon.getImageObserver();
        }
	public void paint( Graphics g ) {
		super.paint( g ) ;
		g.drawImage(image,  0 , 0 , getWidth() , getHeight() , imageObserver);
	}
}



Was This Post Helpful? 1
  • +
  • -

#23 Chario0z  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-April 12

Re: How to fit an image to JPanel

Posted 09 May 2012 - 02:25 PM

I still get the same result.. The picture somehow won't display. It won't go to the paint() method either.. :(

I put a println() method on the set() method, I got an output. But it doesn't go to the paint() method, it doesn't want to draw the picture at all.
Was This Post Helpful? 0
  • +
  • -

#24 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8017
  • View blog
  • Posts: 31,124
  • Joined: 06-March 08

Re: How to fit an image to JPanel

Posted 09 May 2012 - 02:36 PM

You are doing something wrong somewhere else. As nothing to do with the posted code
Was This Post Helpful? 1
  • +
  • -

#25 Chario0z  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-April 12

Re: How to fit an image to JPanel

Posted 09 May 2012 - 03:03 PM

Hmm, let me show some of my code in the same class:

public class AnimalGUI extends JPanel {
    private JSplitPane splitPane;
    private JList list;
    private DefaultListModel model;
    private String[] animals;
    private Animal animalReg;
    private JPanel imagePanel;
    private JTextArea info;

    ImageIcon b = null;

    // Also some textField and button declarations. 

    public AnimalGUI() {
        this.setLayout(new BorderLayout());

        model = new DefaultListModel();
        
        imagePanel = new JPanel(); // this is where I want to add the image. 

        animals = animalReg.getAnimalNames();
        
        // Adding the animal to the JList
        if(animals != null) 
            for(int i = 0; i < animals.length; i++) 
                model.addElement(animals[i]);

        list = new JList(model);
        list.setSelectionMode(ListSelectionMode.SINGLE_SELECTION);
        list.addListSelectionListener(new ListListener());

        JScrollPane infoScroll = new JScrollPane(info);

        // This is the panel for image and text
        JPanel imageText = new JPanel();
        imageText.setLayout(new GridLayout(2,1));
        imageText.add(imagePanel);
        imageText.add(infoScroll);

        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, list,
				imageText);
        splitPane.setOneTouchExpandable(true);
	splitPane.setDividerLocation(150);

	Dimension minSize = new Dimension(100, 50);
	list.setMinimumSize(minSize);
	infoScroll.setMinimumSize(minSize);

	splitPane.setPreferredSize(new Dimension(200, 300));

        this.add(splitPane, BorderLayout.CENTER);
    }

    // Some other methods, like write to file, read from file, showMessage(with JOptionMane) etc.

    // ListListener
    private class ListListener implements ListSelectionListener {
	public void valueChanged(ListSelectionEvent e) {
		JList list = (JList) e.getSource();
                
                Animal a = animalReg.getAnimal(list.getSelectedValue().toString());

                ImageLabel img = null;
                JLabel label = new JLabel();
                
                if(a != null) {
                    b = a.getPicture();
                    if( b != null ) {
                        img = new ImageLabel(b.toString());
                        img.set(B)/>;
                    }
                    else
                        label.setText("Picture not found");

                    info.setText(a.toString());
                }

                imagePanel.removeAll();
                if(img != null) {
		    imagePanel.add(img);
		}
		else
		    imagePanel.add(label);
                imagePanel.revalidate();
                imagePanel.repaint();
                
        }
}

}


Can you see something wrong?

Edit: Typing-error. It's ImageIcon b = null; (not img)

This post has been edited by Chario0z: 09 May 2012 - 03:09 PM

Was This Post Helpful? 0
  • +
  • -

#26 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8017
  • View blog
  • Posts: 31,124
  • Joined: 06-March 08

Re: How to fit an image to JPanel

Posted 09 May 2012 - 03:16 PM

did you imagePanel.setBackground(Color.RED); as I suggested ?
do you see it ?
Was This Post Helpful? 1
  • +
  • -

#27 Chario0z  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-April 12

Re: How to fit an image to JPanel

Posted 09 May 2012 - 03:18 PM

Yes. If I set imagePanel.setBackground(Color.RED); I can see it.
Was This Post Helpful? 0
  • +
  • -

#28 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8017
  • View blog
  • Posts: 31,124
  • Joined: 06-March 08

Re: How to fit an image to JPanel

Posted 09 May 2012 - 03:37 PM

make imagePanel = new JPanel(new GridLayout(1,1))
That will force the ImageLabel to take all the room
Was This Post Helpful? 1
  • +
  • -

#29 Chario0z  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-April 12

Re: How to fit an image to JPanel

Posted 09 May 2012 - 03:42 PM

View Postpbl, on 09 May 2012 - 03:37 PM, said:

make imagePanel = new JPanel(new GridLayout(1,1))
That will force the ImageLabel to take all the room


Oh my god, you're a hero! I don't know how to thank you! :D A few days with frustration, almost giving up, and the solution was to set GridLayout on imagePanel.

Again, thanks! :D
Was This Post Helpful? 0
  • +
  • -

#30 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2110
  • View blog
  • Posts: 8,774
  • Joined: 20-September 08

Re: How to fit an image to JPanel

Posted 09 May 2012 - 03:48 PM

A BorderLayout will work just as well. Your gui (to avoid nasty changes that surprise the user [ see the 'principle of least astonishment']) should really only need a JPanel with that layout set as the viewport view of a JScrollPane

This post has been edited by g00se: 09 May 2012 - 03:49 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2