2

I have a JPanel in which I draw triangles via the method drawPolygon. My Goal is to rotate the graphics around the X axis. I know that there is a method called rotate, but this method rotates only around the Z axis.

How can I rotate a graphic in a JPanel around the X axis?

Example of rotation

If the Graphics 2D library does not have the functionality to do this kind of rotation, please tell me how can I archive my goal otherwise. I don't mind switching to another GUI type like Java-fx or canvas. (But stay in Java)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • *"How can I rotate a graphic in a `JPanel` around the X axis?"* Do you mean to mirror it as though it started above the X axis and then ended up upside down and below the X axis? If so, an appropriate scaling `AffineTransform` should do it. – Andrew Thompson Dec 14 '16 at 16:36
  • @AndrewThompson I do not want to mirror it at the X axis, so no scaling. Here is a [visualisation](https://youtu.be/IKB1hWWedMk?t=8m40s) of what I ment. – NoSenseSenpai Dec 14 '16 at 17:37
  • Not following a link to a video. Get a screenshot or make a drawing of what you mean. – Andrew Thompson Dec 14 '16 at 17:52
  • @AndrewThompson Edited. – NoSenseSenpai Dec 14 '16 at 18:22

1 Answers1

4

Java 2D provides AffineTransform, but an affine transformation ensures that parallel lines remain parallel after transformation. As an alternative, consider JavaFX 3D, which provides a PerspectiveCamera for perspective projection; this complete example uses Rotate.Y_AXIS, but Rotate.X_AXIS will produce the desired effect. Also consider a Java Advanced Imaging Warp, cited here, which offers a WarpPerspective with PerspectiveTransform.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • *"an affine transformation requires that parallel lines remain parallel after transformation"* I'd always wondered about the wording of the docs on that matter (which suggested same to me, but I wasn't sure). It's a pity, given I'd wanted to use a transform to turn a 'flat' map of the world (as might be seen in [this answer](http://stackoverflow.com/a/18825844/418556)) into a globe. But the only way I could think to achieve that using core Java was with a transform. Out of luck on that! – Andrew Thompson Dec 15 '16 at 14:43
  • 1
    @AndrewThompson: Maybe a `PerspectiveTransform`, cited above, but I'm not sure Mercator has an inverse. Also, I didn't like the word "_requires_". – trashgod Dec 15 '16 at 21:15
  • See also [`javafx.scene.effect.PerspectiveTransform`](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/effect/PerspectiveTransform.html) – trashgod Dec 22 '16 at 16:31