【问题标题】:Resizable according to screen resolution, Java Application根据屏幕分辨率调整大小,Java 应用程序
【发布时间】:2023-03-03 05:47:19
【问题描述】:

我正准备发布此应用程序。但是我仍然没有找到一种方法来制作适合任何屏幕的应用程序。事实上,我在 JLabels 中使用 pngs 文件作为背景和按钮(主要),这是我认为的一个大问题。我想要实现的是让 JFrame 及其组件可调整大小但保持比例,就像响应式网站一样! 我计划创建原始PNG的其他方式,但更小,然后创建新框架,并在启动应用程序时询问系统哪个框架最适合屏幕并使用它(但我知道我不会很好,然后当然是操作系统如果您知道我的意思,有时会以不同的方式使用屏幕分辨率,使屏幕看起来更小或更大)。 (我正在使用 Netbeans)。

非常感谢您,我正在寻找前言与您讨论这个问题,我相信很多人都会关注。

【问题讨论】:

    标签: java image jframe resolution resizable


    【解决方案1】:

    Toolkit.getDefaultToolkit() 具有为您提供所需内容的方法,包括获取当前屏幕尺寸:

    private void makeFrameFullSize(JFrame aFrame)
    {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        aFrame.setSize(screenSize.width, screenSize.height);
    }
    

    这篇文章讨论了Resize image while keeping aspect ratio in Java

    现在您有了屏幕尺寸,您可以计算图像高度/宽度与当前帧尺寸的比率,并根据当前帧尺寸与全屏尺寸的百分比差异进行缩放。

    private Dimension get getFrameToScreenRatio(Frame aFrame){
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        return dimension.setSize(aFrame.getWidth()/dimension.getWidth(), aFrame.getHeight()/dimension.getHeight());
    }
    

    这是一个示例实用程序类,它可以缩放图像以适合画布(如背景图像)并对其进行缩放以适应:

    打包 net.codejava.graphics;

    导入 java.awt.Component; 导入 java.awt.Graphics; 导入 java.awt.Image;

    /**
     * This utility class draws and scales an image to fit canvas of a component.
     * if the image is smaller than the canvas, it is kept as it is.
     *
     * @author www.codejava.net
     *
     */
    public class ImageDrawer {
    
        public static void drawScaledImage(Image image, Component canvas, Graphics g) {
            int imgWidth = image.getWidth(null);
            int imgHeight = image.getHeight(null);
    
            double imgAspect = (double) imgHeight / imgWidth;
    
            int canvasWidth = canvas.getWidth();
            int canvasHeight = canvas.getHeight();
    
            double canvasAspect = (double) canvasHeight / canvasWidth;
    
            int x1 = 0; // top left X position
            int y1 = 0; // top left Y position
            int x2 = 0; // bottom right X position
            int y2 = 0; // bottom right Y position
    
            if (imgWidth < canvasWidth && imgHeight < canvasHeight) {
                // the image is smaller than the canvas
                x1 = (canvasWidth - imgWidth)  / 2;
                y1 = (canvasHeight - imgHeight) / 2;
                x2 = imgWidth + x1;
                y2 = imgHeight + y1;
    
            } else {
                if (canvasAspect > imgAspect) {
                    y1 = canvasHeight;
                    // keep image aspect ratio
                    canvasHeight = (int) (canvasWidth * imgAspect);
                    y1 = (y1 - canvasHeight) / 2;
                } else {
                    x1 = canvasWidth;
                    // keep image aspect ratio
                    canvasWidth = (int) (canvasHeight / imgAspect);
                    x1 = (x1 - canvasWidth) / 2;
                }
                x2 = canvasWidth + x1;
                y2 = canvasHeight + y1;
            }
    
            g.drawImage(image, x1, y1, x2, y2, 0, 0, imgWidth, imgHeight, null);
        }
    

    【讨论】:

    • 谢谢!我知道,但它只调整框架的大小,而不是它内部的组件(?)。主要问题是我处理图像。我真的不知道从哪里拿它..
    猜你喜欢
    • 1970-01-01
    • 2011-02-17
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多