I'm working on a Java GUI and I have a JFrame thats 600x800 that I want to split into two portions, the top which has checkboxes for settings and is 600 x 200 and the bottom which is the "canvas" which will be painted on that is 600x600. I have a JFrame that has a JPanel that contains two other JPanels (one for the settings, the other for the canvas). I'm testing the "painting" and for some reason when I try to paint a 600x600 black rectangle, it paints a much smaller rectangle in the area that the settings JPanel occupies. Any ideas what's going on? I'm using the repaint method to call paintComponent and I've tried calling repaint in both the constructor of my JFrame and in the JPanel that is to be painted.
HPAProgram
public class HPAProgram {
public static void main(String[] args) {
MapWindow map = new MapWindow();
}
}
MapWindow
//import java.awt.*;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.*;
import javax.swing.*; //notice javax
public class MapWindow extends JFrame
{
private static final int WIDTH = 600, HEIGHT = 800;
JPanel panel = new JPanel();
SettingsButtonsPanel button_panel = new SettingsButtonsPanel();
MapImagePanel map_panel = new MapImagePanel();
public MapWindow()
{
setLocationRelativeTo(null);
setTitle("HPA* Test");
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(panel);
panel.setBounds(0, 0, 600, 800);
//panel.setLayout(new GridBagLayout());
/*GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;*/
panel.add(button_panel);
/*c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;*/
panel.add(map_panel);
button_panel.setLocation(0,0);
/*map_panel.setLocation(0,200);
map_panel.setSize(600,600);*/
map_panel.repaint();
}
}
SettingsButtonsPanel
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class SettingsButtonsPanel extends JPanel implements ItemListener{
private static final int WIDTH = 600, HEIGHT = 200;
private static final int NUM_MAP_TYPE = 2;
private JCheckBox[] map_type;
JPanel panel = new JPanel();
public SettingsButtonsPanel(){
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
this.setBounds(0,0,WIDTH, HEIGHT);
map_type = new JCheckBox[NUM_MAP_TYPE];
map_type[0] = new JCheckBox("Sparse");
map_type[0].setSelected(true);
//map_type[0].setSize(100,100);
map_type[1] = new JCheckBox("Maze");
map_type[1].setSelected(false);
//map_type[1].setSize(100,100);
for(int i = 0; i < NUM_MAP_TYPE; i++)
{
map_type[i].addItemListener(this);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = i;
this.add(map_type[i], c);
}
}
public void itemStateChanged(ItemEvent e)
{
Object source = e.getItemSelectable();
//if(source == )
}
}
MapImagePanel
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class MapImagePanel extends JPanel{
public MapImagePanel()
{
this.setBounds(0,200, 600,600);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(0,0,600,600);
}
}