0

I am currently working on a GUI card game in Java that uses Swing and AWT libraries. I have created custom classes that extend JPanel to fit the needs of the game, and everything seems to be working fine when I display.

However, the containment hierarchy I am using consists of nested BoxLayouts, FlowLayouts and BorderLayouts.

I am trying to create a simple animation that makes a card move on top of another, and to do that I need to be able to alter the Absolute Position of the card I want to move in order to create some sort of motion.

My question is: is there a simple automatic way to transform a JPanel that is formatted with Layout Managers to one that is identical to it but formatted using Absolute Positioning (null Layout), in order for me to be able to alter the absolute positions of the elements?

Or even better, is there a way to control the Absolute Position of a JComponent or nested JPanel EVEN IF it was inside a container that uses some Layout Manager?

  • 1
    I'm not sure what animation you're specifically trying to achieve, but if for a single card, perhaps you will want to lift the card onto the glass pane, do your animation, and then place it back down into the GUI proper. – Hovercraft Full Of Eels Apr 16 '20 at 15:45
  • The animation is simply moving a card in a straight line from its original position to its final position which is on top of another card (as if to hide that other card). I am not sure what you mean by "life onto the glass pane"? – The Feadow Apr 16 '20 at 15:52
  • 2
    If I were doing this (and having done similar things), I would not use components for my cards but have my own card class with various methods and fields to help move the card around and position it accordingly when painting. I personally find moving components around to achieve animation doesn't work very well. Assuming that is what your implying by mentioning absolute positioning. – WJS Apr 16 '20 at 15:58
  • My cards extend the JPanel class and consist of multiple JLabels that are organized within the containing panel using a Layout Manager. – The Feadow Apr 16 '20 at 16:00
  • The problem is that I am too far in this now and I can't afford to start over. – The Feadow Apr 16 '20 at 16:01
  • *I am not sure what you mean by "lift onto the glass pane"?* - see: https://stackoverflow.com/a/6811800/131872. That example actually uses a JLayeredPane, but the concept is the same. The component is moved from the panel using the GridLayout and is dragged along the layered pane. When the mouse is released the label is added back to the grid. The concept of use a Glass Pane would be the same. See: [How to Use Root Panes](https://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html) for more information and examples on a glass pane. – camickr Apr 16 '20 at 16:01
  • I've looked into the glass pane thing, that will probably do the job.. Thanks a lot, everyone. – The Feadow Apr 16 '20 at 16:24

0 Answers0