为什么这不会在全屏窗口的前20个像素上绘制?

时间:2014-05-04 18:33:30

标签: java fullscreen paint draw screen-positioning

制作全屏跳棋游戏,用于学习/练习绘图/摆动java但不能在屏幕的上半部分绘制(位置[0,0]比我的顶部低20px屏幕。)

这里的代码示例(我现在只使用alt + F4退出)

public class Game extends JFrame{
        //get resolution
        public static GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        public static final int mWidth = gd.getDisplayMode().getWidth();
        public static final int mHeight = gd.getDisplayMode().getHeight();  

    public static void main(String[] a) {

        //create game window
        JFrame window = new JFrame();
        Board board = new Board();

        gd.setFullScreenWindow(window);

        window.setSize(mWidth, mHeight);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setResizable(false);
        window.setVisible(true);
        window.add(board);

        board.repaint();


    }

}

public class Board extends JComponent{

    public void paint(Graphics b){
        b.fillRect(0, 0, Game.mWidth-7, Game.mHeight-29);
        repaint();
    }
}

1 个答案:

答案 0 :(得分:0)

您要做的是致电:

window.setUndecorated(true);

根据Frame documentation"框架可以使用setUndecorated关闭其原生装饰(即Frame和Titlebar)。这只能在框架不可显示时才能完成。"

需要进行更多更改。从Board类中删除偏移值。

public class Board extends JComponent{
    public void paint(Graphics b){
        b.setColor(Color.BLUE); // Just to make the color more obvious
        b.fillRect(0, 0, Game.mWidth, Game.mHeight);
        repaint();
    }
}

确保在添加并重新绘制电路板后调用window.setVisible()

public class Game extends JFrame{
        //get resolution
        public static GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        public static final int mWidth = gd.getDisplayMode().getWidth();
        public static final int mHeight = gd.getDisplayMode().getHeight();  

    public static void main(String[] a) {
        //create game window
        JFrame window = new JFrame();
        window.setUndecorated(true);
        Board board = new Board();

        gd.setFullScreenWindow(window);

        window.setSize(mWidth, mHeight);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setResizable(false);
        window.add(board);
        board.repaint();

        window.setVisible(true); // This needs to be last
    }
}