我有一个小问题。我正在学习 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();
}
}
}