2014-08-28 54 views
0

我有4個圖塊:藍色,紅色,綠色和黃色。當鼠標移過瓦片時,我希望它點亮(只需將圖像更改爲淺色)。相反沒有任何反應JApplet - 當布爾值爲真時不會繪製圖像

我已經做了一些簡單的調試,並知道圖像正確導入和mouseOverTile [0]轉爲真的事實。

import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.util.Arrays; 

import javax.swing.ImageIcon; 
import javax.swing.JApplet; 


public class MainGame extends JApplet implements MouseListener, Runnable 
{ 
    private static final long serialVersionUID = 1L; 
    Thread animator = null; 
    Image blue, blueLit, red, redLit, green, greenLit, yellow, yellowLit; 
    boolean takingTurn; 
    boolean[] mouseOverTile = new boolean[4]; 

    public void init() 
    { 
     setSize(500, 500); 

     blue = new ImageIcon("Images/Blue.png").getImage(); 
     blueLit = new ImageIcon("Images/Blue_Lit.png").getImage(); 
     red = new ImageIcon("Images/Red.png").getImage(); 
     redLit = new ImageIcon("Images/Red_Lit.png").getImage(); 
     green = new ImageIcon("Images/Green.png").getImage(); 
     greenLit = new ImageIcon("Images/Green_Lit.png").getImage(); 
     yellow = new ImageIcon("Images/Yellow.png").getImage(); 
     yellowLit = new ImageIcon("Images/Yellow_Lit.png").getImage(); 
     Arrays.fill(mouseOverTile, false); 
     takingTurn = true; 
     addMouseListener(this); 
    } 

    public void start() 
    { 
     if(animator == null) 
     { 
      animator = new Thread(this); 
     } 
    } 

    public void stop() 
    { 
     animator = null; 
    } 

    @Override 
    public void run() 
    {  
     while(Thread.currentThread() == animator) 
     { 
      try 
      { 
       Thread.sleep(1); 
       repaint(); 
      } 
      catch(Exception e) 
      { } 
     } 
    } 

    public void paint(Graphics g) 
    { 
     if(!mouseOverTile[0]) 
     { 
      g.drawImage(blue, 50, 50, this); 
     } else g.drawImage(blueLit, 50, 50, this); 
     if(!mouseOverTile[1]) 
     { 
      g.drawImage(red, 260, 50, this); 
     } else g.drawImage(redLit, 260, 50, this); 
     if(!mouseOverTile[2]) 
     { 
      g.drawImage(green, 50, 260, this); 
     } else g.drawImage(greenLit, 50, 260, this); 
     if(!mouseOverTile[3]) 
     { 
      g.drawImage(yellow, 260, 260, this); 
     } else g.drawImage(yellowLit, 260, 260, this); 
    } 


    @Override 
    public void mouseClicked(MouseEvent e) 
    { 
     System.out.println("Mouse X: " + e.getX()); 
     System.out.println("Mouse Y: " + e.getY()); 
    } 

    @Override 
    public void mouseEntered(MouseEvent e) 
    { 
     if(takingTurn) 
     { 
      if(e.getX() > 50 && e.getX() < 250) 
      { 
       mouseOverTile[0] = true; 
       System.out.println(mouseOverTile[0]); 
      } 
     } 
    } 

    @Override 
    public void mouseExited(MouseEvent e) 
    { 
     if(takingTurn) 
     { 
      if(e.getX() < 50 || e.getX() > 250) 
      { 
       mouseOverTile[0] = false; 
       System.out.println(mouseOverTile[0]); 
      } 
     } 
    } 

    @Override 
    public void mousePressed(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 


    @Override 
    public void mouseReleased(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

} 
+1

不要連續輪詢喜歡你這樣做。只需使用MouseListener更改狀態並調用重繪。 – 2014-08-28 23:00:08

+0

實際上,它不是連續輪詢,因爲OP永遠不會啓動線程。 – 2014-08-28 23:13:29

+0

你的線程退出條件被破壞了,因爲'animator'不是'volatile'並且沒有同步,所以當'animator'變成'null'時線程可能永遠不會看到,但是沒關係,因爲你不需要或者不想要像這樣的線程擺在首位。 – 2014-08-28 23:14:39

回答

4

您尚未更新視圖。
當您進行一些更改時,請致電repaint()
例如,在你的代碼中mouseEntered(...)

@Override 
public void mouseEntered(MouseEvent e) 
{ 
    if(takingTurn) 
    { 
     if(e.getX() > 50 && e.getX() < 250) 
     { 
      mouseOverTile[0] = true; 
      System.out.println(mouseOverTile[0]); 
      repaint(); 
     } 
    } 
}