I don't understand why the picture painted on my background isn't resized on JFrame resize.
Basically I create a JFrame and a JPanel inside this frame.
Then I set my background picture (animated gif) with the method "setBackground" that creates an object of another class. In this new class called "ImagePanel" I draw the picture and paint another smaller picture over the background picture. The result is ok unless I resize the JFrame.
The background picture "follows" the resize, while the smaller picture remains at the beginning point. I draw both the pictures with the paintComponent(), and I set the dimension of the pictures on the base of the Width and Height of the JFrame, but it doesn't seem to work.
I added an action listener too, to listen to the resize on the JFrame. When it listens the resize it calls "repaint()", but it doesn't work (I also tried "revalidate()" both on the JFrame and the JPanel without success).
It seems that when you enlarge the JFrame almost covering all the screen it has an update, on a fixed point the smaller picture is "updated" but in a wrong way, and only if the JFrame is very large.
Here is the code. The "GameWindow" class:
import java.awt.BorderLayout;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class GameWindow extends JFrame {
private JPanel mapPicture = new JPanel();
public GameWindow(){
super("WELCOME!!!");
setSize(768, 768);
addComponentListener(new ResizeListener(this));
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setBackground();
setVisible(true);
}
private void setBackground() {
mapPicture = new ImagePanel("map.gif", this);
add(mapPicture, BorderLayout.CENTER);
mapPicture.setLayout(new BorderLayout());
}
public class ResizeListener implements ComponentListener {
private JFrame parentFrame;
public ResizeListener(JFrame parentFrame) {
this.parentFrame = parentFrame;
}
public void componentHidden(ComponentEvent e) {}
public void componentMoved(ComponentEvent e) {}
public void componentShown(ComponentEvent e) {}
public void componentResized(ComponentEvent e) {
repaint();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GameWindow2();
}
});
}
}
And here is the "ImagePanel" class:
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImagePanel extends JPanel {
private ImageIcon background;
private JFrame parentFrame;
private ImageIcon smallerImage = new ImageIcon(getClass().getResource("car.png"));
public ImagePanel(String imgPath, JFrame parentFrame) {
this.parentFrame = parentFrame;
background = new ImageIcon(getClass().getResource(imgPath));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawImage(background.getImage(), 0, 0, getWidth(), getHeight(), this);
g2d.drawImage(smallerImage.getImage(), 119*((parentFrame.getWidth()/768)), 172*((parentFrame.getHeight())/768), 32*((parentFrame.getWidth())/768), 32*((parentFrame.getHeight())/768),this);
}
}
I don't understand why the smaller picture isn't redrawn with the new proportions.
PS: I can't use a layout manager to set the position of the second picture, because I will have a lot of objects (in the future) to put in a casual way on the map (and possibly animate them).
it is because you are using integer arithmetic and dividing before multiplying :119*(parentFrame.getWidth()/768)
instead you floating point arithmetic and cast to an int : (int)(119.0*(parentFrame.getWidth()/768.0))