1

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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
1Q91
  • 23
  • 2
  • 7
  • Does popping in a this.mainFrame.repaint() call just after calling setFullScreen() make any difference? I'm wondering if changing to full-screen dumps the bits but it doesn't realise it needs to repaint. – JTeagle Mar 10 '12 at 19:21
  • That unfortunately doesn't change anything. While the screen is turning to fullscreen mode I shortly see the pink background of the canvas before the screen turns black (even without repaint()). – 1Q91 Mar 10 '12 at 19:26
  • Actually, thinking about it, I think the problem is making the current thread sleep - because the current thread is the UI thread, so you are effectively pausing the UI thread and not allowing it to keep working. Try taking the Thread.sleep() out of this class and put it in your main() after calling run() - and then add a separate method to shrink the window back down again. That way the sleep is on the application thread, not the UI thread, if I've got my thread theory correct. – JTeagle Mar 10 '12 at 19:37
  • I quickly tried that but that doesn't seem to solve the problem either. The full screen still turns black after shortly showing the pink background. – 1Q91 Mar 10 '12 at 19:44
  • Unable to compile Java right now... grrr. My last suggestion for today is to try this.repaint() rather than this.mainFrame.repaint(). Or both. The canvas is inside a JPanel inside a frame... so maybe it needs more than a kick to fully repaint after going full-screen. – JTeagle Mar 10 '12 at 19:56
  • 1
    I figured out what's causing the problem: It's the bit-depth parameter for the DisplayMode's constructor (new DisplayMode(800, 600, 32, DisplayMode.REFRESH_RATE_UNKNOWN);) Although a bit depth of 32 bit worked out well when I tried to set a simple JFrame to fullscreen, it seems as if there is a problem when using the Canvas in fullscreen with a bit depth of 32 bit. Using 16 bit worked perfectly fine with the source code I provided in the question. I can't really tell what's the exact problem though. – 1Q91 Mar 10 '12 at 20:35
  • Don't mix Swing & AWT components without good reason. – Andrew Thompson Mar 11 '12 at 06:16

3 Answers3

3

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

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

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.

Ayman
  • 1
0

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".

lxbndr
  • 2,159
  • 1
  • 15
  • 17