JPanels, JFrames, and Windows, Oh my!

Posted by Jonathan on Stack Overflow See other posts from Stack Overflow or by Jonathan
Published on 2009-11-05T02:52:56Z Indexed on 2010/05/09 23:08 UTC
Read the original article Hit count: 222

Filed under:
|
|
|
|

Simply stated, I am trying to make a game I am working on full-screen.

I have the following code I am trying to use:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
if(!gs.isFullScreenSupported()) {
    System.out.println("full-screen not supported");
}

Frame frame = new Frame(gs.getDefaultConfiguration());
Window win = new Window(frame);

try {
    // Enter full-screen mode
    gs.setFullScreenWindow(win);
    win.validate();
}

Problem with this is that I am working within a class that extends JPanel, and while I have a variable of type Frame, I have none of type Window within the class.

My understanding of JPanel is that it is a Window of sorts, but I cannot pass 'this' into gs.setFullScreenWindow(Window win)... How should I go about doing this?

Is there any easy way of calling that, or a similar method, using a JPanel?

Is there a way I can get something of type Window from my JPanel?

-

EDIT: The following method changes the state of JFrame and is called every 10ms:

public void paintScreen()
{
    Graphics g;
    try{
        g = this.getGraphics(); //get Panel's graphic context
        if(g == null)
        {
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setExtendedState(frame.getExtendedState()|JFrame.MAXIMIZED_BOTH);
            frame.add(this);
            frame.pack();
            frame.setResizable(false);
            frame.setTitle("Game Window");
            frame.setVisible(true);
        }
        if((g != null) && (dbImage != null))
        {
            g.drawImage(dbImage, 0, 0, null);
        }
        Toolkit.getDefaultToolkit().sync(); //sync the display on some systems
        g.dispose();
    }
    catch (Exception e)
    {
        if(blockError)
        {
        	blockError = false;
        }
        else
        {
        	System.out.println("Graphics context error: " + e);
        }
    }
}

I anticipate that there may be a few redundancies or unnecessary calls after the if(g==null) statement (all the frame.somethingOrOther()s), any cleanup advice would be appreciated...

Also, the block error is what it seems. I am ignoring an error. The error only occurs once, and this works fine when setup to ignore the first instance of the error... For anyone interested I can post additional info there if anyone wants to see if that block can be removed, but i'm not concerned... I might look into it later.

© Stack Overflow or respective owner

Related posts about java

Related posts about jpanel