2016-05-18 50 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

它的工作非常感謝你! –