3

I am trying to make a Blackjack Game and way I want to design my program is with a graphics panel (Images, drawing of cards, etc.) and on top of that panel a JPanel with buttons. I want this JPanel to be transparent so that the Graphics Panel underneath is Visible but the JButtons do not turn transparent as well.

If someone can send me in the right direction?

Graphic Layer:

public class GraphicsBoard {
    String[] fileName = { "cards.png", "BlackJackBoard.png" };
    ClassLoader cl = GraphicsBoard.class.getClassLoader();
    URL imgURL[] = new URL[2];
    Toolkit tk = Toolkit.getDefaultToolkit();
    Image imgCards, imgBG;

    public GraphicsBoard() throws Exception {
        for (int x = 0; x < imgURL.length; x++)
            imgURL[x] = cl.getResource("pictures/" + fileName[x]);
        imgCards = tk.createImage(imgURL[0]);
        imgBG = tk.createImage(imgURL[1]);
    }

    public void paintComponent(Graphics g) {
        g.drawImage(imgBG, 0, 0, 550, 450, 0, 0, 551, 412, this);

        Graphics2D g2 = (Graphics2D) g;
        for (int x = 0; x <= 550; x += 50) {
            g2.drawLine(x, 0, x, 450);
            g2.drawString("" + x, x + 5, 20);
        }
        for (int y = 5; y <= 450; y += 50) {
            g2.drawLine(0, y, 550, y);
            g2.drawString(" " + y, 0, y + 20);
        }
    }
}

Button Layer:

public class OverBoard extends JPanel implements ActionListener{
    JButton btnDeal = new JButton("Deal");

    public OverBoard(){
        btnDeal.addActionListener(this);
        add(btnDeal);
        setOpaque(false);
    }
}

I want the ButtonLayer to be on top of the GraphicLayer.

Exikle
  • 1,155
  • 2
  • 18
  • 42

2 Answers2

3

I want this JPanel to be transparent so that the Graphics Panel underneath is Visible but the JButtons do not turn transparent as well.

an OverlayLayout JPanel will do what you descibe.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Michael Dunn
  • 818
  • 5
  • 3
  • even suggestion is correct +1, and I love [very short answers have to](http://stackoverflow.com/faq#deletion) 1) use more than one short ..., 2) add link to API or code example, otherwise this answer could be deleted by users here, doesn't matter if is accepted, upvoted or downvoted – mKorbel Jan 03 '13 at 11:51
2

there are a few ways, proper could be to

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319