0

制作一个全屏跳棋游戏,用于在 java 中学习/练习绘图/摇摆,但无法将其绘制在屏幕的上部(位置 [0,0] 大约在我的屏幕顶部下方 20 像素。)

这是示例代码(我现在只是使用 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();
    }
}
4

1 回答 1

0

你想要做的是调用:

window.setUndecorated(true);

根据框架文档, “框架可能会使用 setUndecorated 关闭其原生装饰(即框架和标题栏)。这只能在框架不可显示时完成。”

需要进行更多更改。从 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
    }
}
于 2014-05-04T18:52:55.597 回答