java - How to ImageIcon Array List resize in following code -
i have imageicon array list , has many sizes of icons, need set them specific (85*100) size. how this? code follow:
file file = new file("c://users/ks/desktop/db"); if (file != null) { file[] files = file.listfiles(new filenamefilter() { public boolean accept(file file, string filename) { if (filename.endswith(".png")) { return true; } else { system.out.println("no jpg files"); return false; } } }); system.out.println("current dir : " + file.getcanonicalpath()); list<imageicon> images = new arraylist<imageicon>();// array list (int fileinlist = 0; fileinlist < files.length; fileinlist++) { system.out.print(files[fileinlist].tostring() + " "); system.out.println("this forloop" + fileinlist); if (files[fileinlist].isfile()) { images.add(0, new imageicon(imageio.read(files[fileinlist])));// add imagaes } } } flowlayout f = new flowlayout(); jpanel p = new jpanel(f); f.setalignment(flowlayout.left); p.setsize(700, 100); (int x = 0; x < images.size(); x++) { p.add(new jlabel(images.get(x))); } jpanel1.setpreferredsize(p.getsize()); jpanel1.add(p); jpanel1.revalidate(); jpanel1.repaint(); } else { system.out.println("no file found"); // array index out of bound display insted of } } catch (ioexception e) { e.printstacktrace(); } system.out.println("success");
to resize image can use image.getscaledinstance()
list<imageicon> images = new arraylist<imageicon>();// array list (int fileinlist = 0; fileinlist < files.length; fileinlist++) { if (files[fileinlist].isfile()) { image baseimage = imageio.read(files[fileinlist]); image scaledimage = baseimage.getscaledinstance(85, 100, java.awt.image.scale_smooth); images.add(0, new imageicon(scaledimage)); } }
or use graphics.drawimage().
list<imageicon> images = new arraylist<imageicon>();// array list (int fileinlist = 0; fileinlist < files.length; fileinlist++) { if (files[fileinlist].isfile()) { image baseimage = imageio.read(files[fileinlist]); bufferedimage scaledimage = new bufferedimage(85, 100, bufferedimage.type_int_rgb); graphics2d g2d = (graphics2d)scaledimage.creategraphics(); g2d.addrenderinghints(new renderinghints(renderinghints.key_rendering, renderinghints.value_render_quality)); g2d.drawimage(baseimage, 0, 0, 85, 100, null); images.add(0, new imageicon(scaledimage)); } }
Comments
Post a Comment