1

我是一名初学者,正在学习用 JAVA 编写游戏。

在我正在编写的游戏中,我试图让它支持多种显示模式。首先让我告诉你一些关于我如何设置显示设置的信息。

在代码的开头,我列出了我希望支持的显示模式

//List of supported display modes
private static DisplayMode modes[] = {
    new DisplayMode(640, 480, 32, 0),
    new DisplayMode(1024, 768, 32, 0),
}; 

然后我从显卡中获取支持的显示模式列表,比较列表并使用第一个匹配的显示模式。

/////////////////////////////////////////////
////Variable Declaration
/////////////////////////////////////////////
private GraphicsDevice vc;

/////////////////////////////////////////////   
//Give Video Card Access to Monitor Screen
/////////////////////////////////////////////
public ScreenManager(){
    GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
    vc = e.getDefaultScreenDevice();
}
/////////////////////////////////////////////
//Find Compatible display mode
/////////////////////////////////////////////
//Compare Display mode supported by the application and display modes supported by the video card
//Use the first matching display mode;
public DisplayMode findFirstCompatibleMode(DisplayMode modes[]){
    DisplayMode goodModes[] = vc.getDisplayModes();
    for(int x=0; x<modes.length; x++){
        for(int y=0; y<goodModes.length; y++){
            if (displayModesMatch(modes[x], goodModes[y])){
                return modes[x];
            }
        }
    }
    return null;
}

/////////////////////////////////////////////   
//Checks if two Display Modes match each other
/////////////////////////////////////////////
public boolean displayModesMatch(DisplayMode m1, DisplayMode m2){

    //Test Resolution
    if (m1.getWidth() != m2.getWidth() || m1.getHeight() != m2.getHeight()){
        return false;
    }

    //Test BitDepth
    if (m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI
            && m1.getBitDepth() != m2.getBitDepth()){
        return false;
    }

    //Test Refresh Rate
    if (m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN &&
            m2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN &&
            m1.getRefreshRate() != m2.getRefreshRate()){
        return false;
    }

    return true;
}

目前,我只支持两种分辨率,640x480 和 1024x768。

为了让我的游戏的每一个元素都可以在两种分辨率下使用,首先我找到屏幕调整了多少,并将这个值存储在一个名为 resizeRatio 的变量中

private void getResizeRatio(){
    resizeRatio = (double)1024/(double)s.getWidth();
    //s.getWidth() returns the current width of the screen.
}

对于我导入的每张图像,我都会将图像的高度和宽度除以这个 resizeRatio。

/////////////////////////////////////////////
//Scale the image to the right proportion for the resolution
/////////////////////////////////////////////
protected Image scaleImage(Image in){
    Image out = in.getScaledInstance((int)(in.getWidth(null)/resizeRatio), (int)(in.getHeight(null)/resizeRatio), Image.SCALE_SMOOTH);
    return out;
}

这一切都很好,直到我的应用程序变得越来越大。很快我意识到我忘了调整一些图标的大小,当分辨率为 640x480 时,它们都在错误的位置。

此外,我意识到我必须缩放,不仅是所有图像的大小,还包括所有移动速度和所有位置,因为让我的角色在每次刷新时移动 5px 会使他在以 640x480 显示时移动得比在以 1024x768 显示

所以我的问题是,不是单独缩放每个图像、每个图标和每个动作,有没有办法一次缩放所有内容?或者更确切地说,必须有另一种方式来做到这一点,所以有人可以告诉我吗?

感谢您的阅读,任何帮助将不胜感激。

4

1 回答 1

2

paintComponent(Graphics g)orpaint方法中,您可以使用Graphics2D.scale

private double scale = 0.75;

@override
public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D)g; // The newer more ellaborate child class.
    g2.scale(scale, scale);
    ...
    g2.scale(1/scale, 1/scale);
}
于 2012-06-02T22:56:24.853 回答