Please help me with this, i'm trying to create a domino game (which a project in school) i'm a midway learner of Java Prog, but still i haven't know a lot. My problem is, i'm trying to draw over the background image of my playFrame, but the rectangle does not seem to show, but when i tried to remove the image, it showed up. so How can i put the rectangle over the image? Thank you for helping! Good day!

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


class MyCanvas extends JComponent {

  public void paintComponent(Graphics g) {
      super.paintComponent(g);
    g.drawRect (10, 10, 200, 200);
  }
}
public class DominoTRY extends JFrame implements ActionListener
{
    Font font1 = new Font("Century Gothic", Font.BOLD,20);
    Font font2 = new Font("Century Gothic",Font.PLAIN,14);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    JOptionPane disp = new JOptionPane();
    JFrame mainFrame = new JFrame();
    JFrame instFrame = new JFrame("How to Play");
    JFrame playFrame = new JFrame("Domino Game");
    JButton startBtn = new JButton("START");
    JButton instBtn = new JButton ("Instructions");
    JButton exitBtn = new JButton ("Exit");
    JButton retBtn = new JButton("Return to Menu");
    JLabel background = new JLabel(new ImageIcon("domino_01.jpg"));
    JLabel background2 = new JLabel(new ImageIcon("dominobg.jpg"));
    JTextArea instArea = new JTextArea(5,22);

    SpringLayout layout = new SpringLayout();
    public void startMenu()
    {
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setSize(screenSize);
        mainFrame.setBounds(0,0,screenSize.width,screenSize.height);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setLayout(new BorderLayout());
        startBtn.setBounds(55,80,155,70);
        instBtn.setBounds(55,160,155,70);
        exitBtn.setBounds(55,240,155,70);
        startBtn.setBackground(Color.WHITE);
        startBtn.setForeground(Color.RED);
        startBtn.setContentAreaFilled(false);
        startBtn.setOpaque(true);
        instBtn.setBackground(Color.WHITE);
        instBtn.setContentAreaFilled(false);
        instBtn.setOpaque(true);
        exitBtn.setBackground(Color.WHITE);
        exitBtn.setContentAreaFilled(false);
        exitBtn.setOpaque(true);
        startBtn.setFont(font1);
        instBtn.setFont(font1);
        exitBtn.setFont(font1);
        background.add(startBtn);
        background.add(instBtn);
        background.add(exitBtn);
        mainFrame.add(background);
        startBtn.addActionListener(this);
        instBtn.addActionListener(this);
        exitBtn.addActionListener(this);
        mainFrame.setVisible(true);
    }
    public void startGame()
    {
        mainFrame.dispose();
        playFrame.setLayout(new BorderLayout());
        playFrame.setSize(screenSize);
        playFrame.setBounds(0,0,screenSize.width,screenSize.height);
        playFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        background2.add(new MyCanvas());
        background2.add(retBtn);
        playFrame.add(background2);
        playFrame.setResizable(false);
        playFrame.setVisible(true);
    }//startGame
    public void instructions()
    {
        instArea.setEditable(false);
        instFrame.setLayout(new FlowLayout());
        instFrame.setSize(310,500);
        instFrame.setLocationRelativeTo(mainFrame);
        instFrame.setAlwaysOnTop(true);
        instArea.setLineWrap(true);
        instArea.setWrapStyleWord(true);
        instArea.setFont(font2);
        instArea.setText("How to play Domino Game:\n     The game is played by matching one number of the domino to the same number of another domino, thus connecting it. Played by players, it starts with shuffling the domino DECK stacks and putting the topmost domino in the PLAYFIELD. Each player has their TURN in which they will draw the topmost domino and add it to their HAND. TURN active player should match either number (x,y) of the active domino in the PLAYFIELD with one of his domino in HAND; otherwise if they dont have domino with numbers to match, he should draw from the DECK until he found a matching domino. Active dominoes in the PLAYFIELD are those pieces without pair yet. A TURN ends when the player finally puts a piece in the PLAYFIELD. The game ends when the DECK is depleted and the player with the least domino in HAND will be the winner");
        instFrame.add(instArea);
        instFrame.setResizable(false);
        instFrame.setVisible(true);
    }

    public static void main(String[] args)
    {
        DominoTRY dominotry = new DominoTRY();
        dominotry.startMenu();
    }//void main
    public void actionPerformed(ActionEvent e)
    {
        Object btnClicked = e.getSource();
        if(btnClicked == startBtn)
            startGame();
        else if(btnClicked == instBtn)
            instructions();
        else if(btnClicked == exitBtn)
            {
                int reply = disp.showConfirmDialog(mainFrame,"Exit game?","",JOptionPane.YES_NO_OPTION);
                if(reply==JOptionPane.YES_OPTION)
                    System.exit(0);
            }
    }//actionPerformed

}//class DominoTRY

I don't have time now to study your code - I gave up when I saw you trying to add things to a JLabel! But you can't just add overlapping things to an ordinary frame or panel and have any control over which gets painted on top of which.

But in general there are two ways to have a background image:
1. Create a trivial subclass of JPanel that draws its background from your image file, and add all you other components to that.

class PanelWithBackground extends JPanel {
        ...

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(myBackgroundImage, 0, 0, this);
    }
}
  1. Use a JLayeredPane with a JLabel for the background and use the layered panel's Z ordering to ensure the background label is behind everything else

I see, what you mean sir JamesCherrill, I'm wrong in putting my background image as JLabel, and wanting to overlap things on it. Thanks for the idea sir!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.