java - Zoom image in and out also change JscrollPane accroding to mouse position -
i want zoom image in when left mouse click , out when right mouse click, easy part. portion of image after zoom want show mouse is.
e.g image of car , mouse position on car wheel image should zoom focus should remain on car. that's tired far.
package paractice; import java.awt.borderlayout; import java.awt.flowlayout; import java.awt.graphics2d; import java.awt.image; import java.awt.mouseinfo; import java.awt.point; import java.awt.pointerinfo; import java.awt.renderinghints; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import java.awt.event.mousemotionlistener; import java.awt.geom.affinetransform; import java.awt.image.bufferedimage; import java.io.file; import javax.imageio.imageio; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.swingutilities; public class zoom extends jpanel implements actionlistener, mouselistener, mousemotionlistener{ private static final long serialversionuid = 1l; private jlabel imageadujuster = new jlabel(); private jpanel panel = new jpanel(); private jscrollpane pane = new jscrollpane(); private jbutton zoomin = new jbutton("zoom in"); private jbutton zoomout = new jbutton("zoom out"); private point point; private bufferedimage image; private double scale = 1; /* * constructor */ public zoom() { jpanel buttonpanel = new jpanel(new flowlayout(flowlayout.right)); buttonpanel.add(zoomout); buttonpanel.add(zoomin); panel.setlayout(new borderlayout()); panel.add(imageadujuster, borderlayout.center); pane.setviewportview(panel); pane.setautoscrolls(true); setlayout(new borderlayout()); add(pane, borderlayout.center); add(buttonpanel, borderlayout.south); zoomout.addactionlistener(this); zoomin.addactionlistener(this); imageadujuster.addmouselistener(this); imageadujuster.addmousemotionlistener(this); } private void getimage(string imagepath){ try { file imagefile = new file(imagepath); image = imageio.read(imagefile); setimage(image); }catch(exception e){ system.out.println("image file not found. "+ e.getmessage()); } } private void setimage(image image) { imageadujuster.seticon(new imageicon(image)); } private bufferedimage getscaledimage(double scale) { int w = (int)(scale*image.getwidth()); int h = (int)(scale*image.getheight()); bufferedimage bi = new bufferedimage(w, h, image.gettype()); graphics2d g2 = bi.creategraphics(); g2.setrenderinghint(renderinghints.key_interpolation, renderinghints.value_interpolation_bicubic); affinetransform @ = affinetransform.getscaleinstance(scale, scale); g2.drawrenderedimage(image, at); g2.dispose(); return bi; } public void zoomout(point point) { setimage(getscaledimage(scale * 0.9)); point pos = pane.getviewport().getviewposition(); int newx = (int)(point.x*(0.9f - 1f) + 0.9f*pos.x); int newy = (int)(point.y*(0.9f - 1f) + 0.9f*pos.y); pane.getviewport().setviewposition(new point(newx, newy)); this.pane.revalidate(); this.pane.repaint(); } /** * */ public void zoomin(point point) { setimage(getscaledimage(scale * 1.1f)); point pos = pane.getviewport().getviewposition(); int newx = (int)(point.x*(1.1f - 1f) + 1.1f*pos.x); int newy = (int)(point.y*(1.1f - 1f) + 1.1f*pos.y); pane.getviewport().setviewposition(new point(newx, newy)); this.pane.revalidate(); this.pane.repaint(); } public static void main(string args[]) { jframe frame = new jframe(); zoom zoom = new zoom(); zoom.getimage("c:\\users\\abcd1\\pictures\\picture.jpg"); frame.getcontentpane().add(zoom); frame.pack(); frame.setvisible(true); } public void actionperformed(actionevent e) { if(e.getsource() == zoomin) { scale ++; zoomin(point); } if(e.getsource() == zoomout) { scale --; zoomout(point); } } public void mouseclicked(mouseevent e) { if(swingutilities.isleftmousebutton(e)) { scale += 1; zoomin(point); } if(swingutilities.isrightmousebutton(e)) { scale -= 1; zoomout(point); } } public void mousemoved(mouseevent e) { pointerinfo info = mouseinfo.getpointerinfo(); point = info.getlocation(); system.out.println("point x "+point.x +" point y "+point.y); } public void mouseentered(mouseevent arg0) {} public void mouseexited(mouseevent arg0) {} public void mousepressed(mouseevent arg0) {} public void mousereleased(mouseevent arg0) {} public void mousedragged(mouseevent e) {} }
thank in advance.
to zoom , change jscrollpane
position changed 2 methods , added scaleandzoom(double)
:
public void zoomout(point point) { scaleandzoom( scale * 0.9f); } public void zoomin(point point) { scaleandzoom( scale * 1.1f); } private void scaleandzoom(double scale) { setimage(getscaledimage(scale)); //calculation not quiet accurate. need improve //see aqeel haider comment int movex = (int) ((scale* point.x) - point.x ) ; int movey = (int) ((scale* point.y) - point.y ) ; rectangle view = pane.getviewport().getviewrect(); view.setbounds(view.x+movex,view.y+movey, view.width, view.height); imageadujuster.scrollrecttovisible(view); }
Comments
Post a Comment