Java - Screen turns black, when setting a JFrame to Fullscreen

爷,独闯天下 提交于 2019-12-04 02:18:00

问题


I'm trying to draw something on a Canvas, add it to a JFrame and then set this JFrame to Fullscreen. My problem is: in fullscreenmode I only see a black screen. Before the screen turns black I shortly can see the pink background of the canvas.

Drawing directly on a JFrame and then setting it to fullscreen works perfectly fine and I can see the testtext. I assume there is a problem with displaying the Canvas properly.

Here is my code:

public class FullscreenTest extends Canvas {

    private JFrame mainFrame;

    public FullscreenTest(){
        this.mainFrame = new JFrame();
        JPanel contentPane = (JPanel) mainFrame.getContentPane();
        contentPane.add(this);
    }

    public void run(DisplayMode dm){
        setBackground(Color.PINK);
        setForeground(Color.WHITE);
        setFont(new Font("Arial", Font.PLAIN, 24));

        Screen s = new Screen();

        s.setFullScreen(dm, this.mainFrame);

        try {
            Thread.sleep(5000);
        } catch (InterruptedException exc) { exc.printStackTrace(); }

        s.closeFullScreenWindow();
    }

    public void paint(Graphics g){      
        g.drawString("This is some testtext", 200, 200);
    }

    public static void main(String[] args){
        DisplayMode dm = new DisplayMode(800, 600, 32, DisplayMode.REFRESH_RATE_UNKNOWN);
        FullscreenTest test = new FullscreenTest();
        test.run(dm);
    }
}

Here is what the Screen.setFullScreen(DisplayMode dm, JFrame window) method does:

//graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment()
//                 .getDefaultScreenDevice();
public void setFullScreen(DisplayMode dm, JFrame window){
    window.setUndecorated(true);
    window.setResizable(false);
    graphicsDevice.setFullScreenWindow(window);

    if(dm != null && graphicsDevice.isDisplayChangeSupported()){
        graphicsDevice.setDisplayMode(dm);          
    }
}

Does anyone have a clue why I don't see the JFrame's content in fullscreen?


回答1:


1) you have three general issues

  • never block EDT by using Thread.sleep(5000); use Swing Timer instead, demonstrations here

  • (if aren't there really important reasons) don't mixing AWT with Swing the rest is here, and use JPanel instead of Canvas (for Canvas is there paint method, for JPanel is there paintComponent)

  • your public void paint(Graphics g){ is to the JFrame instead of Canvas and locked by Thread.sleep(5000);

2) Swing GUI rellated should be wrapped into invokeLater() meaning

public static void main(String[] args){

more in the Initial Thread

3) in linked code example you can find out demostrations how to use background thread in the Swing




回答2:


I agree with mKorbel (actually, I have your code working with corrections he suggest). Just one hint to achieve more predictable results further: take control on colors in paint() method. Default color for background may vary on different systems. On my system it draws white text on light-red background. But if it will draw black text on black background, test will look like "not working".




回答3:


hey i had he same problem and the screen turns black every time i run the program. in the part of the paint method , you wrote, which i think that it is from Bucky tutorial which is amazing by the way :

public void paint(Graphics g){      
        g.drawString("This is some testtext", 200, 200);
    }

all you have to do is to use "super"

public void paint(Graphics g){
        super.paint(g);
        g.drawString("This is some testtext", 200, 200);
    }

I tried it myself and it is working just fine.



来源:https://stackoverflow.com/questions/9649394/java-screen-turns-black-when-setting-a-jframe-to-fullscreen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!