JPanels, JFrames, and Windows Oh my!

Simply put, I'm trying to create a game in which I work in full screen mode.

I have the following code that 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();
}

The problem with this is that I work in a class that extends JPanel, and although I have a variable of type Frame, I don't have any Window types inside the class.

My understanding of JPanel is that it is a kind of window, but I cannot pass 'this' to gs.setFullScreenWindow (Window win) ... How do I do this?

Is there an easy way to call this method or a similar method using JPanel?

Is there a way to get something like Window from my JPanel?

-

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

public void paintScreen()
{
    Graphics g;
    try{
        g = this.getGraphics(); //get Panel 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);
        }
    }
}

, if (g == null) ( frame.somethingOrOther()), ...

, - , . . , , ... , , , - , , ... .

+3
4

- ? , , , ? , JFrame Window, , JFrame, .

, Java, . , , . :

// 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();

, , JPanel 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();

, , . , .

+2

JPanel Window. JFrame .

, :

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

appyYourFullScreenCodeFor( yourFrame );

.

0

, , .

  • ,      .

  • . , .

  • . , .

    frame.setUndecorated(); frame.add();// .

. , Unecorated API , , , , setVisible (false).

EDIT1. , , :

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

, , .

EDIT2. Frame Window, gs.setFullScreen(frame). .

0
source

My understanding of JPanel is that it is a window view

Why do you think so? Have you read the API? Does JPanel Appear From a Window?

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

-1
source

Source: https://habr.com/ru/post/1721947/


All Articles