JPanel hiden behind JFrame decoration

Hi, I disabled all Swing repaiting and added my own Jpanel with will do all painting with active rendering.

Now I’m trying to place this JPanel inside my JFrame. BUT
When I set the JPanel as contentPane or add it in the center then a part of the Panel is hiden by the frame decorations. How can I force the Jpanel to display totaly, without a part being hiden.
I know one solution: create a top cst make it 10 pixels and everytime i paint add it to the y var. But then again… that isn’t a ‘real’ solution.

I would like to be 0,0 the left top of my Jpanel and not 0,0 hiden behind the decoration…

I know about getInsets(); but I don’t know how that solves the problem.

Frame:

public class GameWindow extends JFrame {
  private JPanel gamePanel;

  public GameWindow(String title, GraphicsConfiguration gc, Dimension dimension) {
    super(title, gc);
    initFrame(dimension);
    initComponents();
    add(gamePanel, BorderLayout.CENTER);  // Ellements automatic resize to fit the Frame.
    pack();
  }

  private void initFrame(Dimension dimension) {
    setResizable(false);
    setIgnoreRepaint(true);
    setPreferredSize(dimension);
    setFocusTraversalKeysEnabled(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    ((JComponent) getContentPane()).setOpaque(false);
  }

Panel:

  public GamePanel() {
    super(false);
    initPanel();
  }

  private void initPanel() {
    setIgnoreRepaint(true);
    setFocusTraversalKeysEnabled(false);
  }

please advise

What I do is to set the size of the panel to (widht, height) and the size of the frame to (width + insets.left + insets.right, height + insets.top + insets.bottom). If you want you can do it the otherway round (setting the frame to (width, height) and subtracting the frame-insets from the panel-size). Hope that helps :slight_smile:

add(gamePanel, BorderLayout.CENTER);  // Ellements automatic resize to fit the Frame.

should be replaced by getContentPane().add(gamePanel, BorderLayout.CENTER), indeed the frame contentpane layout is overlapping the contentpane…
And as for going to FS mode, a Window instance can always be setUndecorated(true) to clear the look-and-feel decoration if and only if the JFrame has not been made displayable, that is has not been made visible. See : isDisplayable() and setVisible(boolean) ::slight_smile:

Sorry, but I have to disagree:

Yea, .add works also now It was a pain to typ contentpane everytime anyway :stuck_out_tongue:

Noya, I like you idee about making the frame a bit bigger to fit the entire Frame, but for some reason I get 0 insets?

public class GameWindow extends JFrame {
private JPanel gamePanel;
private Dimension size;

public GameWindow(String title, GraphicsConfiguration gc, Dimension dimension) {
super(title, gc);
Insets ins = getInsets();
size = new Dimension(dimension.width + ins.left + ins.right, + dimension.height + ins.top + ins.bottom);
initFrame();
initComponents(dimension);
add(gamePanel, BorderLayout.CENTER); // Ellements automatic resize to fit the Frame
pack();
}

private void initFrame() {
setResizable(false);
setIgnoreRepaint(true);
setPreferredSize(size);
setMinimumSize(size);
setFocusTraversalKeysEnabled(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
((JComponent) getContentPane()).setOpaque(false);
}

private void initComponents(Dimension dimension) {
gamePanel = new GamePanel(dimension);
}
}

and I have no idee why, the function inside the java package sais nothing about returning 0.

Somewhere (but I really don’t remember where) the documentation says that the insets are not known until the frame was one time visible on the screen. So you have to do frame.setVisible(true) before frame.getInsets() works.

ok, that worked the Jframe is now bigger, but the JPanel is still positioned behind the decoration.
So I need to tell the Jpanel to move down, and right according to the insets.
So I tried to override getInsets in my panel but it is not called by pack at all.


code]@Override
  public Insets getInsets() {
    return new Insets(50,20,30,50);
  }

is there another method that get’s called on pack for positioning the panel?

Also the Jpanel doesn’t have setInsets(),

I tried this:

    Border top = new EmptyBorder(ins.top,ins.left,0,0);
    gamePanel.setBorder(top);

but no change.

This is what I always do:

GamePanel gamePanel = new GamePanel();
gamePanel.setPreferredSize(new Dimension(GAME_WINDOW_WIDTH, GAME_WINDOW_HEIGHT)); 

JFrame frame = new JFrame("Hello world!");
frame.setContentPane(gamePanel);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);

You might want setMinimumSize and setMaximumSize as well.

Unfortunately, this makes the JFrame larger than GAME_WINDOW_WIDTH, GAME_WINDOW_HEIGHT, but it does make sure all of gamePanel is visible.

Hi,

I tried your code like this:

  public test() {
    gamePanel = new JPanel();

    gamePanel.setPreferredSize(new Dimension(500, 600));
    gamePanel.setMinimumSize(new Dimension(500, 600));
    gamePanel.setMaximumSize(new Dimension(500, 600));

    JFrame frame = new JFrame("Hello world!");
    frame.setContentPane(gamePanel);
    frame.pack();
    frame.setResizable(false);
    frame.setVisible(true);
    Graphics g = frame.getGraphics();
    g.drawString("can you see me1?", 0, 0);
    g.drawString("can you see me2?", 0, 20);
    g.drawString("can you see me3?", 0, 50);
  }


  public static void main(String[] args) {
    new test();
  }
}

and I can only see the third string

I am suprised you see anything. You are not drawing correctly at all.


public class Test extends JPanel{
    public Test() {
        setPreferredSize(new Dimension(500, 600));
        setMinimumSize(new Dimension(500, 600));
        setMaximumSize(new Dimension(500, 600));

        JFrame frame = new JFrame("Hello world!");
        frame.setContentPane(this);
        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawString("can you see me1?", 0, 10);
        g.drawString("can you see me2?", 0, 30);
        g.drawString("can you see me3?", 0, 50);
    }

    public static void main(String[] args) {
        new Test();
    }
}

Don’t for get that you cannot drawString(“Something”, 0, 0) because a font’s base line is below the top and drawing at 0 starts at the base line.

I know, i know i’m a bad boy i paint at init ;D it was just a quick test… :stuck_out_tongue:

I figured it out! :wink: by looking at the spaceinvaders game by Kevin Glass, mayby i should examine those again :d

it seems that when you add a panel to a contentpane it goes straight up behind the decor,
but a awt canvas isn’t affected by the pack() so it stays in place?,… if somebody could give a better explanation
go ahead.

i know that mixing awt and swing is not good. and i alsoo know that on every tutorial they use a canvas inside a Jframe ::slight_smile:
mayby it’s for this reason, i don’t know. But i got my 0,0 cordinate where i want it to be.

http://www.java-gaming.org/forums/index.php?topic=9511.0 in the Game.java class

Solved!

A Canvas is a heavy weight component, so it will always draw above light weight components. Swing is made of almost entirely light weight components. That is why I always recommend to people to do the ENTIRE Java Tutorial before trying to do your own stuff. All that is explained in there.

I’ve always just created a JFrame and then added a JPanel to the content pane and never had any issues at all. Unless I explicitly resize the JPanel myself, it always fits the JFrame. Are you trying to resize the JPanel? If that’s what you’re doing, that’s the issue. You should resize the JFrame instead – the JPanel will be resized to fit it, but once again only if you didn’t specify a custom size.