问题
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