我正在創建一個6x6的網格內存遊戲。它的要求是在面板中使用圖像作爲按鈕的替代品。Sprites上的MouseListener
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。
*「的項目** **需要實現以下條件** ** MouseListeners:」 *哇!由於JToggleButton只需3行代碼就可以提供這種功能,因此需求非常嚴格。有關示例,請參見此[演示](http://stackoverflow.com/a/7360696/418556)。 –
是的。可悲的是,我們的教授要求使用面板而不是JButton或JToggleButton。 – Rapharlo
順便說一句 - 以下兩件事情如何進入它? [tag:animation]&[tag:gridlayout] –