How can I resize the Image panel in JLayeredPane ?

Hi, I'm currently developing swing application.

I need to draw background image and some images on it.

So, I chose to use JLayeredPane.

I checked the related articles in this forum as the followings.

Chess game with Drag and drop using JLayeredPane

-http://forum.java.sun.com/thread.jspa?forumID=57&threadID=518707

Drawing background images on JFrame components

-http://forum.java.sun.com/thread.jspa?forumID=57&threadID=316074

The code (http://www.discoverteenergy.com/files/ImagePanel.java) from the second article is used for background image drawing.

It works well in the other components, but not in the JLayeredPane.

When I use the image panel in the JLayeredPane,

the background image does not appear.

publicclass UIMainPanelextends javax.swing.JLayeredPaneimplements MouseListener,MouseMotionListener{

privateint nodeSizeX, nodeSizeY;

private BufferedImage roomImage;

private JLabel baseNodeLabel;

private JLabel locNodeLabel;

public UIMainPanel(){

initValues();

initComponents();

addMouseListener(this);

addMouseMotionListener(this);

}

privatevoid initValues()

{

nodeSizeX = 32;

nodeSizeY = 32;

}

privatevoid initComponents(){

setPreferredSize(new java.awt.Dimension(600, 480));

String roomImageName ="room.jpg";

String baseNodeImageName ="node_base.png";

String locNodeImageName ="node_loc.png";

java.net.URL roomImageUrl = this.getClass().getResource(roomImageName);

java.net.URL baseNodeImageUrl = this.getClass().getResource(baseNodeImageName);

java.net.URL locNodeImageUrl = this.getClass().getResource(locNodeImageName);

try{

roomImage = javax.imageio.ImageIO.read(new java.io.File("room.jpg"));

}catch (IOException e){

e.printStackTrace();

}

ImagePanel roomImagePanel =new ImagePanel(roomImage);

Image baseNodeImage =new ImageIcon(baseNodeImageUrl).getImage().getScaledInstance(nodeSizeX,nodeSizeY,Image.SCALE_DEFAULT);

Image locNodeImage =new ImageIcon(locNodeImageUrl).getImage().getScaledInstance(nodeSizeX,nodeSizeY,Image.SCALE_DEFAULT);

baseNodeLabel =new JLabel(new ImageIcon(baseNodeImage) );

locNodeLabel =new JLabel(new ImageIcon(locNodeImage) );

roomImagePanel.setBounds(0,0,600,400);

add(roomImagePanel,JLayeredPane.PALETTE_LAYER);

baseNodeLabel.setBounds(10,10,32,32);

locNodeLabel.setBounds(80,80,32,32);

add(baseNodeLabel,JLayeredPane.PALETTE_LAYER);

add(locNodeLabel,JLayeredPane.PALETTE_LAYER);

}

}

class UIMainPanel is child class of JLayeredPane.

If roomImagePanel.setBounds(0,0,600,400) is called,

the image is appeared with incorrect size.

If roomImagePanel.setBounds(0,0,600,400) is not called,

there is no background image!!

But JLabel object (baseNodeLabel, locNodeLabel) is shown correctly.

I need to resizable background image panel.

Is it impossible?

publicclass ImagePanelextends JPanel{

private BufferedImage image;

public ImagePanel(BufferedImage image)

{

this.image = image;

setLayout(new BorderLayout());

}

protectedvoid paintComponent(Graphics g)

{

super.paintComponent(g);

if (image ==null )return;

Dimension d = getSize();

g.drawImage(image, 0, 0, d.width, d.height,null);

}

}

Here is the code of simplified imagepanel class.

According to the debugging, it seems that paintComponent() method is never called when the imagepanel class is included in the JLayeredPAne class.

[5601 byte] By [dorado2a] at [2007-10-2 21:17:25]
# 1

Hi,

I didn't read your code, because I haven't much time and a different approach anyway: Use a JLabel instead of Panel (extends Container and can draw Icons).

I extended JLabel to provide zooming strategies:

package backgroundimagetest;

import javax.swing.JButton;

import javax.swing.SwingConstants;

import org.softsmithy.lib.swing.icon.FullZooming;

import org.softsmithy.lib.swing.icon.XIcon;

import org.softsmithy.lib.swing.icon.XImageIcon;

public class ImageFrame extends javax.swing.JFrame {

/** Creates new form ImageFrame */

public ImageFrame() {

initComponents();

XIcon icon1 = new XImageIcon("<your image>");

jXIconLabel1.setXIcon(icon1);

jXIconLabel1.setHorizontalAlignment(SwingConstants.CENTER);

jXIconLabel1.setZoomingStrategy(FullZooming.RESPECTING_ASPECT_RATIO_INSTANCE);

JButton button = new JButton("test");

button.setLocation(100, 100);

button.setSize(100, 50);

jXIconLabel1.add(button);

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

private void initComponents() {

jXIconLabel1 = new org.softsmithy.lib.swing.JXIconLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

getContentPane().add(jXIconLabel1, java.awt.BorderLayout.CENTER);

pack();

}// </editor-fold>

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new ImageFrame().setVisible(true);

}

});

}

// Variables declaration - do not modify

private org.softsmithy.lib.swing.JXIconLabel jXIconLabel1;

// End of variables declaration

}

The only problem I encountered was that NetBeans' Matisse doesn't seem to support JLabels as a Container -> you have to do the layout manually.

For more information about my extension (it's open source) see:

http://forum.java.sun.com/thread.jspa?threadID=722687

-Puce

Pucea at 2007-7-14 0:25:56 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks for your reply.

I already tested JLabel with ImageIcon.

Also, I tested your XImageIcon.

However, the problem remains.

The main problem is that if the size (by setBounds()) is not defined earlier, the background image does not appear.

And if the size is fixed before drawing, the background image appears correctly, but the image is not resized when the size of the whole application is changed.

jXIconLabel1 = new org.softsmithy.lib.swing.JXIconLabel();

add(jXIconLabel1, JLayeredPane.DEFAULT_LAYER);

XIcon icon1 = new XImageIcon("room.jpg");

jXIconLabel1.setXIcon(icon1);

jXIconLabel1.setHorizontalAlignment(SwingConstants.CENTER);

jXIconLabel1.setZoomingStrategy(FullZooming.RESPECTING_ASPECT_RATIO_INSTANCE);

dorado2a at 2007-7-14 0:25:56 > top of Java-index,Desktop,Core GUI APIs...
# 3
> add(jXIconLabel1, JLayeredPane.DEFAULT_LAYER);Why do you still use layers? Use a LayoutManager as BorderLayout or GridBagLayout for the parent of jXIconLabel, so the label resizes with the
Pucea at 2007-7-14 0:25:56 > top of Java-index,Desktop,Core GUI APIs...