3

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?

Petter Thowsen
  • 1,697
  • 1
  • 19
  • 24

1 Answers1

4

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
Durandal
  • 19,919
  • 4
  • 36
  • 70
  • It seems I'm using the Graphics class and not Graphics2D. I'm using the Canvas, how would I get hold of a Graphics2D object? *edit* I figured it out! I had to typecast when I call the ``bufferStrategy.getDrawGraphics()``. – Petter Thowsen Oct 19 '13 at 14:44
  • I noticed that the FPS dropped down from 6000fps to 3000fps when I scaled to 2. Is this normal? :\ Btw, this is for a game. – Petter Thowsen Oct 19 '13 at 17:20
  • @PetterThowsen Its quite possible the performance varies ten to hundredfold depending on the transformation and rendering hints applied to a graphics. The graphics implementation may be able to draw things hardware accelerated in some case, and scaling the output may interfere with that switching it to software rendering. Depends on a lot of factors, so its hard to tell. I wouldn't worry about halved speed though. I have written a prototype for a game using just basic passive rendering with image tiles and it ran on 60fps (and it was on 2003 hardware). – Durandal Oct 20 '13 at 14:36
  • (not enough space) Worry about performance when it turns out to be a problem, not while you still develop the basics. If you can stick to basic 2d-rendering, it should be fast enough, if you need fancy 3d-effects consider switching to openGL based rendering (there are premade 2d-game engines that use openGL for rendering, but I have no experience which of those projects are good to use). – Durandal Oct 20 '13 at 14:39