Search code examples
javaswingscreenfullscreenpaint

Paint method not working on full screen - Java


I am trying to create a full screen with a paint method. I want "This is the day" in pink with a blue background to show up. The problem is, is that when I run my program, It shows up with what I painted on my screen with my toolbar and applications instead of showing up a blue background with "This is the day" in the middle in pink. Some code is below.

public static void main(String args[])
{
    DisplayMode dm1 =
        new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
    RunScreen runProgram = new RunScreen();
    runProgram.run(dm1);
}

public void run(DisplayMode dm)
{
    getContentPane().setBackground(Color.BLUE);
    setForeground(Color.PINK);

    setFont(new Font("Arial", Font.PLAIN, 24));

    FullScreen s = new FullScreen();
    try
    {
        s.setFullScreen(dm, this);

        try
        {
            Thread.sleep(5000);
        }
        catch (Exception e)
        {

        }

    }
    finally
    {
        s.restoreScreen();
    }

}

@Override
public void paint(Graphics g)
{

    g.drawString("This is the day", 200, 200);

}

Solution

  • This seems to work just fine...

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.GraphicsConfiguration;
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    import javax.swing.AbstractAction;
    import javax.swing.ActionMap;
    import javax.swing.InputMap;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.KeyStroke;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class TestFullScreen {
    
        public static void main(String[] args) {
            new TestFullScreen();
        }
    
        public TestFullScreen() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setUndecorated(true);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestPane());
                    GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
                    device.setFullScreenWindow(frame);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g); 
                String text = "Hello";
                FontMetrics fm = g.getFontMetrics();
                int x = (getWidth() - fm.stringWidth(text)) / 2;
                int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
                g.drawString(text, x, y);
            }        
        }    
    }
    

    Beware, the "window" that you request to be made into a full screen window may not be the actually window used by the system.

    You should avoid overriding paint and use paintComponent instead. You should also avoid overriding paint of top level containers (like JFrame).

    Check out Performing Custom Painting and Painting in AWT and Swing for more details...