T1m1 T1m1 - 8 months ago 22
Java Question

JPanel and JFrame size not changing

I'm making a game in Java and first I didn't use a JPanel which caused flickering on repaint() and so I decided to use it. I'm not sure how to implement it in my current code. When I tried to do so all I got was a window that was as small as it gets. My original Window class code:

public class Window extends JFrame {
private double stepLen;

public Window(double stepLen) {
this.stepLen = stepLen;

this.setSize(800, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setTitle("Frogger");
this.setLayout(null);

getContentPane().setBackground(Color.black);

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int x = (dim.width - this.getSize().width)/2;
int y = (dim.height - this.getSize().height)/2;

this.setLocation(x, y);

JLabel goal = new JLabel();
goal.setText("|=========|");
goal.setForeground(Color.WHITE);
goal.setFont(new Font("Seif", Font.PLAIN, 20));
add(goal);
goal.setBounds(325, -10, 600, 50);

setFocusable(true);
requestFocusInWindow();

this.setVisible(true);
}


This code works and it creates a window.
Main class:

Window window = new Window(50);


And then I tried to do it this way:
I have separate GameFrame (JFrame) and GameCanvas (JPanel) classes.
The Frame looks like this:

public class GameFrame extends JFrame{
private double stepLen;

public GameFrame() {
this.stepLen = 50;

this.setSize(800, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setTitle("Frogger");
this.setLayout(null);
this.setVisible(true);

this.getContentPane().setBackground(Color.black);

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int x = (dim.width - this.getSize().width)/2;
int y = (dim.height - this.getSize().height)/2;

GameCanvas gcanvas = new GameCanvas();
this.add(gcanvas);
this.pack();

this.setLocation(x, y);

}

}


}


And the GameCanvas class

public class GameCanvas extends JPanel {
public GameCanvas() {
setDoubleBuffered(true);
JLabel goal = new JLabel();
goal.setText("|=========|");
goal.setForeground(Color.WHITE);
goal.setFont(new Font("Seif", Font.PLAIN, 20));
this.add(goal);
goal.setBounds(325, -10, 600, 50);

this.getPreferredSize();

this.setVisible(true);

this.repaint();
}

@Override
public void paintComponent(Graphics g) {

int firstRoad = 5;
int i = 0;
int max = 10;

Graphics2D g2 = (Graphics2D) g;

super.paintComponent(g2);
g2.setColor(Color.WHITE);
g2.drawRect(5, 30, 75, 40);

while(i < max) {
g2.setColor(Color.WHITE);
g2.setStroke(new BasicStroke(3));

if(i % 2 == 0) {
g.setColor(Color.WHITE);
g.drawRect(3, firstRoad + 50 * i, 793, 50);
//g.fillRect(3, firstRoad + 50 * i, 793, 50);
} else {
g2.setColor(Color.WHITE);
g2.drawRect(3, firstRoad + 50 * i, 793, 50);
}
i++;
}
}
}

Answer Source

Camickr is correct - go up vote and mark his answer as correct, this is only here to save him from pulling out what little hair he has remaining

  • You're using a null layout, without taking over its responsibility
  • Failed to provide sizing hints to for the component to allow the layout manager (which you're no longer using) to do it's job

This are all common mistakes, to which there are countless answers already provided

GameFrame

public class GameFrame extends JFrame {

    private double stepLen;

    public GameFrame() {
        this.stepLen = 50;

        this.setSize(800, 600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setTitle("Frogger");
        // Well, there's your problem...
        //this.setLayout(null);
        // Don't do this here...
        this.setVisible(true);

        this.getContentPane().setBackground(Color.black);

        // Simpler way to achieve this
        //Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        //int x = (dim.width - this.getSize().width) / 2;
        //int y = (dim.height - this.getSize().height) / 2;

        GameCanvas gcanvas = new GameCanvas();
        this.add(gcanvas);
        this.pack();

        //this.setLocation(x, y);

        setLocationRelativeTo(null);
        setVisible(true);

    }

}

GameCanvas

public class GameCanvas extends JPanel {

    public GameCanvas() {
        // Pointless
        //setDoubleBuffered(true);
        JLabel goal = new JLabel();
        goal.setText("|=========|");
        goal.setForeground(Color.WHITE);
        goal.setFont(new Font("Seif", Font.PLAIN, 20));
        this.add(goal);
        // Pointless
        //goal.setBounds(325, -10, 600, 50);

        // Pointless
        //this.getPreferredSize();
        // Pointless
        //this.setVisible(true);
        // Pointless
        //this.repaint();
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 400);
    }

    @Override
    public void paintComponent(Graphics g) {

        int firstRoad = 5;
        int i = 0;
        int max = 10;

        Graphics2D g2 = (Graphics2D) g;

        super.paintComponent(g2);
        g2.setColor(Color.WHITE);
        g2.drawRect(5, 30, 75, 40);

        while (i < max) {
            g2.setColor(Color.WHITE);
            g2.setStroke(new BasicStroke(3));

            if (i % 2 == 0) {
                g.setColor(Color.WHITE);
                g.drawRect(3, firstRoad + 50 * i, 793, 50);
                //g.fillRect(3, firstRoad + 50 * i, 793, 50);
            } else {
                g2.setColor(Color.WHITE);
                g2.drawRect(3, firstRoad + 50 * i, 793, 50);
            }
            i++;
        }
    }
}