简体   繁体   中英

Add a JPanel to a Graphics Panel and making the JPanel Transparent except for objects

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.

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 .

there are a few ways, proper could be to

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM