2016-05-18 57 views
1

我是java的新手,我目前正在开发一款游戏。本质上,气泡上升,用户必须通过将鼠标移到它们上面来弹出它们。在JFrame上制作一个透明的JPanel

我已经在JFrame上做了一个动画,我需要在上面添加一个MouseMotionListener的JPanel。但是,当我在JFrame上添加JPanel时(即使setOpaque为false),它仍然不会让我看到我的动画在下面。你可以在下面看到我的代码。如果您发现编码错误,请告诉我。

我有两种解决方案,要么在JPanel中生成动画(我不知道该怎么做),要么让JPanel透明。

游戏类:

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

不想'paintComponent'到'paint',还要确保你调用的方法超级实现(即'super.paint(图形)')接下来的问题是,为什么不把监听器添加到'Game' Panel'? – MadProgrammer

回答

0

game面板没有显示出来,因为你要添加两种成分的BorderLayout的相同位置(在BorderLayout.CENTER)。因此mainPanel没有被添加到“game之上”,它正在替换它。这就是说:

它似乎不像你的mainPanel实际上是做任何事情,除了听老鼠的动作。你能不能只是添加MouseAdaptergame对象,因为它延伸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; 
    } 
}); 
+0

它的工作非常感谢你! –