Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (527)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  JPanel hiden behind JFrame decoration  (Read 2738 times)
0 Members and 1 Guest are viewing this topic.
Offline stef569

Junior Devvie





« Posted 2008-01-19 20:00:04 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
  public GamePanel() {
    super(false);
    initPanel();
  }

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


please advise
Offline Noya

Senior Newbie





« Reply #1 - Posted 2008-01-19 21:16:30 »

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 Smiley
Offline broumbroum

Junior Devvie





« Reply #2 - Posted 2008-01-20 05:14:55 »

    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)  Roll Eyes

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Noya

Senior Newbie





« Reply #3 - Posted 2008-01-20 11:43:43 »

should be replaced by getContentPane().add(gamePanel, BorderLayout.CENTER), indeed the frame contentpane layout is overlapping the contentpane...

Sorry, but I have to disagree:

Quote from: javadoc 1.5 JFrame
As a conveniance add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write:
frame.add(child);
And the child will be added to the contentPane
Offline stef569

Junior Devvie





« Reply #4 - Posted 2008-01-20 14:09:12 »

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

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.
Offline Noya

Senior Newbie





« Reply #5 - Posted 2008-01-20 16:08:12 »

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.
Offline stef569

Junior Devvie





« Reply #6 - Posted 2008-01-20 18:44:37 »

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.

1  
2  
3  
4  
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?
Offline stef569

Junior Devvie





« Reply #7 - Posted 2008-01-22 01:57:25 »

Also the Jpanel doesn't have setInsets(),

I tried this:
1  
2  
    Border top = new EmptyBorder(ins.top,ins.left,0,0);
    gamePanel.setBorder(top);


but no change.
Offline Markus_Persson

JGO Wizard


Medals: 16
Projects: 19


Mojang Specifications


« Reply #8 - Posted 2008-01-23 12:36:27 »

This is what I always do:

1  
2  
3  
4  
5  
6  
7  
8  
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.

Play Minecraft!
Offline stef569

Junior Devvie





« Reply #9 - Posted 2008-01-24 15:15:00 »

Hi,

I tried your code like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
  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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #10 - Posted 2008-01-24 19:58:04 »

I am suprised you see anything.  You are not drawing correctly at all.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
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.

Offline stef569

Junior Devvie





« Reply #11 - Posted 2008-01-24 23:39:14 »

I know, i know i'm a bad boy i paint at init  Grin it was just a quick test... 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  Roll Eyes
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!
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #12 - Posted 2008-01-25 03:36:33 »

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.

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #13 - Posted 2008-02-08 05:30:04 »

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.

See my work:
OTC Software
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (73 views)
2014-11-26 15:22:04

toopeicgaming1999 (63 views)
2014-11-26 15:20:36

toopeicgaming1999 (15 views)
2014-11-26 15:20:08

SHC (29 views)
2014-11-25 12:00:59

SHC (27 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (27 views)
2014-11-24 19:59:16

trollwarrior1 (40 views)
2014-11-22 12:13:56

xFryIx (78 views)
2014-11-13 12:34:49

digdugdiggy (57 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!