-1

Im trying to add an image to a JLabel and i have encountered a problem where the image is to big for the JLabel so I'm looking for a way to make the image automatically scale to the size of the JLabel.

Here is my main body of code where i call up my GUImain class:

public class Main 
{
    public static void main(String[] args)
    {
        int i = 0;
        int t = 0;
        int st = 0;
        int h = 0;

        Texts textObject = new Texts();
        textObject.TextList();

        Commands commandObject = new Commands();
        commandObject.commands();

        GUImain guiObject = new GUImain();
        guiObject.displayGUI();
    }
}

And here is the code for my GUImain class, i will only include one image button because there are allot of objects in my code.

import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JTextPane;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JScrollBar;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

    public class GUImain 
    {
        private JFrame frame;
        private JTextField textField;
        private ImageIcon image1;

    public void displayGUI() 
    {
        this.frame.setVisible(true);
    }

    //Launch the application.
    public static void main(String[] args)
    {
        GUImain window = new GUImain();
    }

    //Create the application.
    public GUImain() 
        {
        frame = new JFrame();
        frame.setBounds(100, 100, 611, 471);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        ...

        JLabel lblHunger = new JLabel();
        Image img2 = new ImageIcon(this.getClass().getResource("/Food.png")).getImage();
        lblHunger.setIcon(new ImageIcon(img2));
        lblHunger.setBounds(211, 58, 50, 50);
        panel.add(lblHunger);
        }
    }
}
Tom T
  • 59
  • 1
  • 1
  • 12
  • It's `JLabel`, not `JLable`, and I'd instead display my image not in a JLabel but rather within a JPanel's paintComponent method, using the Graphics object's `drawImage(...)` method to stretch or shrink my image. – Hovercraft Full Of Eels Dec 05 '14 at 19:54
  • Sorry I'm quite new to java, so am a learner how would i implement that in my code? – Tom T Dec 05 '14 at 20:06
  • This has been asked many times before. Please Google or search this site -- drawing image in JPanel. I've closed the question with one such answer. – Hovercraft Full Of Eels Dec 05 '14 at 20:11

1 Answers1

2

Don't use a null layout!!!

Swing was designed to be used with layout managers. Let each component determine its preferred size and then let the layout manager size and position the component based on the rules of the layout manager.

Instead of doing custom painting you can use Darry'ls Stretch Icon. The Icon will be painted based on the space available to the icon.

i have encountered a problem where the image is to big for the JLabel

Just reread that part of the question. In that case you may want to use the Image.getScaledInstance(...) method to scale the image to an appropriate size. The you can use a regular Icon and the appropriate layout manager.

what layout would i be able to use to easily choose the dimensions and where the objects are positioned

You are not supposed to choose the size/location of a component. That is the job of the layout manager. Read the tutorial on Layout Managers and choose the one (or combination of layout managers) that is appropriate for your requirements.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Chit, before I saw your answer, I closed the question as duplicate with a link to one of your answers. Coincidence? Too strange! 1 + – Hovercraft Full Of Eels Dec 05 '14 at 20:12
  • Ok, what layout would i be able to use to easily choose the dimensions and where the objects are positioned with out being restricted by things like grids from GridBagLayout? – Tom T Dec 05 '14 at 20:13
  • I looked at what layout I'm using and I'm using the absolute layout which allows me to set absolute values to the location and dimension of my components. – Tom T Dec 05 '14 at 20:18
  • @TomT, AbsoluteLayout is not really a layout. Again you should NOT be using absolute values to determine the location and size of components. Each component may have a slightly different LAF on different platforms. – camickr Dec 05 '14 at 20:24
  • @HovercraftFullOfEels, good coincidence but I may have been a little to quick to answer. The question is not really about dynamically resizing an image, but containing the maximum size of an image. Anyway all these answers give the OP some ideas. – camickr Dec 05 '14 at 20:26
  • so what layout/layouts would be appropriate if I'm doing a text based game with a gui which looks like this: http://imgur.com/jjfKPCj – Tom T Dec 05 '14 at 20:28
  • @TomT, we don't have enough information to make a suggestion because we don't know how components should be resized as the size of the frame is changed. In any GUI some components will remain at a fixed size and other will change size based on the space available. That is why you need to first learn the basics of each layout manager so you can decide how to nested panels with different layout manager to allocate space properly. – camickr Dec 05 '14 at 20:41