Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lock aspect ratio of a gridLayout in Java?

Is there an easy way of locking the aspect ratio of a GridLayout component in Java Swing ? Or should this be done on the JPanel containing that layout ?

like image 936
BuZz Avatar asked Mar 25 '12 02:03

BuZz


People also ask

How do I change the size of GridLayout in Java?

Put your GridLayout on its own JPanel, and then you can use panel. setSize(x,y) to change the panel size and thus increase or decrease the size of the cells.

What does GridLayout do in Java?

Creates a grid layout with the specified number of rows and columns. All components in the layout are given equal size. One, but not both, of rows and cols can be zero, which means that any number of objects can be placed in a row or in a column.

Which is the correct a constructor of GridLayout?

Constructors of GridLayout class GridLayout(): creates a grid layout with one column per component in a row. GridLayout(int rows, int columns): creates a grid layout with the given rows and columns but no gaps between the components.

How do I change the layout of a panel in Java?

You can set a panel's layout manager using the JPanel constructor. For example: JPanel panel = new JPanel(new BorderLayout()); After a container has been created, you can set its layout manager using the setLayout method.


1 Answers

GridLayout effectively ignores a component's preferred size, but you can control the aspect ratio of whatever is drawn in paintComponent(), as shown in this example. The rendered shape remains circular (1:1 aspect), while (nearly) filling the container in the narrowest dimension. Resize the frame to see the effect.

Addendum: For example, I added N * N instances of CirclePanel to a GridLayout below.

enter image description here

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

/**
 * @see https://stackoverflow.com/a/9858355/230513
 * @see https://stackoverflow.com/a/3538279/230513
 */
public class SwingPaint {

    private static final int N = 4;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setLayout(new GridLayout(N, N));
                for (int i = 0; i < N * N; i++) {
                    frame.add(new CirclePanel());
                }
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    private static class CirclePanel extends JPanel {

        private static final Random r = new Random();

        public CirclePanel() {
            this.setPreferredSize(new Dimension(80, 80));
            this.setForeground(new Color(r.nextInt()));
            this.addMouseListener(new MouseAdapter() {

                @Override
                public void mousePressed(MouseEvent e) {
                    CirclePanel.this.update();
                }
            });
        }

        public void update() {
            this.setForeground(new Color(r.nextInt()));
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Dimension size = this.getSize();
            int d = Math.min(size.width, size.height) - 10;
            int x = (size.width - d) / 2;
            int y = (size.height - d) / 2;
            g.fillOval(x, y, d, d);
            g.setColor(Color.blue);
            g.drawOval(x, y, d, d);
        }
    }
}
like image 175
trashgod Avatar answered Nov 11 '22 12:11

trashgod