如何在我的绘画功能中循环播放某些内容?

How to loop something in my paint function?

所以我正在尝试设置这个东西,当鼠标悬停在上面时会改变图像。我已经设置了 mouseListener 来知道鼠标是否在我的图像上。我有一个变量,其中存储了图像位置,当鼠标悬停在图像上时它会发生变化。当我的绘画命令是 运行 时,它会绘制默认图像,当我将鼠标悬停在它上面时,它不会改变,因为它不会再次绘制。我怎样才能让它在图像位置更改时再次重新绘制。顺便说一句,mouseListener 与图像 class 不同。

我的形象:

private String settingsConfig = snake.settingsConfig;
settingsImage = new ImageIcon(getClass().getResource(settingsConfig));
settingsImage.paintIcon(this, g, 700, 23);

我的主要class(画法在其他class)

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";
            }
        }
    });
}

非常小的一块油漆(图形g)(在不同的class):

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);
}

How can I make it so that it repaints it again when the image location is changed

在需要更新画作的地方调用reapint();。例如,当您将鼠标悬停在图像上时在侦听器中。

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";
    }
    repaint();  
}