Reputation: 47
The problem with my code is that my image only gets the beginning size of the JFrame
- but I want it to change size every time the user changes the size of the window.
Also should the image size be relative to JFrame
size or its JPanel
? If so, how would I achieve that?
Here is my code:
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
public class MainFrame extends JFrame {
private JPanel card1;
private JPanel principalPanel,centerPanel;
private BufferedImage image;
public MainFrame() {
setSize(800,600);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
URL url = null;
try {
url = new URL("https://i.sstatic.net/7bI1Y.jpg");
} catch (MalformedURLException e) {
}
try {
image = ImageIO.read(url);
} catch (IOException e) {
System.out.println("Can not find image");
}
Image scaledImage = image.getScaledInstance(this.getWidth(),
this.getHeight(),Image.SCALE_SMOOTH);
card1 = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(scaledImage,0,0,this);
}
};
centerPanel = new JPanel();
centerPanel.setLayout(new CardLayout());
centerPanel.add(card1,"1");
principalPanel = new JPanel();
principalPanel.setLayout(new BorderLayout());
principalPanel.add(centerPanel,BorderLayout.CENTER);
setContentPane(principalPanel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MainFrame();
}
});
}
}
Upvotes: 0
Views: 46
Reputation: 168815
The image could (indeed should) be scaled in the paintComponent(Graphics)
method. That way it can change to whatever size the panel is.
See also the comments in the code:
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
public class MainFrame2 extends JFrame {
private JPanel card1;
private BufferedImage image;
public MainFrame2() {
setSize(800, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
try {
// if there is a problem with URL - no point trying to load
URL url = new URL("https://i.sstatic.net/7bI1Y.jpg");
image = ImageIO.read(url);
} catch (Exception e) {
System.out.println("Problem loading image");
e.printStackTrace();
}
/* The frame will actually be larger than the panel.
Better to scale the image to the SIZE OF THE PANEL ...
Image scaledImage = image.getScaledInstance(this.getWidth(),
this.getHeight(), Image.SCALE_SMOOTH); */
card1 = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// .. this is how.
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
};
setContentPane(card1);
setVisible(true); // this should be last!
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MainFrame2();
}
});
}
}
Upvotes: 1