Screen Size in Java

纵然是瞬间 提交于 2019-12-13 06:49:55

问题


Hello I am making a game in Java and I get the screenSize using DefaultToolkit, but the problem with that is that is detects the size of the screen if it was FULLSCREEN. How can I get the screen size of the area the game screen (which is not fullscreen) takes up. To be more precise, my sprite is moving beyond the edge of the bottom screen because of the system tool bar which add extra "padding" to it. How can I get the size of the Area the Game Screen takes up? Thank you very much


回答1:


I think Screen Bounds is what you are looking for:

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

public class FrameInfo
{
    public static void main(String[] args)
    {
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle bounds = env.getMaximumWindowBounds();
        System.out.println("Screen Bounds: " + bounds );

        GraphicsDevice screen = env.getDefaultScreenDevice();
        GraphicsConfiguration config = screen.getDefaultConfiguration();
        System.out.println("Screen Size  : " + config.getBounds());
        System.out.println(Toolkit.getDefaultToolkit().getScreenSize());

        JFrame frame = new JFrame("Frame Info");
        frame.setSize(200, 200);
        frame.setVisible( true );

        System.out.println("Frame Size   : " + frame.getSize() );
        System.out.println("Frame Insets : " + frame.getInsets() );
        System.out.println("Content Size : " + frame.getContentPane().getSize() );
     }
}



回答2:


You can get the size of the task bar and other things using Insets. Here's an example to get the screen size that doesn't include the task bar/others.

// Screen size
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

// Screen insets
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());

// Get the real width/height
int width = screen.getWidth() - insets.left - insets.right;
int height = screen.getHeight() - insets.top - insets.bottom;


来源:https://stackoverflow.com/questions/14845886/screen-size-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!