これは私がそれをやった方法です持っているものです。それはまた、音も再生されます。あなたは単にフラグを使用します。注意、mouseEnteredではなくmouseMovedを使用しました。
クラスMouseInput
@Override
public void mouseMoved(MouseEvent e) {
int x=e.getX();
int y=e.getY();
if (x>100&&x<200&&y>150&&y<200) {
if (mouseInStart==false) { <---- if this line is true, means mouse entered for first time.
Sound.playSound(soundEnum.BUTTONHOVER);
}
mouseInStart=true;
} else {
mouseInStart=false;
}
}
public boolean mouseInStart() { <--use this in your update method
return mouseInStart;
}
そして、私の他のクラス(クラスメニュー)で
public void render(Graphics2D g) {
....
....
gradient = new GradientPaint(100, 150, setStartColor(), 200, 200, Color.gray);
g.setPaint(gradient);
g.fill(startButton);
}
public Color setStartColor() {
if (mouseInStart) {
return Color.red;
} else {
return Color.white;
}
}
public void update() { <--- and this is to keep checking if your mouse is in start. This is part of the giant game loop.
mouseInStart=mouseInput.mouseInStart();
mouseInLoad=mouseInput.mouseInLoad();
mouseInQuit=mouseInput.mouseInQuit();
}
あなたはobject.setBackground = menuImage2を行うカント? –
多分もっと多くのobject.setBackground(menuImage2);それはmenuImage2の定義と型に依存する – azro