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.

