Search code examples
javaswingresizegridpaintcomponent

JPanel only shows small portion of graphics


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 12x12 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 12x12 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.


Solution

  • 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!