zoom the selected area in an image

hi all,

i heve created a an application in which user can select an area and on an image and then zoom that particular portion but when i zoom it the image keeps on going to the right bottom corner of the screen.I want my zoom to keep the selected area in the center of the screen.can anybody help me in thet.

thanks

sangeeta

[357 byte] By [sangeeta_bg] at [2007-9-26 3:54:37]
# 1
Please post the zooming code and the calculations you make so that I can offer a solution that best suits your current situation
kaze0 at 2007-6-29 12:43:58 > top of Java-index,Security,Cryptography...
# 2

hi,

i can't send u the full code but i can send u the code written in paint.tell me one more thing if something else is also written in the paint for some other function will it create problems in painting for my function:

Paint

...

if(fence_zoom)

if (currentRect != null) {

System.out.println("hello");

g.setColor(Color.white);

g.drawRect(rectToDraw.x, rectToDraw.y,

rectToDraw.width - 1, rectToDraw.height - 1);

System.out.println("drawing rectangle");

bi = bimg.getSubimage(rectToDraw.x, rectToDraw.y, rectToDraw.width, rectToDraw.height);

System.out.println(rectToDraw.x+","+ rectToDraw.y);

System.out.println("drawing image");

//g.drawImage(bi,rectToDraw.x, rectToDraw.y,this);

/*

g=bi.getGraphics();

Graphics2D g3=(Graphics2D)g;

t= g3.getTransform();

g3.setTransform(t);

System.out.println("settransform callrd");

zoomFactor *=1;

t.setToScale(zoomFactor, zoomFactor);

// t.translate(1.0,1.0);

System.out.println("setto Scale called");

g2.drawImage(bi, 0,0,this);

repaint();

sangeeta_bg at 2007-6-29 12:43:58 > top of Java-index,Security,Cryptography...
# 3

hi,

i too had the same problem...so i got a piece of code which will zoom with respect to the center co-ordinates...may be u can modify it a bit and can use to it...it worked for me...

here the simple example

import java.awt.*;

import java.awt.geom.*;

import java.applet.*;

//<applet code=ScaleApplet width=400 height=400> </applet>

public class ScaleApplet extends Applet

{

private RectangularShape normalShape;

private Shape bigShape;

public void init()

{

normalShape = new Rectangle(50, 50, 30, 20);

bigShape = createScaledRectangularShapeFromCenter(normalShape, 3.0, 3.0);

}

public void paint(Graphics g)

{

Graphics2D g2 = (Graphics2D) g;

g2.draw(normalShape);

g2.draw(bigShape);

}

static public Shape createScaledShapeFromPoint(Shape s, Point2D.Double p, double sx, double sy)

{

AffineTransform at = new AffineTransform();

// initially set to identity

at.translate(p.x, p.y);

at.scale(sx, sy);

at.translate(-p.x, -p.y);

return at.createTransformedShape(s);

}

static public Shape createScaledRectangularShapeFromCenter(RectangularShape rs, double sx, double sy)

{

return createScaledShapeFromPoint(rs, new Point2D.Double(rs.getCenterX(), rs.getCenterY()), sx, sy);

}

}

ravigoel at 2007-6-29 12:43:58 > top of Java-index,Security,Cryptography...
# 4

hi,

thanks for the reply.i've already done that with the help of translate.but my problem is that once i zoom the image on mouse release i want to zoom it further on mouse click.i'm not able to get values from mouse relaeae to mouse click as height nd width becomes zero as soon as i click the mouse at the same place.any suggestions?

thanks

sangeetai

sangeeta_bg at 2007-6-29 12:43:58 > top of Java-index,Security,Cryptography...
# 5
how can i do this with a lot of shapes.
shayhandelman at 2007-6-29 12:43:58 > top of Java-index,Security,Cryptography...
# 6

hi,

my problem is that once i'm able to zoom my image ,if i want to zoom it further either on mouse click or selecting a rectangle again on mousedrag,it throws an exception coz i'm not able to get either size or bofferedimage from the once zoomed image.

do u have any suggestions:

i'm sending my latest code

private Graphics2D createGraphics2D(int w, int h) {

Graphics2D g2 = null;

if (bimg == null || bimg.getWidth() != w || bimg.getHeight() != h) {

bimg = (java.awt.image.BufferedImage) createImage(w, h);

}

g2 = bimg.createGraphics();

g2.setBackground(getBackground());

g2.clearRect(0, 0, w, h);

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

return g2;

}

public void update(Graphics g) {

paint(g);

}

public void paint(Graphics g) {

super.paint(g);

try{

t=new AffineTransform();

//////added 09aug

////

Dimension d = getSize();

System.out.println(d);

if(!first_paint){

setScale(d.width,d.height - DRG_YPOS);

first_paint = true;

}

if(!image1)

Graphics2D g2 = createGraphics2D(d.width, d.height -DRG_YPOS);

g2.translate(((d.width/2) - ((zoomx*maxx + zoomx*minx)/2))

+ zoomx*scrollx,

(((d.height-DRG_YPOS)/2))+scrolly);

g2.scale(zoomx,zoomy);

draw(d.width, (int)(maxy - (maxy- miny)/2) , g2);

g.drawImage(bimg, 0, DRG_YPOS, this);

System.out.println("image drawn for the "+counter+"time");

counter++;

System.out.println("counter is"+counter);

System.out.println("image1 is"+image1);

file://t= g2.getTransform();

if(fzoom1)

{

g.setColor(Color.red);

g.drawRect(beginX,beginY,end1X-beginX,end1Y-beginY);

System.out.println("drawing rect");

}

if(fzoom){

wx=beginX;

wy=beginY;

ww=end1X-beginX;

wh=end1Y-beginY;

bi = bimg.getSubimage(wx,wy,ww,wh);

System.out.println("hello 2d");

System.out.println(wx+","+wy+","+ww+","+wh);

Dimension d1=getSize();

Graphics2D g2d = bi.createGraphics();

// g2d.setBackground(getBackground());

int w=500;

int h=500;

System.out.println(w+","+h);

System.out.println("g2d"+g2d);

System.out.println("settransform callrd");

// zoomFactor *=1;

g2d.translate(100.0f, 100.0f);

g2d.scale(zoomFactor, zoomFactor);

System.out.println("scale called"+zoomFactor);

file://zoomFactor++;

g.drawImage(bi,50,50,w,h,this);

System.out.println("w,h,bi"+w+","+h+","+bi);

file://repaint();

image1=true;

file://zoomFactor++;

file://fzoom=false;

}

g2.dispose();

}catch(Exception e)

{

System.out.println("exception"+e);

}

}

class IMS2DFrameMouseAdapter extends MouseAdapter implements MouseMotionListener{

int mx;

int my;

int endx,endy;

int X;

int Y;

Point point;

public void mouseClicked( MouseEvent event ) {

if(select)

{

Dimension d = getSize();

AffineTransform at = new AffineTransform();

at.setToIdentity();

at.translate(((d.width/2) - ((zoomx*maxx + zoomx*minx)/2))

+ zoomx*scrollx,

(((d.height-DRG_YPOS)/2))+scrolly);

at.scale(zoomx,zoomy);

Point src = new Point(event.getX(),event.getY() - DRG_YPOS);

Point pt = new Point();

try{

at.inverseTransform( src, pt);

}

catch( NoninvertibleTransformException e)

{

e.printStackTrace();

}

pt.setLocation(pt.getX(), (maxy - (maxy- miny)/2 - pt.getY()));

hitObject(pt);

}

/*if(fence_zoom){

fzoom2=true;

}else

{

fzoom2=false;

}

*/

}

public void mousePressed( MouseEvent event ) {

if(fence_zoom)

{

System.out.println("mouse Pressed");

beginX = event.getX();

beginY = event.getY();

repaint();

}

}

public void mouseReleased( MouseEvent event ) {

end1X = event.getX();

end1Y = event.getY();

System.out.println("ENDX"+endx);

System.out.println("endy--"+endy);

if(translation)

translatedrawing(endx - startx,endy - starty);

if(fence_zoom){

System.out.println("mouse released");

file://end1X = event.getX();

//end1Y = event.getY();

fzoom=true;

repaint(); file://updateSize(event);

}

else{

fzoom=false;

}

}

public void mouseDragged(MouseEvent event) {

if(fence_zoom){

end1X = event.getX();

end1Y = event.getY();

fzoom1=true;

System.out.println("mouse dragged");

repaint();

}

else {

fzoom1=false;

}

}

public void mouseMoved(MouseEvent event)

{

}

}

thanks

sangeeta

"Gustavo Henrique Soares de Oliveira Lyrio" wrote:

I think you are using the wrong drawimage method. If you have the selection retangle coordinates, you will need a method that draw this selection in a new retangle selection (your canvas or panel)

Where goes my paintMethod:

protected void paintComponent(Graphics g)

{

g.drawImage(image, START_WINDOW_X, START_WINDOW_Y, WINDOW_X, WINDOW_Y, px1, py1, px2, py2, this);

}

Take a look in the drawimage methods in the java web page. I think it will help.

[]`s

Gustavo

-- Original Message --

From: sangeetagarg

To: Gustavo Henrique Soares de Oliveira Lyrio

Sent: Thursday, August 16, 2001 9:14 AM

Subject: Re: Re: re:zoom in 2d

i'm sending u the paint method.but still i'm not able to select the desired area .it zooms the while image.tell me if there is any error in the code.

file://if(fence_zoom)

if(fzoom1 )

if (currentRect != null) {

System.out.println("hello");

g.setColor(Color.white);

g.drawRect(rectToDraw.x, rectToDraw.y,

rectToDraw.width - 1, rectToDraw.height - 1);

System.out.println("drawing rectangle");

fzoom1=false;

}

if(fzoom)

{

bi = bimg.getSubimage(rectToDraw.x, rectToDraw.y, rectToDraw.width, rectToDraw.height);

System.out.println(rectToDraw.x+","+ rectToDraw.y+","+rectToDraw.width+","+ rectToDraw.height);

Graphics2D g2d = bi.createGraphics();

g2d.setBackground(getBackground());

g2d.clearRect(0, 0,0,0 );

file://ZoomIn();

file://t= g2d.getTransform();

file://g2d.setTransform(t);

System.out.println("settransform callrd");

// zoomFactor *=1;

g2d.translate(100.0f, 0.0f);

g2d.scale(zoomx, zoomy);

System.out.println("scale called"+zoomFactor);

zoomx += ZOOMSTEP;

zoomy += ZOOMSTEP;

file://zoomFactor+=zoomFactorupdate;

g.drawImage(bi, 0, 0, this);

// g.drawImage(bi,10,10,this);

repaint();

fzoom=false;

}

thanks

sangeeta

sangeeta_bg at 2007-6-29 12:43:58 > top of Java-index,Security,Cryptography...
# 7

How a can catch the image of background of the Drawrec tthe code is these

public void paint(Graphics g)

{

if (null!=image)

{

g.drawImage(image, 0, 0, this);

g.setColor(Color.black);

//int x, int y, int with,int height,int dx,int dy;

//i need to catch these coordenates because they are of the mouse

//can i copy these coordenates for a file or to (this) (Jframe/windows..etc)

g.drawRect(x_start,y_start,x_end-x_start,y_end-y_start);

tmp2=g;

}

}

My email Adrianorfgomes@hotmail.com

Thks ;)

Olhinhoazul at 2007-6-29 12:43:58 > top of Java-index,Security,Cryptography...