gpt4 book ai didi

java - 如何使用鼠标事件切换图像?

转载 作者:行者123 更新时间:2023-12-02 06:27:31 26 4
gpt4 key购买 nike

我正在做一项任务,其中图像会四处移动,当用户单击图像时,图像会发生变化,一旦通过计时器类再次移动,图像就会变回原始图像。截至目前,我可以单击图像来更改它,但当再次移动时它不会变回来。移动后有办法变回来吗?

这是我的代码

主要:

import java.awt.*;
import javax.swing.*;

public class Catch_The_Creature
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Catch the Creature");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JOptionPane.showMessageDialog(frame, "Catch Pikachu!");
frame.getContentPane().add(new Creature());
frame.pack();
frame.setVisible(true);
}
}

面板:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Creature extends JPanel
{
private static final int DELAY=900;
private Random generator = new Random();
private ImageIcon image, image1;
private Timer timer;
private int x, y;
private int catchCount=0;

public Creature()
{
image = new ImageIcon ("pikachu.png");
image1 = new ImageIcon ("pokeball.png");
timer = new Timer(DELAY, new MoveListener());
x = generator.nextInt( 1900 );
y = generator.nextInt(1000);
addMouseListener (new MouseClickedListener());
setBackground (Color.green);
setPreferredSize(new Dimension(1900,1000));
timer.start();
}
//Draws the image.
public void paintComponent(Graphics page)
{
super.paintComponent(page);
image.paintIcon (this, page, x, y);
page.drawString("Pikachus Captured: " + catchCount, 10, 35);
setFont(new Font("Arial", Font.BOLD,35));

}
//Method for moving the image.
public void move()
{
timer.start();
x = generator.nextInt( 1900 );
y = generator.nextInt(1000);
if (timer.isRunning())
{
x = generator.nextInt( 1900 );
y = generator.nextInt(1000);
}
repaint();
}
//Method for getting the number of times caught.
public int getCatchCount()
{

return catchCount;
}
//Makes the image move
private class MoveListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
move();
repaint();

}
}
//Records when the user clicks the image.
private class MouseClickedListener extends MouseAdapter
{
public void mouseClicked(MouseEvent event)
{

if((event.getButton() == MouseEvent.BUTTON1) && between(event.getX(), x, x + image.getIconWidth()) && between(event.getY(), y, y + image.getIconHeight()))
{
System.out.println("CAUGHT ONE!");
catchCount++;
move();
image=image1;

}

}
}
private static boolean between(int x, int lower, int upper)
{
return (x >= lower) && (x <= upper);

}
}

最佳答案

我希望我明白你想要实现什么目标。首先你需要 3 张图片:

private ImageIcon imageToDraw, image1, image2;

生物现在看起来像这样:

public Creature()  
{
image1 = new ImageIcon ("pikachu.png");
image2 = new ImageIcon ("pokeball.png");
imageToDraw = image1;
...
}

在 move() 中,您应该将图像设置为 image1:

public void move()
{
imageToDraw = image1;
timer.start();
x = generator.nextInt( 1900 );
...
}

不要忘记在paint()中将图像更改为imageToDraw:

public void paintComponent(Graphics page)
{
super.paintComponent(page);
imageToDraw.paintIcon (this, page, x, y);
...
}

删除 move();从 onclick-event 并在 click-action 中将图像更改为 imageToDrwa:

public void mouseClicked(MouseEvent event)
{

if((event.getButton() == MouseEvent.BUTTON1) && between(event.getX(), x, x + image.getIconWidth()) && between(event.getY(), y, y + image.getIconHeight()))
{
System.out.println("CAUGHT ONE!");
catchCount++;
//move(); should be removed
imageToDraw=image1;

}

}

编辑:移动();从 onclick-event 中删除

关于java - 如何使用鼠标事件切换图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20388988/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com