java - How to handle multi resolution images in slideshow? -
i use code slideshow, , it's working 100%, problem image resolution if resolution high no problem if low image shown in bad shape.
and code :
public class easyview_v1 { public static timer tm = null; public static file[] f = new file("c:/ma7moud/my projects/rami/netbeans/web applications/thechatserverapp_3/web/images/brugges2006/big").listfiles(); public static void main(string[] args) { jpanel panel5 = new jpanel(); jlabel pic = new jlabel(); pic.setsize(new dimension(610, 695)); //call function setimagesize try { setimagesize(f.length - 1, f, pic); } catch (exception e) { system.out.println(e.tostring()); joptionpane.showmessagedialog(null, "images directory not found", "error", joptionpane.error_message); joptionpane.showmessagedialog(null, "plz check images directory , try again", "attention", joptionpane.information_message); system.exit(1); } try { //set timer tm = new timer(1000, (actionevent e) -> { setimagesize(x, f, pic); x += 1; try { if (x >= f.length) { x = 0; } } catch (exception ex) { system.out.println(ex.tostring()); system.exit(1); } }); } catch (exception e) { system.out.println(e.tostring()); } panel5.add(pic); tm.start(); jframe f = new jframe () f.add(panel5); f.pack(); f.setvisible(true); }
here code image drawing:
public static void setimagesize(int i, file[] f, jlabel pic) { try { imageicon icon = new imageicon(f[i].tostring()); image img = icon.getimage(); @suppresswarnings("unusedassignment") image newimg = null; newimg = img.getscaledinstance(pic.getwidth(), pic.getheight(), image.scale_smooth); imageicon newimc = new imageicon(newimg); pic.seticon(newimc); } catch (exception ex) { system.out.println(ex.tostring()); } } }
i have found solution , fixing aspect ratio of each image , position of image jpanel solution follow :
i change set image size :
public static void setimagesize(int i, file[] f, jlabel pic) { try { pic.setsize(new dimension(610, 695)); imageicon icon = new imageicon(f[i].tostring()); image img = icon.getimage(); imageicon finaliimage = new imageicon(imgresize(img, img.getwidth(pic), img.getheight(pic), pic)); xy = imgposition(finaliimage.geticonwidth(), finaliimage.geticonheight(), pic); pic.setsize(new dimension(pic.getwidth(), img.getheight(pic))); pic.seticon(finaliimage); } catch (exception ex) { system.out.println(ex.tostring() + "set image size"); } }
and add 2 methods :
private static image imgresize(image srcimg, double w, double h, jlabel pic) { @suppresswarnings("unusedassignment") bufferedimage resizedimg = null; int w = pic.getwidth(), h = pic.getheight(); double ar = w / h, nh = 0, nw = 0; try { if (w > h) { nw = w; nh = nw / ar; } else { nh = h; nw = nh * ar; } resizedimg = new bufferedimage((int) nw, (int) nh, bufferedimage.type_int_argb); graphics2d g2 = resizedimg.creategraphics(); g2.setrenderinghint(renderinghints.key_interpolation, renderinghints.value_interpolation_bilinear); g2.drawimage(srcimg, 0, 0, (int) nw, (int) nh, null); g2.dispose(); return resizedimg; } catch (exception e) { system.out.println(e.tostring() + " resize"); } return resizedimg; } private static int[] imgposition(int w, int h, jlabel pic) { int w = pic.getwidth(), h = pic.getheight(); @suppresswarnings({"unusedassignment", "localvariablehidesmembervariable"}) int x = 0, y = 0; int[] = new int[2]; try { if (w > h) { x = 5; y = ((h / 2) - (h / 2)); } else { y = 0; x = ((w / 2) - (w / 2)) - 30; } } catch (exception e) { system.out.println(e.tostring() + " position"); } a[0] = x; a[1] = y; return a; } }
and add main method :
pic.addcomponentlistener(new componentlistener() { @override public void componentresized(componentevent e) { //==================== variables panel ==================== int panelwidthandhight = (integer) ((math.round(window.getheight() / 2)) - 2); int textwidth = (integer) ((math.round(window.getheight() / 2)) - 75); int textheight = 23; int location1 = (integer) ((math.round(window.getheight() / 2))); int location2 = (integer) (math.round(window.getheight())); int picwidth = (integer) (math.round(window.getwidth() - (window.getheight()) - 2)); int picheight = (integer) (math.round(window.getheight() - 3)); //==================== end ==================== //==================== first panel ==================== panel.setsize(new dimension(panelwidthandhight, panelwidthandhight)); panel.setlocation(1, 1); amounttext1.setsize(new dimension(textwidth, textheight)); littretext1.setsize(new dimension(textwidth, textheight)); //==================== end ==================== //==================== second panel ==================== panel2.setsize(new dimension(panelwidthandhight, panelwidthandhight)); panel2.setlocation(location1, 1); amounttext2.setsize(new dimension(textwidth, textheight)); littretext2.setsize(new dimension(textwidth, textheight)); //==================== end ==================== //==================== third panel ==================== panel3.setsize(new dimension(panelwidthandhight, panelwidthandhight - 40)); panel3.setlocation(1, location1); amounttext3.setsize(new dimension(textwidth, textheight)); littretext3.setsize(new dimension(textwidth, textheight)); //==================== end ==================== //==================== third panel ==================== panel4.setsize(new dimension(panelwidthandhight, panelwidthandhight - 40)); panel4.setlocation(location1, location1); amounttext4.setsize(new dimension(textwidth, textheight)); littretext4.setsize(new dimension(textwidth, textheight)); //==================== end ==================== panel5.setsize(new dimension(picwidth, picheight)); pic.setlocation(xy[0], xy[1]); window.invalidate(); } @override public void componentmoved(componentevent e) { //to change body of generated methods, choose tools | templates. } @override public void componentshown(componentevent e) { //to change body of generated methods, choose tools | templates. } @override public void componenthidden(componentevent e) { //to change body of generated methods, choose tools | templates. } });
and that's ...
and hope answer others.
Comments
Post a Comment