Paint方法无法全屏工作 - Java

时间:2013-05-19 08:32:41

标签: java swing screen fullscreen paint

我正在尝试使用paint方法创建一个全屏。我想"这是一天"粉红色的蓝色背景出现。问题是,当我运行我的程序时,它显示我用我的工具栏和应用程序在我的屏幕上绘制的内容,而不是显示蓝色背景"这是一天"在粉红色的中间。有些代码如下。

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

}

4 个答案:

答案 0 :(得分:2)

Thread.sleep(5000);

不要阻止EDT(事件调度线程) - 当发生这种情况时,GUI将“冻结”。而不是调用Thread.sleep(n)为重复任务实现Swing Timer或为长时间运行的任务实现SwingWorker。有关详细信息,请参阅Concurrency in Swing

答案 1 :(得分:1)

隐藏工具栏等。使用setUndecorated

runProgram.setUndecorated(true);
runProgram.run(dm1);

要在中间显示粉红色的“这是当天”的蓝色背景,请在paint(或paintComponent)中设置颜色

g.setColor(Color.BLUE);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.PINK);
g.drawString("This is the day", 200, 200);

其次,你不应该从EDT(事件调度线程)中涉及GUI。你应该做这样的事情:

SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        setFont(new Font("Arial", Font.PLAIN, 24));
    }
});

第三,您不应覆盖顶级容器的paint。 (我不是指@Override符号)。你应该做Andrew Thompson mentioned

答案 2 :(得分:1)

这似乎工作得很好......

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);
        }        
    }    
}

请注意,您请求进入全屏窗口的“窗口”可能不是系统使用的实际窗口。

您应该避免覆盖paint并改为使用paintComponent。您还应该避免覆盖paint顶级容器(例如JFrame)。

查看Performing Custom PaintingPainting in AWT and Swing了解更多详情......

答案 3 :(得分:0)

  

全屏显示时,未输入重画方法。因此,这种方式对于重新绘制Swing JFrame很有用。

public void actionPerformed(ActionEvent arg0) {
    if (getExtendedState() == 6) {//6 full-screen, 0 normal, -1 minimized
        repaint();
    } else {
        repaint();
    }
}