user3755709
user3755709

Reputation: 3

Screen not created for drawstring text to display

I am working on Java Game Development Tutorials given on buckysroom.org. I am creating a Screen class, which allows me to go full screen. Then, when I try to paint it with text, the screen does not appear, and only the text is shown on my working window (Eclipse) If I comment out the paint method or make it paintComponent, the red screen appears, but without any text. Can anyone help me on how to deal with this?

The Screen class:

import java.awt.*;
import javax.swing.JFrame;

public class Screen {

private GraphicsDevice vc;

public Screen() {
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    vc = env.getDefaultScreenDevice();
}

public void setFullScreen(DisplayMode dm, JFrame window) {
    window.setUndecorated(true);
    window.setResizable(false);
    vc.setFullScreenWindow(window);
    if (dm != null && vc.isDisplayChangeSupported()) {
        try {
            vc.setDisplayMode(dm);
        } catch (Exception ex) {
        }
    }
}

public Window getFullScreenWindow() {
    return vc.getFullScreenWindow();
}

public void restoreScreen() {
    Window w = vc.getFullScreenWindow();
    if (w != null) {
        w.dispose();
    }
    vc.setFullScreenWindow(null);
 }
}

This is the class where I call it:

import java.awt.*;
import javax.swing.JFrame;

public class bucky extends JFrame {

public static void main(String[] args) {
    DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
    bucky m = new bucky();
    m.run(dm);
}

public void run(DisplayMode dm) {
    this.getContentPane().setBackground(Color.RED);
    setForeground(Color.BLACK);
    setFont(new Font("Arial", Font.PLAIN, 24));
    Screen s = new Screen();
    try {
        s.setFullScreen(dm, this);
        try {
            Thread.sleep(5000);
        } catch (Exception ex) {
        }
    } finally {
        s.restoreScreen();
    }    
}

public void paint(Graphics g) {

    g.drawString("This is awesome", 200, 200);
 }
}

Upvotes: 0

Views: 66

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347314

My suspicion is that the Thread.sleep is cause the Event Dispatching Thread to be put to sleep, preventing it from processing any paint events (or any other kind of events for that matter) and/or the paint method of the JFrame is not an appropriate method for performing paint in (generally it's not for a number of reasons)...

Instead, you should consider using something like javax.swing.Timer which you can use to set up an alarm for some time in the future and it will call you back.

You should also consider using something like a JPanel to perform your custom painting on, for example...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class FullScreenTest {

    public static void main(String[] args) {
        new FullScreenTest();
    }

    public FullScreenTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setContentPane(new TestPane());

                DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
                Screen.setFullScreen(null, frame);

                Timer timer = new Timer(5000, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Screen.restoreScreen();
                    }
                });
                timer.start();
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setFont(new Font("Arial", Font.PLAIN, 24));
            setBackground(Color.RED);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g.drawString("This is awesome", 200, 200);
            g2d.dispose();
        }

    }

    public static class Screen {

        public static GraphicsDevice getGraphicsDevice() {
            GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
            return env.getDefaultScreenDevice();
        }

        public static void setFullScreen(DisplayMode dm, JFrame window) {
            window.setUndecorated(true);
            window.setResizable(false);
            GraphicsDevice gd = getGraphicsDevice();
            gd.setFullScreenWindow(window);
            if (dm != null && gd.isDisplayChangeSupported()) {
                try {
                    gd.setDisplayMode(dm);
                } catch (Exception ex) {
                }
            }
        }

        public static Window getFullScreenWindow() {
            GraphicsDevice gd = getGraphicsDevice();
            return gd.getFullScreenWindow();
        }

        public static void restoreScreen() {
            GraphicsDevice gd = getGraphicsDevice();
            Window w = gd.getFullScreenWindow();
            if (w != null) {
                w.dispose();
            }
            gd.setFullScreenWindow(null);
        }
    }

}

Take a look at:

For more details...

Also, because of the type of work it's doing, Screen could also be a fully static class. It maintains no contextual information and simply provides utility functionality - IMHO.

If you needed one for each GraphicsDevice, that would be another story ;)

Upvotes: 1

Related Questions