-2

I added an image like this

BufferedImage eastFramerPicture = ImageIO.read(new File("src\\Images\\farmer.png"));
        JLabel eastFarmer = new JLabel(new ImageIcon(eastFramerPicture));

but how can I make the image fill the panel, the image provided shows the problem that I am facing ( the colouring is used to indicate the regions of East, West, Center and south )

enter image description here.

What am I doing wrong ? Please if something is not clear comment it out, I will reply.

Thanks for your time

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Samer34
  • 13
  • 6
  • Do you want to maintain the aspect ratio of the image? Something [like this](http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928) for example? Or even [something like this](http://stackoverflow.com/questions/12876615/how-do-i-resize-images-inside-an-application-when-the-application-window-is-resi/12876799#12876799) – MadProgrammer Feb 21 '17 at 03:11
  • I just want the image to fill the panel ( in the image provide the east ( which is grass should fill the whole green colour area ). I don't mind it get stretched – Samer34 Feb 21 '17 at 03:18
  • Also, don't reference the `src` path, it won't exist when the project is packaged. In resource contained within the `src` package, is likely to embedded within the resulting jar file, making it inaccessible as a `File`, you will need to use `Class#getResource` instead – MadProgrammer Feb 21 '17 at 03:18
  • Thanks for the link but it is complicated for me – Samer34 Feb 21 '17 at 03:19
  • You're not going to have a solution that isn't "complicated" as the default API doesn't do what you want. The only solution you have is to head down the custom painting route, which means you're going to run into issues with scale quality. Another solution would be to title the image instead – MadProgrammer Feb 21 '17 at 03:20
  • I see, Can you please explain what you mean by title the image instead ? – Samer34 Feb 21 '17 at 03:22
  • @MadProgrammer I have tried to do this WaterPicture.getScaledInstance(600, 600, Image.SCALE_FAST); but it didn't work. Do you have any idea why is that ? – Samer34 Feb 21 '17 at 03:25
  • A [tiling example](http://stackoverflow.com/questions/23403707/how-to-set-repeating-background-image-to-a-jpanel/23403786#23403786) - Basically, this paints the image repeatedly so it covers the entire panel, but doesn't require it to scale the image – MadProgrammer Feb 21 '17 at 03:28
  • Without greater context to the code, it's difficult to know why something might not work, but remember, `getScaledInstance` will return a NEW image, it won't affect the original – MadProgrammer Feb 21 '17 at 03:33
  • ooh I didn't know that. So after using getScaledInstance , I need to store it in a variable. But what is the type of the variable ? – Samer34 Feb 21 '17 at 03:37
  • never mind, thanks so much – Samer34 Feb 21 '17 at 03:41
  • IT WORKED :D If you want wright your comment as a solution and I will choose it so that you get extra score ;) – Samer34 Feb 21 '17 at 03:42
  • Note that editing titles to ask for posts to be deleted is regarded as vandalism here, and creates work for other people to fix. If the system won't let you delete a question cleanly then edit the question to improve it please. – halfer Mar 24 '17 at 09:02

3 Answers3

1

Check out the Background Panel. You can display an image:

  1. at its normal size
  2. scaled
  3. tiled

So try each option to see what you like best.

Or you could also use the Stretch Icon. The Icon in the label will be scaled to fill the available space.

Both solutions provide reusable code so you don't need to write custom code every time you have this type of requirement. The code is also dynamic and will adjust as the frame is resized.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • can you please give an example of how to use the Stretch class ? I passed the image to it and it did nothing – Samer34 Feb 21 '17 at 04:19
1

The answer will depend on a number of factors...

  • Do you want maintain the aspect ratio?
  • Do you want tile the image to fill the area
  • If you want to maintain the aspect ratio, do you want to fit or fill the available space.

The simple solution would be to use Image#getScaledInstance, but if you go that route, you should have a read of The Perils of Image.getScaledInstance

BufferedImage eastFramerPicture = ImageIO.read(getClass().getResource("/Images/farmer.png"));
Image scaled = eastFramerPicture.getScaledInstance(600, 600, Image.SCALE_SMOOTH);

You could also make use of a library like imgsclr

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
-1

Try to use setBounds(x,y,width,height);

ARLEQUINA
  • 129
  • 1
  • 1
  • 11
  • I tried eastGrass.setBounds(east.getLocation().x, center.getLocation().y, center.getWidth(), center.getHeight()); But it did not work, any ideas ? – Samer34 Feb 21 '17 at 02:44
  • public usersMenu() { con = MySQLConnect.ConnectDB(); initComponents(); fillList(); jPanel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/muis.jpg"))); setResizable(false); Dimension dm = Toolkit.getDefaultToolkit().getScreenSize(); setBounds(0,0,dm.width,dm.height); jPanel1.setBounds(0,0, dm.width, dm.height); setTitle("МОНГОЛ УЛСЫН ИХ СУРГУУЛЬ | ТЕСТИЙН СИСТЕМ 1.0 | Хэрэглэгч хэсэг"); //jPanel1.setBounds(dm.width/4, dm.height/4, dm.width/8, dm.height/8); } – ARLEQUINA Feb 21 '17 at 03:52