java 如何在使用drawImage调整框架大小的同时调整图像大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16472217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:03:44  来源:igfitidea点击:

how to resize image while resize frame using drawImage

java

提问by Blackchart

I have a little problem. I'm learning about Java forms and I want to resize my Image while I resize my form. But unfortunately I only resize form. Image have still these same dimensions. How can i rescale image? What should i change? What i do wrong? I enclose the code:

我有一个小问题。我正在学习 Java 表单,我想在调整表单大小的同时调整我的图像大小。但不幸的是,我只调整表单的大小。图像仍然具有这些相同的尺寸。如何重新缩放图像?我应该改变什么?我做错了什么?我附上代码:

    package przegladarka;
import java.io.*;
import java.util.ArrayList;
import java.awt.*;

import javax.swing.*;
public class view extends JPanel {
    Image imag; 
    Dimension size;
    public static int framew=300;
    public static int frameh=300;
    static String[] children;
    static ArrayList<String> sciezki = new ArrayList<String>(); 

    public static void main(String[] args) {
        File folder = new File("./zdjecia/");
        visitAllFiles(folder);

        JFrame frame = new JFrame("Przegladarka");
        frame.setSize(300, 300);

        frame.setVisible(true);
        frame.getContentPane().add(new MyCanvas());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
    }



    public static void visitAllFiles(File dir) {
        if (dir.isDirectory()) {
            children = dir.list();
            for (int i = 0; i < children.length; i++) {
                visitAllFiles(new File(dir, children[i]));
                System.out.println(""+children[i]);
                Image imag = Toolkit.getDefaultToolkit().getImage(children[i]); 
            }

        } else {
            System.out.println(dir.getAbsolutePath() + " is being processed.");
            sciezki.add(dir.getAbsolutePath());
        }
    }
}
class MyCanvas extends JComponent{
    public void paint(Graphics g){
        Graphics2D g2d = (Graphics2D) g;

        for(int i=0; i<view.sciezki.size();i++){

            Image imag = Toolkit.getDefaultToolkit().getImage(view.sciezki.get(i));


        g2d.drawImage(imag, 0, 0, this);

        g2d.finalize();
        }
    }
}

回答by Daan Luttik

First when you do the drawImage you'll have to not only set x and y but also set width and height. Only then will you be able to scale the image.

首先,当您执行 drawImage 时,您不仅要设置 x 和 y,还要设置宽度和高度。只有这样,您才能缩放图像。

drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) 

Your canvas should get repainted when you re-size your form, but if it doesn't you can force it with .repaint();

当您重新调整表单大小时,您的画布应该重新绘制,但如果没有,您可以使用 .repaint(); 强制它;

ADDITION: You told me that you wanted it to be the same size as your form. You'll have to do 2 things then. First you use the method mentioned above with as width this.getWidth() and as height this.getHeight where this refers to the canvas. You'll have to make sure that your canvas scales with your frame you can simply do that by choosing an appropriate layoutmanager.

附加:你告诉我你希望它和你的表格一样大。那么你必须做两件事。首先,您使用上面提到的方法,宽度为 this.getWidth(),高度为 this.getHeight,其中 this 指的是画布。您必须确保您的画布与您的框架一起缩放,您可以通过选择合适的布局管理器来简单地做到这一点。

frame.setLayout(...)