-2

How do I repaint the farthest right card last and the left most first so the overlapping will reverse?

So I have a JPanel in flowlayout which contains JButtons. I want to emulate the feel of holding cards in hand, so I overlapped the JButtons. The problem is that the overlapping of the images of the JButtons is so that the numbers only appear at the bottom right corner, where I want them to be top left. This is what I have now:

enter image description here

ZOrdering didn't work, it only reversed the order of the cards (ace on other side, king first, etc). What I want is to repaint the farthest right card last and the left most first so the overlapping will reverse.

Edit: The code for constructing the hand:

hand= new JPanel();
    hand.setLayout(new FlowLayout(FlowLayout.CENTER, -45, 0));
    hand.setOpaque(false);
    for(int i=0; i<p1.getHand().size(); i++){
        JButton te=new JButton();
        //button customization, icons and such
        te.addActionListener(this);
        //here I tried the ZOrdering that didn't work
        //hand.setComponentZOrder(te, 0)
        hand.add(te);
    }
    this.add(hand);
    }
    //yes, the setBounds is long and weird 
    hand.setBounds(WINDW/2-((p1.getHand().size())*CARDWHAND*2/5), WINDH-CARDHHAND-30, p1.getHand().size()*CARDWHAND*4/5+50, CARDHHAND+30);

this layout manager is null

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
halfway258
  • 115
  • 13
  • 1
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) *"So I have a.."* ..question? What is your question? – Andrew Thompson Jan 27 '15 at 19:08
  • Sorry I don't have a definite question in the description. My question is, how do I repaint the farthest right card last and the left most first so the overlapping will reverse? – halfway258 Jan 27 '15 at 19:23
  • As Andrew already said, we need an MCVE (i.e. an example code) we can work with. Help us to help you reading those links and doing what is said in them C: – Frakcool Jan 27 '15 at 19:25
  • Have you tried adding in the cards in reverse order? – DaaaahWhoosh Jan 27 '15 at 19:41
  • The cards need to stay where they are, sorted from smallest to greatest. And add them in reverse order won't change the repainting, the number would still display at the bottom right corner. – halfway258 Jan 27 '15 at 20:11
  • Use a JLayeredPane and write your own layout manager – MadProgrammer Jan 27 '15 at 20:45
  • 1
    By the way, a component is generally painted in the order it was added (if I recall correctly) – MadProgrammer Jan 27 '15 at 20:46
  • I thought so too, it is weird that my jbuttons are acting that way.. I'll try the JLayeredPane, but I'm not thrilled about making my own layout manager. – halfway258 Jan 27 '15 at 20:53
  • @MadProgrammer, `By the way, a component is generally painted in the order it was added` - the components are painted based on ZOrder, from highest to lowest. A component is assigned a higher ZOrder as it added, so components are painted in reverse order. – camickr Jan 27 '15 at 21:50
  • @camickr Knew it was one way or the other... – MadProgrammer Jan 27 '15 at 21:52
  • Note: The question should be *in the question*, not in a comment. 2) That uncopmpilable code snippet above is not an MCVE. Follow the link and *read* the entire page. – Andrew Thompson Jan 28 '15 at 02:36
  • I must say: STOP using Null Layout, take a look at [Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). Probably your error has also something related to it. For better help sooner please post an MCVE which we can copy-paste and see the same issue as you – Frakcool Jan 28 '15 at 03:02

1 Answers1

3

Check out the OverlapLayout which can support this.

You may also want to use a JLabel instead of a JButton for this. The above link will explain the potential problem with a button, although I think this problem only occurs when you use the Border of the button.

camickr
  • 321,443
  • 19
  • 166
  • 288