java - how to resize image while resize frame using drawImage -
i have little problem. i'm learning java forms , want resize image while resize form. unfortunately resize form. image have still these same dimensions. how can rescale image? should change? wrong? enclose code:
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(); (int = 0; < 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() + " 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(); } } }
first when drawimage you'll have not set x , y set width , height. able scale image.
drawimage(image img, int x, int y, int width, int height, imageobserver observer)
your canvas should repainted when re-size form, if doesn't can force .repaint();
addition: told me wanted same size form. you'll have 2 things then. first use method mentioned above width this.getwidth() , height this.getheight refers canvas. you'll have make sure canvas scales frame can choosing appropriate layoutmanager.
frame.setlayout(...)
Comments
Post a Comment