私は課題に取り組んでいますが、1 つの要件で行き詰っています。私はできる限り誠意を持って努力してきましたが、謙虚にあなたの専門知識を求めるしかありません。私の課題では、Java で単純なゲームを作成する必要があります。このゲームでは、画像がランダムに表示され、ランダムに再表示されます。ユーザーは画像をクリックすることになっており、ユーザーが画像をクリックすると、クリック数が画面に出力されます。私の主な質問は、ランダムな期間、ランダムな位置に画像を表示/再表示させるにはどうすればよいですか? 期間をランダムにするには、タイマーで何らかの方法で設定しますか? 私はそれを試しましたが、うまくいきませんでした。ランダムな位置については、コーディングを開始する方法さえわかりません。誰かが私を正しい方向に向けることができますか? actionPerformed メソッドでそれを行いますか?
とにかく、これまでのところ私のコードです。コンパイルされますが、動きと速度はスムーズで一定です。滑らかな一定の速度で「滑空」するのではなく、画像をランダムに表示/再表示する必要があります。
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();
}
}
}