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);
}
}
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