tags:

views:

141

answers:

1

I have a Java applet, which is a form that draw shapes into it (Rect, Oval, Line). Each shape is represented by 2 points and can draw itself to the form. When the JApplet form resizes, I need to resize the shapes also while keeping the aspect ratio.

I didn't find an high quality solution for doing this that solves this problem.

Tried to write a solution from this, but it came up as lousy when tested, Can someone publish an example code for doing that please?

A: 

You give no details but I am guessing that when you scale the object moves as well as scales. You will need to know something about graphics transformations. There are two main approaches:

  • resize the object by scaling its coordinates

  • apply a transformation matrix to the object. This is probably the better way.

If you are using Graphics2D (http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics2D.html) you will have access to transformations.

When you resize the window you will want to work out which dimension (width or height) has most effect on the scale and take the others one - this will stop the object expanding too much. Calculate a single scale and apply it equally to X and Y.

You will probably not have the object on the origin. In this case you need to carry out three operations: - translate the object to the origin - rescale it - translate it back to where it was

These operations can be combined into a single matrix.

I cannot be more definite as I do not know the details of your problem but this should give you some pointers.

Try out the examples in: http://www.java2s.com/Code/Java/2D-Graphics-GUI/Linetransformationrotationshearscale.htm which should show you some of the effects.

peter.murray.rust