Zooming for a Graphics2D object
I currently have a Graphics2D object which is acting as a perpendicular coordinate system(regular x-y system). I want to implement zooming which acts on a mouse listener. I have thought of getting a BufferedImage with 开发者_开发百科Robot Class and then using PixelGrabber to zoom in, but wondered if I can directly work with Graphics2D without using Image objects.
Yes. You can use an AffineTransform (AffineTransform.getScaleInstance in particular) to scale all aspects of a graphics context. You could use that to either zoom in (create a larger scale) or zoom out (use a scale less than 1). I have a blog post that shows how to use AffineTransforms for things like rotation and translation, and not so much scaling, but it's a similar principle. You'll have to be mindful of how scaling the graphics context affects things like selection, listeners, etc. (Your coordinate system for the panel will need to be converted into the coordinate system for the graphics context in order to correctly select/click on objects in the scaled graphics context.)
精彩评论