Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

JPanel is not a subclass of Window. JFrame is.

So you could try:

JFrame yourFrame = new JFrame();
yourFrame.add(yourPanel);

appyYourFullScreenCodeFor( yourFrame );

That should work.

I think I got what you need.

  1. Set the frame undecorated, so it comes without any title bar and stuff.

  2. Add your panel to the frame., so it looks like only your panel is shown.

  3. Maximize your frame. So now it should look like there's only your panel taking the full screen without and window stuff.

    frame.setUndecorated(true); frame.add(panel); //now maximize your frame.

Note: Its important to note that the undecorated API can only be called when your frame is undisplayable, so if its already show, then first you need to do setVisible(false).

EDIT1: If all you want is to get the window containing your panel, then you can do this:

Window win = SwingUtilities.getAncestorOfClass(Window.class, myPanel);

Once you get the window instance you can pass it wherever you want.

EDIT2: Also the Frame class extends Window so you can directly do gs.setFullScreen(frame). You dont need to create a new window for that frame.

My understanding of JPanel is that it is a Window of sorts

Why would you think that? Did you read the API? Does JPanel extend from Window?

You can try using the SwingUtilities class. It has a method that returns the Window for a given component.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top