我正在做一項任務,並且我被困在一個需求上。我儘可能地做出了誠意的努力,並且別無選擇,只能虛心地請求您的專業知識。 我的任務要求我在Java中進行一個簡單的遊戲,在該遊戲中圖像將隨機出現並隨機重複出現。用戶點擊圖像,並且如果用戶點擊圖像,則點擊數量被輸出到屏幕。 我的主要問題是我如何讓隨意時間隨機出現/重現所述圖像?爲了使持續時間隨機我會以某種方式設置在定時器?我試過了,但沒有奏效。至於隨機位置,我甚至不知道如何開始編碼,有人可以指引我正確的方向嗎?我會在actionPerformed方法中做到這一點嗎?Java:如何使JPanel中的ImageIcon出現/隨機出現隨機持續時間
無論如何,這裏是我的代碼到目前爲止。它編譯,但運動和速度是平穩和不變的。我需要圖像隨機出現/重現,而不是以平滑的恆定速率「滑動」。
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.Random;
public class CreatureClass extends JPanel
{
private final int WIDTH = 400, HEIGHT = 300;
private final int DELAY=20, IMAGE_SIZE = 60;
Random r = new Random();
private ImageIcon image;
private Timer timer;
private int x, y, moveX, moveY;
private int catchCount=0;
//-----------------------------------------------------------------
// Sets up the panel, including the timer for the animation.
//-----------------------------------------------------------------
public CreatureClass()
{
timer = new Timer(DELAY, new CreatureListener());//how to make duration random here?
addMouseListener (new MouseClickedListener());
image = new ImageIcon ("UMadBro.gif");
x = 0; //starting coordinates of image
y = 40;
moveX = moveY = 3;//image is shifted 3 pixels every time image is updated. I
// tried setting these to a random number, but it makes the
// image "stuck" in one position.
setPreferredSize (new Dimension(WIDTH, HEIGHT));
setBackground (Color.yellow);
timer.start();
}
//-----------------------------------------------------------------
// Draws the image in the current location.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent (page);
image.paintIcon (this, page, x, y);
page.drawString("Number of clicks: " + catchCount, 10, 15);
}
//*****************************************************************
// Detects when mouse clicked=image postition
//*****************************************************************
public boolean pointInMe(int posX, int posY)
{
if(x == posX && y == posY)
{
catchCount++;
return true;
}
return false;
}
public int getCatchCount()
{
return catchCount;
}
private class MouseClickedListener extends MouseAdapter
{
public void mouseClicked (MouseEvent event)
{
pointInMe(event.getX(), event.getY());
}
}
//*****************************************************************
// Represents the action listener for the timer.
//*****************************************************************
private class CreatureListener implements ActionListener
{
//--------------------------------------------------------------
// Updates the position of the image and possibly the direction
// of movement whenever the timer fires an action event.
// (I don't know how to make the image appear and reappear
// randomly for random durations)
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
x += moveX;
y += moveY;
if (x <= 0 || x >= WIDTH-IMAGE_SIZE)
moveX = moveX * -1;
if (y <= 0 || y >= HEIGHT-IMAGE_SIZE)
moveY = moveY * -1;
repaint();
}
}
}