Making a transparent JPanel over a JFrame

Zeefum Dajma

I am new to graphics in java and I am currently working on a game. Essentially, there are rising bubbles, and the user has to pop them by moving the mouse over them.

I have already made an animation on the JFrame and I need to add a JPanel for a MouseMotionListener on top. However, when I add the JPanel on top of the JFrame (even with the setOpaque to false) it still does not let me see my animation underneath. You can see my code below. If you find coding errors, please let me know.

I have two solutions in mind, either animate in JPanel (which I don't know how to do), or make the JPanel transparent.

Game Class:

public class Game extends JPanel{

  public static final int WINDOW_WIDTH = 600;
  public static final int WINDOW_HEIGHT = 400;

  private boolean insideCircle = false;
  private static boolean ifPaused = false;
  private static JPanel mainPanel;
  private static JLabel statusbar;
  private static int clicks = 0;
  //Creates a Bubbles object Array
  Bubbles[] BubblesArray = new Bubbles[5];

  public Game() {

    //initializes bubble objects
     for (int i = 0; i < BubblesArray.length; i++)
       BubblesArray[i] = new Bubbles();
     }

public void paint(Graphics graphics) {

  //makes background white
   graphics.setColor(Color.WHITE);
   graphics.fillRect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);

   //paints square objects to the screen
   for (Bubbles aBubblesArray : BubblesArray) {
     aBubblesArray.paint(graphics);
   }
}

public void update() {

 //calls the Square class update method on the square objects
 for (Bubbles aBubblesArray : BubblesArray) aBubblesArray.update();
}

    public static void main(String[] args) throws InterruptedException {
     statusbar = new JLabel("Default");
     mainPanel = new JPanel();
     mainPanel.setOpaque(false);
     mainPanel.setBackground(new Color(0,0,0,0));
     mainPanel.setVisible(true);
     mainPanel.addMouseMotionListener(new MouseAdapter() {
       @Override
        public void mouseMoved(MouseEvent e) {
          statusbar.setText(String.format("Your mouse is at %d, %d", e.getX(), e.getY()));
          }

       public void mouseExited(MouseEvent e){
         ifPaused = true;
        }   

        public void mouseEntered(MouseEvent e){
        ifPaused = false;
         }

     });

     Game game = new Game();
    JFrame frame = new JFrame();

    frame.add(game);
     frame.add(mainPanel);



   frame.add(statusbar, BorderLayout.SOUTH);
   frame.setVisible(true);
   frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
   frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   frame.setTitle("Bubble Burst");
   frame.setResizable(false);
   frame.setLocationRelativeTo(null);

   while (true) {
      if (!ifPaused){
       game.update();
       game.repaint();
       Thread.sleep(5);
      }
   }
}    
}
Jonah Haney

Your game panel is not showing up because you are adding two components to the same location of a BorderLayout (at BorderLayout.CENTER). Therefore mainPanel is not being added "on top of" game, it is replacing it. That being said:

It doesn't seem like your mainPanel is actually doing anything except listening for mouse motion. Could you not just add the MouseAdapter to your game object since it extends JPanel like this:

game.addMouseMotionListener(new MouseAdapter() {
    @Override
    public void mouseMoved(MouseEvent e) {
        statusbar.setText(String.format("Your mouse is at %d, %d", e.getX(), e.getY()));
    }

    public void mouseExited(MouseEvent e){
        ifPaused = true;
    }   

    public void mouseEntered(MouseEvent e){
        ifPaused = false;
    }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related