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 8683579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:05:00+00:00 2026-06-12T22:05:00+00:00

this is my first post here on stackoverflow, so I hope I’m doing this

  • 0

this is my first post here on stackoverflow, so I hope I’m doing this the right way.

Anyway, I’m trying to create a JPanel inside a JFrame and draw a Grid inside that panel. Therefore, I’ve overridden the paintComponents() method to do that. I’ve tried to set the preferredSize as well as the minimumSize of that panel, but no matter what I do, it shows only a 12×12 portion of that panel.

Here’s my Grid class:

package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

import collections.MapUtils;
import executor.Robot;

public class Grid extends JPanel {
    private static final long serialVersionUID = 1L;
    private int rs = GUI.ROBOT_SIZE;

    private final int width;
    private final int height;
    private final Map<Coord, Color> colors;

    private int rx;
    private int ry;

    public Grid(int width, int height) {
        this.width = width;
        this.height = height;

        colors = MapUtils.hashMap();
        for(int i = 0; i < width; i++)
                for(int j = 0; j < height; j++)
                colors.put(new Coord(i, j), Color.WHITE);

        rx = width/2;
        ry = height/2;
        setPreferredSize(new Dimension(width * rs, height * rs));
        setBorder(BorderFactory.createLineBorder(Color.CYAN));
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
    return height;
}

public void update(Robot robot) {
    this.rx = width/2 + robot.getX();
    this.ry = height/2 + robot.getY();

    repaint();
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    for(Map.Entry<Coord, Color> es : colors.entrySet()) {
        g.setColor(Color.RED);
//          g.setColor(es.getValue());
        Coord c = es.getKey();
        g.fillRect(c.getX()*rs, c.getY()*rs, rs, rs);
    }

    g.setColor(Color.BLACK);
    for(int i = 0; i <= width; i++)
        g.drawLine(i*rs, 0, i*rs, height*rs);
    for(int i = 0; i <= height; i++)
        g.drawLine(0, i*rs, width*rs, i*rs);

    g.setColor(Color.DARK_GRAY);
    g.drawOval(rx*rs, ry*rs, rs, rs);
    g.setColor(Color.LIGHT_GRAY);
    g.fillOval(rx*rs, ry*rs, rs, rs);
}
}

And this is how it looks like (note the 12×12 red square in the top left corner, which should be the grid):
http://imageshack.us/a/img818/3545/49409190.png

The panel itself is resized correctly, but the graphics don’t fill it. I don’t know, maybe I’m overlooking something simple, but right now I don’t know what to do. How can I make it paint the whole grid?

EDIT: Ah now I feel stupid. My mistake was to name the getters ‘getWidth’ and ‘getHeight’ which were overriding the methods from JComponent. After renaming them it is working perfectly fine.

  • 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-12T22:05:01+00:00Added an answer on June 12, 2026 at 10:05 pm

    I figured out what was wrong. I called the getter for the width and height ‘getWidth()’ and ‘getHeight()’ respectively, which overrode the methods inherited from JPanel.
    After renaming them to something different, it worked 🙂
    But thanks for all your replies!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My first post here, so i hope this is the right area. I am
this is my first post here on stackoverflow and am very impressed by the
This is my first post here so go easy. I am trying to build
This is my first time here so I hope I post this question at
this is my first post here on StackOverflow and I'm quite excited about it
This is my first post on StackOverflow, so apologies if it's lacking the right
this is my first post on stackoverflow. I hope my question is not to
This is my first post here on Stackoverflow, though over the years I've been
This is my first post here in StackOverflow. I am not a newbie to
This is my first post here. I have a problem. I need to take

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.