這是我做到了。它也播放聲音。你只是使用標誌。請注意,我使用mouseMoved而不是mouseEntered。
類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