Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中,创建支持不同分辨率/显示模式的应用程序_Java_Image_Io_Resolution - Fatal编程技术网

在Java中,创建支持不同分辨率/显示模式的应用程序

在Java中,创建支持不同分辨率/显示模式的应用程序,java,image,io,resolution,Java,Image,Io,Resolution,我是一个初学者,正在学习用JAVA编写游戏 在我编写的游戏中,我试图让它支持多种显示模式。首先让我告诉你一点我是如何设置显示设置的 在代码的开头,我列出了我希望支持的显示模式 //List of supported display modes private static DisplayMode modes[] = { new DisplayMode(640, 480, 32, 0), new DisplayMode(1024, 768, 32, 0), }; 然后,我从视频

我是一个初学者,正在学习用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;
}
对于我导入的每一幅图像,我会用这个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时,它们都在错误的位置

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

所以我的问题是,有没有一种方法可以一次缩放所有图像、图标和动作,而不是单独缩放每个图像、每个图标和每个动作?或者更确切地说,必须有另一种方法这样做,那么有人能告诉我吗


感谢阅读,如有任何帮助,我们将不胜感激。

paintComponent(Graphics g)
paint
方法中,您可以使用:


1+,但如果图形与鼠标侦听器交互,最好小心。谢谢,我现在就试试@HovercraftFullOfEels我确实要用鼠标控制角色的某些方面,我应该注意什么?鼠标位置在屏幕坐标中,需要缩放以匹配图形对象。@Synia:是的,正如乔普所说。另外,请不要忘记为乔普的努力投票。
/////////////////////////////////////////////
//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;
}
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);
}