简体   繁体   English

Java屏幕大小

[英]Screen Size in Java

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. 您好,我正在用Java做游戏,我使用DefaultToolkit得到了screenSize,但问题是如果屏幕是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 非常感谢你

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

You can get the size of the task bar and other things using Insets. 您可以使用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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM