Dear Craig,
Thanks. From what I understand it is the shape s that has its co-ordinates changed by the Affine Transform.
I am not clear about the following - any explanations would be most welcome.
1. When the Affine Transform "at" is created does it in effect convert all of the JPanel into its new co-ordinate space.
2. When I save and reopen a file with the code below then it works correctly - i.e. the objects - generators are in the positions where they were last left. How does this work because I do not save the Affine Transform "at"
To repopulate the ArrayList for Generators the following code is used
The whole canvas is then repainted.
Finally, would you mind reading my explanation of how I think the program is working at some point - This is for my dissertation. There is a chance that some poor soul might have to interpret this program and develop it at a later date and I do not want to lead him or her up a blind alley.
The Drawing Canvas provided by the ShapePanel class is based on Affine Transforms. Affine Transforms were chosen because of the Zoom feature. Affine Transforms are a concept developed in Euclidean Algebra so that ratios are preserved between items - in essence it is the co-ordinate space that is zoomed not the object itself. This is important otherwise objects will seem to get closer to eachother as they are zoomed into.
The Affine Transform is created in the ShapePanel class
private AffineTransform at = new AffineTransform();
The AffineTransform class is used to move and scale and create electrical items of the
Java Shape class. The mouse events are performed on the AffineTransform not on the electrical item as defined in the electrical item class itself. The key method in the paintComponent method is the following line of code
Shape s = at.createTransformedShape(shpnew);
This creates a new object 's' of the Java Shape class. The java Shape class is not to be confused with the Shapes class in the translation package which is a generic class which defines the methods that can be performed on any electrical item. The above code is used to create an affine transformed Shape s which is then drawn. This is done for all of the electrical objects as they come out of the ArrayList. Each of the electrical objects - transformers, transmission lines etc are Shapes created by one Affine Transform 'at'.
The code then provides for all of the electrical objects to be moved on the canvas by the following method
public void movePrimitives(int x, int y) {
System.out.println("Goodbye");
at.translate(x, y);
repaint();
}
It is the use of Affine Transforms that allow this. We are moving the Affine Transform 'at' and all the objects that have been created from it. Again, in the method 'zoomup' where the objects on the canvas are resized, it is due to the use of the Affine Transform that we can do this.
public void zoomup(int p) {
// This keeps the center of the component in place.
cx = this.getWidth() / 2;
cy = this.getHeight() / 2;
at.setToTranslation(cx, cy); //Reset atf*/
at.scale(p, p);
at.translate(-cx, -cy); //bring back the objects to the center
repaint();
}
The actions take place on the Affine Transform and as the electrical objects are part of the Affine Transform they are affected by these actions.
Kind regards,
Kieran