I am using a BufferedImage
to display two images on my JPanel
. However, I want the images move in random directions when a button is clicked and should only stop when another button is pressed.
How do I make my images move randomly on the panel?
This is part of my code:
private void getImages() {
try {
frogImage = ImageIO.read(new File ("C:\\OOP\\CyberPet\\src\\img\\frog.gif"));
flyImage = ImageIO.read(new File ("C:\\OOP\\CyberPet\\src\\img\\fly.gif"));
g = panel.getGraphics();
g.drawImage(frogImage, 500, 25, 40, 40, null); //set position and size of the image
g.drawImage(flyImage, 40, 40, 10, 10, null); //set position and size of the image
} catch (IOException e) {
}
}
public void actionPerformed(ActionEvent event) {
getImages();
if (event.getSource() == makeButton){
responseArea.setText(enterField.getText());
}
else if(event.getSource() == hungryButton){
}
else if(event.getSource() == resetButton){
}
}
public void draw()
{
}
}