OldLazarus
OldLazarus

Reputation: 71

How to create an ImageIcon with the exact dimensions of a GridBagLayout cell

For a simple GUI I am currently making I want a design similar to this.

enter image description here

The blue and the green area are supposed to be just text and numbers.
The red area is supposed to be an image. Currently, I am creating scaled instances of an Image, create an ImageIcon out of this and then add this to a label to fit an image into different spaces. The problem is, that without a width I can not create a scaled instance of the picture.

My current GridBagLayout code looks like this:

private static void createAndShowUI()
    {
        JFrame frame = new JFrame();
        JLabel map = new javax.swing.JLabel();
        JLabel data = new javax.swing.JLabel();
        JLabel menu = new javax.swing.JLabel();
        GridBagConstraints c;

        frame = new JFrame("Risiko");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

        frame.getContentPane().setLayout(new GridBagLayout());

        map.setText("MAP");
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 0.75;
        c.weighty = 0.75;
        frame.getContentPane().add(map, c);

        data.setText("DATA");
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 1;
        c.weighty = 0.25;
        frame.getContentPane().add(data, c);

        menu.setText("MENU");
        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = 0;
        c.gridheight = 2;
        c.weightx = 0.25;
        frame.getContentPane().add(menu, c);

        frame.setVisible(true);
    }

I create the three areas and now I want to create an image with the exact width and height of the red area.
So my question is, how can I get the width and height of the red area so I can create a scaled instance of the picture so that it fits into this area?

Upvotes: 0

Views: 97

Answers (1)

c0der
c0der

Reputation: 18812

Test the dynamic layout of the following mre by resizing the frame. The background image used as background for MapPane is resized to fill the JPanels width and height.
This is achieved by overriding paintComponent:

import java.awt.*;
import java.awt.image.*;
import java.net.URL;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

public class SwingTestPane extends JPanel {

    public SwingTestPane() {

          GridBagLayout gridBagLayout = new GridBagLayout();
          gridBagLayout.rowWeights = new double[]{0.75, .25};
          gridBagLayout.columnWeights = new double[]{0.75, 0.25};
          setLayout(gridBagLayout);

          GridBagConstraints c = new GridBagConstraints();
          c.fill = GridBagConstraints.BOTH;
          c.gridx = 0;
          c.gridy = 0;

          JPanel mapPane = new MapPane();
          add(mapPane, c);

          c.gridx = 0;
          c.gridy = 1;

          JLabel data = new javax.swing.JLabel("DATA");
          JPanel dataPane = new JPanel();
          dataPane.add(data);
          dataPane.setBackground(Color.YELLOW);
          dataPane.setOpaque(true);
          add(dataPane, c);

          c.gridx = 1;
          c.gridy = 0;
          c.gridheight = 2;

          JLabel menu = new JLabel("MENU");
          JPanel menuPane = new JPanel();
          menuPane.add(menu);
          menuPane.setBackground(Color.GREEN);
          menuPane.setOpaque(true);
          add(menuPane, c);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400,400);
    }
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.getContentPane().add(new SwingTestPane());
        frame.pack();
        frame.setVisible(true);
    }
}

class MapPane extends JPanel {

    String imageUrlString = "https://findicons.com/files/icons/345/summer/128/cake.png";
    BufferedImage image = null;
    MapPane() {
        URL url;
        try {
            url = new URL(imageUrlString);
            image = ImageIO.read(url);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @Override //Override to paint image as the background
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
    }
} 

Upvotes: 1

Related Questions