Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8999631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:12:20+00:00 2026-06-16T00:12:20+00:00

I’m having what seems to me should be a really simple issue. In my

  • 0

I’m having what seems to me should be a really simple issue. In my opinion it’s just a massive oversight of the people who made GridBagLayout…

Anyway. I’m using GridBagLayout to display a 27×13 grid of tiles for the game I’m making. I used this layout because of its ability to resize the components and because of how easy it is to configure, but there’s just one small problem. When it’s resizing the tiles, if the width is not a multiple of 27 or if the height is not a multiple of 13, there will be white space put around the borders.

To show what I mean:

Here is what it looks like when I resize the frame so that the JPanel within is sized 864×416, perfect multiples of 27 and 13.

Here is what it looks like when I resize the frame so that the JPanel within is sized 863×415, just barely not multiples of 27 or 13.

It simply is not distributing the extra pixels among the tiles. I don’t know why. When I fiddle with the minimum/maximum/preferred size using their respective methods or overriding them, or even use GridBagLayout‘s ipadx and ipady constraints, I can remove the white space – but all it does is just squish the outermost tiles to fit the rest. You can see this in the example code below.

Here’s an SSCCE:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Random;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends JPanel {
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                start();
            }
        });
    }
    static void start() {
        JFrame frame = new JFrame("Game");
        JPanel newFrame = new MainGameScreen();
        frame.getContentPane().add(newFrame);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
class MainGameScreen extends JPanel {
    public MainGameScreen() {
        setPreferredSize(new Dimension(864, 551));
        setLayout(new GridBagLayout());
        setBackground(Color.green);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.ipadx = 0; //Change to 64 to see undesired effects
        gbc.ipady = 0; //^
        for (int i=0;i<13;i++) {
            for (int j=0;j<27;j++) {
                gbc.gridx = j;
                gbc.gridy = i;
                add(new ImagePanel(), gbc);
            }
        }
    }
}
class ImagePanel extends JComponent { 
    private int r,g,b;
    public ImagePanel() {
        Random generator = new Random();
        r = generator.nextInt(100)+1;
        g = generator.nextInt(100)+1;
        b = generator.nextInt(100)+1;
    }
    @Override
    public void paintComponent(Graphics gr) {
        super.paintComponent(gr);
        gr.setColor(new Color(r,g,b));
        gr.fillRect(0, 0, getWidth(), getHeight());
    }
}

My question is, how can I make the layout constantly look like the first image? Do I need a different layout? I’m very lost here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:12:21+00:00Added an answer on June 16, 2026 at 12:12 am

    I would use a GridLayout for the panel with square tiles, and nest that inside another layout (either GridBagLayout, BorderLayout, BoxLayout, or some combination of layouts) for the rest of the window.

    That said, both GridLayout and GridBagLayout will distribute pixels evenly in this case. Since you can only use whole pixels, you’ll be left with a remainder after dividing up the the space, and that will be used as padding. This is largely unavoidable, although there are a few potential workarounds:

    • enforce certain window sizes
    • draw a larger panel than you need, but use a viewport so the extra tiles are truncated in the view
    • add other elements bordering the tiled area to effectively hide the fact that there’s extra padding

    If you’re okay with the first option, you can make a slight modification to snap the window to a compatible size, such that there is no remainder (note: I also changed a couple of your hard-coded ints to constants):

    package com.example.game;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ComponentAdapter;
    import java.awt.event.ComponentEvent;
    import java.util.Random;
    
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
        public class Game extends JPanel {      
            public static void main(String[] args) {
                javax.swing.SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        start();
                    }
                });
            }
    
            static void start() {
                final JFrame frame = new JFrame("Game");
                JPanel newFrame = new MainGameScreen();
    
                frame.addComponentListener(new ComponentAdapter() {
                    @Override
                    public void componentResized(ComponentEvent e) {
                        int h = frame.getContentPane().getHeight() % MainGameScreen.ROWS;
                        int w = frame.getContentPane().getWidth() % MainGameScreen.COLS;
    
                        // Subtract the remainder pixels from the size.
                        frame.setSize(frame.getWidth() - w, frame.getHeight() - h);
                    }
                });
                frame.getContentPane().add(newFrame);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        }
        class MainGameScreen extends JPanel {
            static final int ROWS = 13;
            static final int COLS = 27;
    
            public MainGameScreen() {
                setPreferredSize(new Dimension(864, 551));
                setLayout(new GridBagLayout());
                setBackground(Color.green);
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.fill = GridBagConstraints.BOTH;
                gbc.weightx = 1;
                gbc.weighty = 1;
                gbc.ipadx = 0; //Change to 64 to see undesired effects
                gbc.ipady = 0; //^
                for (int i=0;i<ROWS;i++) {
                    for (int j=0;j<COLS;j++) {
                        gbc.gridx = j;
                        gbc.gridy = i;
                        add(new ImagePanel(), gbc);
                    }
                }
            }
        }
    
        class ImagePanel extends JComponent { 
            private int r,g,b;
            public ImagePanel() {
                Random generator = new Random();
                r = generator.nextInt(100)+1;
                g = generator.nextInt(100)+1;
                b = generator.nextInt(100)+1;
            }
            @Override
            public void paintComponent(Graphics gr) {
                super.paintComponent(gr);
                gr.setColor(new Color(r,g,b));
                gr.fillRect(0, 0, getWidth(), getHeight());
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm making a simple page using Google Maps API 3. My first. One marker
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.