views:

394

answers:

4

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.

A: 

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.

camickr
I had read the API, but had gotten mixed up between JFrame and JPanel (as was pointed out by Oscar). My mistake.
Jonathan
A: 

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.

OscarRyz
This does not seem to work, but I think that may be in part to the method I have appended to my original question. The method is called every 10ms and messes with the state of my JFrame.
Jonathan
A: 

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.

Suraj Chandran
@Jonathan, Tell me if this couldnt solve your problem!!
Suraj Chandran
I do not think this is what I need.I believe the code I have mentioned above makes use of several efficiencies that your solution does not.Most notably, if I am correct, it does not act as if it was in a windowed or simulated full-screen (your solution). It is either maximized above all other windows and the task bar, or minimized onto the task bar. This way it does not have to worry about checking for other windows on top and calling a repaint method. It also looks cleaner. If you run the above code you will see what I mean.
Jonathan
@Jonathan...See my edit to the answer if it helps. I am posting in comments in case you miss: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.
Suraj Chandran
@Jonathan....See my Edit2 :)...If still does not help. Please let me know.
Suraj Chandran
+1  A: 

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.

Clayton