How to fit Image size to JFrame Size?

image, java, jframe, jpanel, swing

Solution

First of all, I wouldn't be loading the image inside the `paintComponent` method, this method is call repeatedly (and some times in quick succession), you don't want to do anything that takes time to execute or consumes resources unnecessarily

Check out Java: maintaining aspect ratio of JPanel background image for suggestions on filling/fitting images to a given area

Problem

I have a `JPanel` into a `JFrame`. I loaded a picture on the `JPanel` but its shown just a part of the picture: This is the part of the code where i did it: ``` JPanel panelImg = new JPanel() { public void paintComponent(Graphics g) { Image img = new ImageIcon("Welcome.png").getImage(); Dimension size = new Dimension(img.getWidth(null), img.getHeight(null)); setPreferredSize(size); setMinimumSize(size); setMaximumSize(size); setSize(size); setLayout(null); g.drawImage(img, 0, 0, null); } }; mainFrame.add(panelImg); ``` So this is how it looks like: The complete picture looks like this: Is there a way to scale the picture to the `JFrame`s size? Thanks in advance

Original source

Related problems