Java "zooming" Canvas? - java

I have a java.awt.canvas object and I draw stuff with the Graphics2D (which I get from the bufferStrategy) and I'd like to "zoom" in and out.
So if I zoom in (scaling it up by a factor of 1) such that a line I draw from (0,0) to (10,10) Would be in reality drawn from (0,0) to (20,20)
Is this possible, or do I have to implement this myself?

Take a look at Graphics2D: http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html
You apply a suitable transformation to the graphics to achieve many transformations, rotate, scale (aka zoom) and translation. Simplest way to zoom would probably be
graphics2d.scale(2.0, 2.0); // draw everything twice the original size

Related

With Java's graphics shape drawing operations, is there an easy way to give the shapes transparency gradients and blurry edges?

What I would like is to give circles a fill color with a gradient that starts from the middle and then as it moves out to the edges becomes progressively more transparent, giving a blur effect. What is the simplest way of doing this?
Try setting an appropiately defined java.awt.RadialGradientPaint (using Colors with alpha), then render your circles using that. You may need to translate the graphics coordinate system to get the gradient centered in the circle. (http://docs.oracle.com/javase/7/docs/api/java/awt/RadialGradientPaint.html)
Or just make an image in a graphics program and simply draw the image.

LWJGL Camera stretches and shrinks shapes

I've been looking around and i couldn't find an answer to this but what I have done is create a cube / box and the camera will squash and stretch depending on where I am looking at. This all seems to resolve it self when the screen is perfectly square but when I'm using 16:9 it stretches and squashes the shapes. Is it possible to change this?
16:9
and this is 500px X 500px
As a side question would it be possible to change the color of background "sky"?
OpenGL uses a cube [-1,1]^3 to represent the frustum in normalized device coordinates. The Viewport transform strechtes this in x and y direction to [0,width] and [0,height]. So to get the correct output aspect ratio, you have to take the viewport dimensions into account when transfroming the vertices into clip space. Usually, this is part of the projection matrix. The old fixed-function gluPerspective() function has a parameter to directly create a frustum for a given aspect ratio. As you do not show any code, it is hard to suggest what you actually should change, but it should be quite easy, as it boils down to a simple scale operation along x and y.
To the side question: That color is defined by the values the color buffer is set to when clearing the it. You can set the color via glClearColor().

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.)

java2d and scale

when I scale my shape [Rectangle] with g2d.scale(2,2) or with AffineTransform , I can't detect correct hit on shape because my shape scale still same as previous and only view of shape is change.
I need solution for solving this problem [change size of shape after Transform].
thanks
You can either use an inverse transform, as shown here, or do the scaling explicitly, as shown here.

Rotation and Scaling -- How to do both and get the right result?

I've got a set of Java2D calls that draw vectors on a graphics context. I'd like for the image to be doubled in size and then rotated 90 degrees.
I'm using the following code to do this:
Graphics2D g2 = // ... get graphics 2d somehow ...
AffineTransform oldTransform = g2.getTransform();
AffineTransform newTransform = (AffineTransform)oldTransform.clone();
newTransform.concatenate(AffineTransform.getTranslateInstance(x1, x2));
newTransform.concatenate(AffineTransform.getScaleInstance((double)newW/(double)iconW, (double)newH/(double)iconH));
newTransform.concatenate(AffineTransform.getRotateInstance(Math.toRadians(rotationAngle), (double)iconW/2.0d, (double)iconH/2.0d));
// ... do my drawing ...
This rotates and scales, however, the scale isn't applied the way I would like. It is as if it is rotated before scaling, thus making the image wider on the wrong axis.
Is there a better way to do this?
I believe those transforms are implemented like a stack - so the last transform is performed first. Try reversing the order of the rotate and scale transformations and you should get what you are looking for.
newTransform.concatenate(AffineTransform.getTranslateInstance(x1, x2));
newTransform.concatenate(AffineTransform.getRotateInstance(Math.toRadians(rotationAngle), (double)iconW/2.0d, (double)iconH/2.0d));
newTransform.concatenate(AffineTransform.getScaleInstance((double)newW/(double)iconW, (double)newH/(double)iconH));
Rotations are always performed about the origin. In order to rotate about a certain point you must translate the points.
This page explains the maths behind what you're trying to do and show why transformations need to be applied in a certain order.
Change the order in which you concatenate the transforms to control the order in which they are applied in the composite.

Categories

Resources