Mirror Shape in JAVA(Swing) -


Hello everyone,

I have a homework in which to draw and add shapes to swing GUIs Includes. / P>

I have a problem that when I try to mirror my shapes, then I get no results.

The Drawallnodes method is called in the Pencil Paint Combonant.

  Public Zero Drawing (ArrayList & lt; DevicesEditor & gt; Nodes, Graphics2DG2) {int arraysize = nodes.size (); ArrayList & LT; DevicesEditor & gt; Temparray; AffineTransform = New AffineTransform (); If (nodes.size ()! = 0) {System.out.println ("nodes.size" + nodes.size ()); If (currentreandex> = 0) {AffineTransform afx = New AffineTransform (); // to rotate (int i = 0; i & lt; = currentarrayindex; i ++) {if (nodes.get (i) .getWasAngleChanged ()) {Afx.rotates (Math.tudian (nodes.gate) (I) .getAngleInDegrees ()), nodes.jet (i) .getCenter () .x, nodes.gate (i) .getCenter (). Y); Nodes.get (i) .setShape (afx.createTransformedShape (nodes. Gate (i) .getShape ())); Nodes.get (i) .setWasAngleChanged (wrong); Nodes.get (i) .setokrajRectangle (); } Try {rectangle = nodes. (I) .getShape () GetBounds (); } Hold (Exception e) {System.out.println ("foundbomb found exception, no get size with getbounds"); } AffineTransform saveXform = g2.getTransform (); G2.setColor (nodes.get (i) .getColor ()); Int w = getWidth (); // it gets the JPanels width, which is set to 758px = AffineTransform.getTranslateInstance (w, 0); System.out.println ("Get the width of the figure *********:" + w); At.scale (-1, 1); // mirror-x, y g2.setPaint (Color.red); G2.draw (at.createTransformedShape (nodes.get (i) .getShape ())); {G2.drawString (nodes.get (i) .getText (), (int) nodes.get (i) .getCenter () GetX (), (int) nodes.get (i) .getCenter (). ()); } Hold (exception e) {System.err.println ("no text on node"); } Try {g2.draw ((size) nodes. Gate (i) .getShape ()); } Hold (exception e) {System.err.println ("No size was found on the node"); } // g2.transform (AffineTransform.getRotateInstance (0, 1)); G2.setTransform (saveXform); }}}}  

When I mirror the shape, for example I pull it to the right, but mirrored image is visible on the left ... I want to reflect the size < / P>

When you change the shape with a simple denominator such as Scale (-1,1) then it will monitor horizontally x = 0 . That is, when you have (300,100) objects, it will be later (-300100)

  • To mirror the object in "place" (that is,
  • Mirror the object
  • Move the object back to its original state
  • Move the object to its original state
  • / Li>

This sequence of changes (and should be required) can be presented by a single AffineTransform which AffineTransforms concat For example, you can create a method such as

  Personal static shape mirrorAlongX (double X, shape shape), which can be achieved by enating  {AffineTransform at = new AffineTransform (); At.translate (x, 0); At.scale (-1, 1); At.translate (-x, 0); Return on. Contract format (shape); }  

To paint a shape on which a horizontal (i.e. mirror) flip in your center, you can then call

  G.fill (mirrorAlongX (Shape.getBounds2D). GetCenterX (), size));  

BTW: Get rid of all these exceptions ! This is a terrific style such as replace the exception check that prints "no text on the node"

  string text = nodes. Gate (i) .getteext (); If (text! = Null) {g2.drawString (text, ...); } Else {System.err.println ("No text found on node"); // can also not be required ...}  

Comments