所以我试图设置这个东西,当盘旋时改变图像。我已经设置了mouseListener来知道鼠标是否在我的图像上。我有一个变量,其图像位置存储在它中,当图像悬停时它会发生变化。当我的绘画命令运行时,它绘制默认图像,当我将它悬停在它上面时,它不会更改,因为它不会再被绘制。我怎样才能让它在图像位置发生变化时再次重新绘制它。顺便说一句,mouseListener与图像不同。如何在绘制函数中循环某些东西?
我的图像:
private String settingsConfig = snake.settingsConfig;
settingsImage = new ImageIcon(getClass().getResource(settingsConfig));
settingsImage.paintIcon(this, g, 700, 23);
我的主类(画方法是在其它类)
public class snake implements MouseListener{
public static int mouseX;
public static int mouseY;
public static String settingsConfig = "/assets/settings.png";
public static void main(String[] args) {
// JFrame
JFrame obj = new JFrame("Snake");
gameplay Gameplay = new gameplay();
obj.setBounds(10, 10, 905, 700);
obj.setBackground(Color.DARK_GRAY);
obj.setResizable(false);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.add(Gameplay);
obj.setVisible(true);
obj.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
if(mouseX > 699 && mouseX < 761 && mouseY > 22 + 25 && mouseY < 54 + 25) {
settingsConfig = "/assets/settingshover.png";
}
else {
settingsConfig = "/assets/settings.png";
}
}
});
}
非常小的片涂料(图形克)(在不同的类):
public void paint (Graphics g) {
if(moves == 0) {
snakexlength[2] = 50;
snakexlength[1] = 75;
snakexlength[0] = 100;
snakeylength[2] = 100;
snakeylength[1] = 100;
snakeylength[0] = 100;
}
if(moves >= 1) {
playing = true;
}
// Draw title image border
g.setColor(Color.WHITE);
g.drawRect(24, 10, 851, 55);
// Draw the title image and settings
titleImage = new ImageIcon(getClass().getResource("/assets/snaketitle.jpg"));
titleImage.paintIcon(this, g, 25, 11);
settingsImage = new ImageIcon(getClass().getResource(settingsConfig));
settingsImage.paintIcon(this, g, 700, 23);
// Draw the border for gameplay
g.setColor(Color.WHITE);
g.drawRect(24, 74, 851, 577);
// Draw background for the gameplay
g.setColor(Color.BLACK);
g.fillRect(25, 75, 850, 575);
// Draw score
g.setColor(Color.WHITE);
g.setFont(new Font("arial", Font.PLAIN, 14));
g.drawString("Score: " + score, 780, 30);
// Draw high score
g.drawString("High Score: " + highScore, 780, 50);
}
你已经问过这个问题,并给出了重复以及许多类似问题的链接。你为什么再问一次?你为什么没有显示你从前面的链接中获得了一些东西?你为什么不按照以前的要求发布[mcve]? –
虽然这是不同的,但我在问我如何在绘画函数中循环某些东西 –
你不循环,而且你没有正确地思考逻辑。阅读重复内容。您可以更改鼠标侦听器中某个字段的状态,然后使用该更改决定要绘制的内容。但是,如前所述,再次用JLabels交换ImageIcons会好得多。 –