Adding JComponent on top of full-screen graphics

I have a full-screen program that draws a lot of things for the game I am doing. A canvas is the whole window that should be. Now I want to add a button or text field to my window (JButton and JTextField, etc.). However, when I add a button or something else, they leave for the canvas.

Do you have an idea how to make a button appear on the canvas, so I can click on it?

Edit: Drawing Code:

public synchronized void draw(Graphics2D g) {
        if (!welcome)
            g.drawImage(background,0,0,null);

        if (welcome) {
            g.drawImage(new ImageIcon (getClass().getResource("images/backgroundWelcome.jpg")).getImage(), 0,0, null);
        }
        else if (gettingName) {
            setFont (new Font("Verdana", Font.ITALIC, 40));

            g.draw3DRect(center.x -325, center.y -50, 650, 100, true);
            g.drawString("Please enter your name", center.x -250, center.y -120);
            g.drawString(name, center.x -315, center.y);
        }
        if (game) {
            for (Player p: player) {
                int x = center.x;
                int y = center.y;

                switch (p.getID()) {
                case 1:
                    x -= name.length()*10;
                    y *= 1.4;
                    break;
                case 2:
                    x -= name.length()*10;
                    y /= 1.4;
                    break;
                case 3:
                    x *= 1.4;
                    x -= name.length()*10;
                    break;
                case 4:
                    x *= 0.6;
                    x -= name.length()*10;
                    break;
                default:
                    System.out.println("Error 2");
                }
                g.drawString(p.getName(),x,y);
            }
            for (int x = 0; x<13; x++) {
                g.drawImage(diamonds[x].getImage(), (int) diamonds[x].getX(), (int) diamonds[x].getY(), null);
                g.drawImage(clubs[x].getImage(), (int) clubs[x].getX(), (int) clubs[x].getY(), null);
                g.drawImage(hearts[x].getImage(), (int) hearts[x].getX(), (int) hearts[x].getY(), null);
                g.drawImage(spades[x].getImage(), (int) spades[x].getX(), (int) spades[x].getY(), null);
            }
            g.drawImage(put.getImage(), (int) put.getX(), (int) put.getY(), null);
        }

        if (won) {
            g.drawImage(new ImageIcon (getClass().getResource("images/backgroundWelcome.jpg")).getImage(), 0,0, null);
        }
        else if (lost) {
            g.drawImage(new ImageIcon (getClass().getResource("images/backgroundWelcome.jpg")).getImage(), 0,0, null);
        }
    }

Edit2: My frame is a JFrame.

+3
source share
2 answers

It's hard to say a lot from the code snippet (please consider publishing SSCCE in the future), but:

  • Do not encode AWT in this millennium.
  • Do not mix Swing with AWT (at least until Java 7)
  • draw() ( paintComponent()).
  • , Swing JComponent JPanel, , .
+2

, , , , .

+2

All Articles