Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPanels, JFrames, and Windows, Oh my!

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.

like image 583
Jonathan Avatar asked Nov 22 '25 14:11

Jonathan


1 Answers

Have you made any progress on this problem? It might be helpful if you could update your question with your expected behavior and what the code is actually doing? As was already pointed out, JFrame is a subclass of Window, so if you have a JFrame, you don't need a Window.

For what it's worth, I have a Java app which works in fullscreen mode. Although the screen is not repainted as often as yours, it is repainted regularly. I do the following to enter fullscreen:

// pseudo-code; not compilable

JPanel container = new JPanel();
container.setOpaque( true ); // make sure the container will be visible

JFrame frame = new JFrame();
frame.getContentPane().add(container); // add the container to the frame
frame. ... //other initialization stuff, like default close operation, maximize, etc

if ( fullScreenModeIsSupported )
    frame.setUndecorated( true ); // remove window decorations from the frame
    gs.setFullScreenWindow( frame );
    frame.validate();

Then whenever I need to update the screen, I just plug a new JPanel into the container JPanel:

// pseudo-code; not compilable

container.removeAll(); // clean out the container
container.add( jPanelWithNewDisplay ); // add the new display components to the container
container.validate();  // update and redisplay
container.repaint();

Can't claim that it's technically perfect, but it works well for me. If the pseudo-code examples don't cut it, I can spend some time putting together a compilable example.

like image 174
Clayton Avatar answered Nov 24 '25 04:11

Clayton