简体   繁体   English

在JFrame上创建透明的JPanel

[英]Making a transparent JPanel over a JFrame

I am new to graphics in java and I am currently working on a game. 我是java的图形新手,我目前正在开发一款游戏。 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. 我已经在JFrame上制作了一个动画,我需要在顶部为MouseMotionListener添加一个JPanel。 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. 但是,当我在JFrame顶部添加JPanel时(即使将setOpaque设置为false),它仍然不允许我在下面看到我的动画。 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. 我有两个解决方案,要么在JPanel中动画(我不知道该怎么做),要么让JPanel透明化。

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);
      }
   }
}    
}

Your game panel is not showing up because you are adding two components to the same location of a BorderLayout (at BorderLayout.CENTER ). 您的game面板没有显示,因为您要将两个组件添加到BorderLayout的相同位置(在BorderLayout.CENTER )。 Therefore mainPanel is not being added "on top of" game , it is replacing it. 因此, mainPanel没有被添加到“ game之上”,它正在取代它。 That being said: 话虽如此:

It doesn't seem like your mainPanel is actually doing anything except listening for mouse motion. 看起来你的mainPanel实际上并没有做任何事情,除了听老鼠的动作。 Could you not just add the MouseAdapter to your game object since it extends JPanel like this: 你能不能只将MouseAdapter添加到game对象中,因为它扩展了JPanel如下所示:

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;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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