Zoom in/out function

Hi i am making a photo editing program where all the canvases extend Jpanel and are stored in JInternalFrames.

what i am trying to do is to make a decent zoom in/out function where everything is properly in proportion without losing any image quality when you zoom back to 100%.

when it is zoomed in or out the pixels need to be the right size so that if you draw a dot when it is zoomed in it will be the right size when you zoom back out...if that makes sense!

any help is appreciated, i just need a point in the right direction.

thanks.

[570 byte] By [boblettoj99a] at [2007-11-27 6:35:46]
# 1
Have a look at [url= http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics2D.html#scale(double, double)]Graphics2D.scale[/url] (all the Graphics objects passed to a JComponent's paintComponent-method are instances of Graphics2D).
thomas.behra at 2007-7-12 18:03:01 > top of Java-index,Java Essentials,Java Programming...
# 2
iv tried that and it gave me some pretty weird results.i dont know exactly how to implement it properly :(
boblettoj99a at 2007-7-12 18:03:01 > top of Java-index,Java Essentials,Java Programming...
# 3

> iv tried that and it gave me some pretty weird

> results.

Like what?

> i dont know exactly how to implement it properly :(

You don't implement it, you use it. E.g.// create a temporary Graphics context

Graphics2D gfx = (Graphics2D)g.create();

// apply zoom level, e.g. 2

gfx.scale(2, 2);

// other painting code

// remember to paint onto gfx and not onto g

// clean up the temporary context

gfx.dispose();

thomas.behra at 2007-7-12 18:03:01 > top of Java-index,Java Essentials,Java Programming...
# 4
yeh thats what i did, but it didnt resize what was already painted on to the canvas. Also when i tried using the pen tool when i "zoomed in" it would draw about 50 pixels up and to the left of where it was meant to draw.
boblettoj99a at 2007-7-12 18:03:01 > top of Java-index,Java Essentials,Java Programming...
# 5

> yeh thats what i did, but it didnt resize what was

> already painted on to the canvas.

Well, obviously not. If you change your zoom level, you have to call your JComponent's repaint-method.

> Also when i tried using the pen tool when i "zoomed in" it would draw

> about 50 pixels up and to the left of where it was meant to draw.

You will need to convert between component coordinates (which are the coordinates generated by mouse events) and canvas coordinates (which depend on the transform applied to your Graphics context).

thomas.behra at 2007-7-12 18:03:01 > top of Java-index,Java Essentials,Java Programming...
# 6
how and when do i need to convert them?i have tried multiplying the coordinates by the scale factor but that causes problems when you zoom back to 100%.I tried it in the actual paintComponent method and also the second they are painted but neither seem to work as they
boblettoj99a at 2007-7-12 18:03:01 > top of Java-index,Java Essentials,Java Programming...
# 7
please someone i need help desperatelyi think im going insane!!!
boblettoj99a at 2007-7-12 18:03:01 > top of Java-index,Java Essentials,Java Programming...
# 8
iv uploaded the jar file of it if it helps you understand it better.just play around with the zoom slider and you'll see what i mean:rocketeermusic.co.uk/tplFiles/Paintpage.html
boblettoj99a at 2007-7-12 18:03:01 > top of Java-index,Java Essentials,Java Programming...
# 9

> how and when do i need to convert them?

>

> i have tried multiplying the coordinates by the scale

> factor but that causes problems when you zoom back to

> 100%.

>

> I tried it in the actual paintComponent method and

> also the second they are painted but neither seem to

> work as they should.

To convert a point when you click it is like that:

public void mousePressed(MouseEvent e)

{

Point point = e.getPoint();

// We must put the point in the user's view : rescale the point

point.x/=getZoom();

point.y/=getZoom();

// Do from here what you want

}

To rescale a drawing, it is like that:

public void repaint(Graphics g)

{

g.scale(zoom, zoom);

g.drawImage(...);// From here you can draw what you want

}

arno_ba at 2007-7-12 18:03:01 > top of Java-index,Java Essentials,Java Programming...