2013-09-24 26 views
0

我正在創建一個6x6的網格內存遊戲。它的要求是在面板中使用圖像作爲按鈕的替代品。Sprites上的MouseListener

enter image description here 210×70

該項目需要實現以下條件MouseListeners: 該按鈕將顯示第一pokebell。 當鼠標懸停在pokeball上時,它變成第二個pokeball。 當鼠標離開pokeball時,它會回到第一個pokeball。 當鼠標點擊pokeball時,它變成第三個pokeball。

電網MouseEvents

public void mouseEntered(MouseEvent e) { 
    for(i = 0; i < 36; i++){   
     if(e.getSource() == pkm[i]){     
       pkb[i].repaint();      
     } 
    } 
} 

public void mouseExited(MouseEvent e) { 
    for(i = 0; i < 36; i++){ 
     if(e.getSource() == pkm[i]){ 
       pkb[i].repaint();   
     } 
    } 
} 

PokeBall類

int start = 0; 
int ht = 0, wt = 0; 
URL url; 
BufferedImage img, sp1; 

public PokeBall(String imgLink, int w, int h, int x){ 
wt = w; 
ht = h; 
start = x; 
url = this.getClass().getResource(imgLink);  
    try{ 
     img = ImageIO.read(url); 
    } 
    catch(Exception e){   
    } 
} 


public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    Graphics2D g2d = (Graphics2D)g; 

    sp1 = img.getSubimage(start, 0, wt, ht); 
    g2d.drawImage(sp1,20,10,null); 

    if(start == 70) { 
     start = 0; 
    } 
    else { 
     start += 70; 
    }   
} 

我的想法是,精靈的變化會在相應的鼠標事件中調用,而是我得到了整個pokeball在動畫無限循環,即使鼠標沒有調用任何事件。

我需要一些想法或建議,以便如何阻止它自己循環並實際調用指定的pokeball。

+0

*「的項目** **需要實現以下條件** ** MouseListeners:」 *哇!由於JToggleButton只需3行代碼就可以提供這種功能,因此需求非常嚴格。有關示例,請參見此[演示](http://stackoverflow.com/a/7360696/418556)。 –

+0

是的。可悲的是,我們的教授要求使用面板而不是JButton或JToggleButton。 – Rapharlo

+0

順便說一句 - 以下兩件事情如何進入它? [tag:animation]&[tag:gridlayout] –

回答

0

基本上,繪畫球的責任是PokeBall類,它需要知道什麼時候狀態發生了變化,因此將MouseListenerMouseMotionListener應用於它是有意義的。那麼你不需要關心嘗試更新網格位置和其他有趣的東西。

在您GridPane,我會再附上點擊球時,另一個MouseListener這樣你就可以檢測並採取有適當的行動......

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.imageio.ImageIO; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class MyPokeBalls { 

    public static void main(String[] args) { 
     new MyPokeBalls(); 
    } 

    public MyPokeBalls() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new PokeBall()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class PokeBall extends JPanel { 

     private BufferedImage balls; 
     private int ballWidth = 70; 
     private int ballHeight = 70; 
     private int ballOffset = 0; 

     public PokeBall() { 
      try { 
       balls = ImageIO.read(new File("PokeBalls.png")); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 

      MouseAdapter ma = new MouseAdapter() { 

       private boolean isIn = false; 

       @Override 
       public void mouseEntered(MouseEvent e) { 
        ballOffset = 1; 
        isIn = true; 
        repaint(); 
       } 

       @Override 
       public void mouseExited(MouseEvent e) { 
        ballOffset = 0; 
        isIn = true; 
        repaint(); 
       } 

       @Override 
       public void mousePressed(MouseEvent e) { 
        ballOffset = 2; 
        repaint(); 
       } 

       @Override 
       public void mouseReleased(MouseEvent e) { 
        if (isIn) { 
         ballOffset = 1; 
        } else { 
         ballOffset = 2; 
        } 
        repaint(); 
       } 

      }; 

      addMouseListener(ma); 
      addMouseMotionListener(ma); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(ballWidth, ballHeight); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (balls != null) { 
       Graphics2D g2d = (Graphics2D) g.create(); 
       BufferedImage ball = balls.getSubimage(ballWidth * ballOffset, 0, ballWidth, ballHeight); 
       int x = (getWidth() - ball.getWidth())/2; 
       int y = (getHeight() - ball.getHeight())/2; 
       g2d.drawImage(ball, x, y, this); 
       g2d.dispose(); 
      } 
     } 
    } 
} 
0
AbstractButton b=new JToggleButton(firstIcon); 
b.setContentAreaFilled(false); 
b.setFocusable(false); 
b.setBorder(BorderFactory.createEmptyBorder()); 
b.setRolloverEnabled(true); 
b.setRolloverIcon(secondIcon); 
b.setSelectedIcon(thirdIcon); 

爲什麼重新發明輪子?

創建這樣的按鈕的網格是很簡單...

+0

好的,這個問題的評論來自我寫這個... – Holger