私は、すべての研究にもかかわらず、材料を理解できないようなので、クラスの宿題に取り組んでいます。私は初心者であり、Javaの方法ではあまり知らない。また、これは私の最初の投稿ですので、あなたがこれを読んでいるときには許してください。Javaで異なる速度で移動する複数のオブジェクトをペイントするにはどうすればよいですか?
最近の過去の宿題を更新した教科書からソースコードを作成していますが、今では複数の四角形を描画し、それらのオブジェクトを独立して異なる速度で移動するクラスを生成しようとしています。彼らはすべて壁からも跳ね返る必要があります。私は指示に従って、1と10の間のランダムなxとyの値を保持する2つの配列を作成しました。しかし、私は配列に苦労し、正しく行っていないと確信しています。だから、私はそれが正しく設定されているかどうかを確認するためにいくつかのフィードバックを愛するだろう。
私は引き上げJPanelの描画を持ち、そして限り、正方形の1があるとして、それが壁に跳ね返る正常に動作しているが、私は複数を描く際に物事が変化します。独立して動かず、同じ速度を共有します。いくつかは時々消える。これは本当に私を捨てた。私は助けていただきありがとうございます!要するに
、私はすべてが異なる方向に異なる速度で移動する新しい四角形を描画しようとしています。私たちが想定している指示に従って、xとyの値を扱う2つの配列を作成して使用します。ここで
は、私がこれまで持っているものです。
public class DotsPanel extends JPanel
{
private int delay = 15;
private final int SIZE = 7, IMAGE_SIZE = 3; // radius of each dot
private Timer timer;
private int x, y, i;
private ArrayList<Point> pointList;
static int [] xarray = new int [1000];
static int [] yarray = new int [1000];
Random rand = new Random();
//-----------------------------------------------------------------
// Constructor: Sets up this panel to listen for mouse events.
//-----------------------------------------------------------------
public DotsPanel()
{
pointList = new ArrayList<Point>();
int [] xarray = new int [1000];
int [] yarray = new int [1000];
timer = new Timer(delay, new ReboundListener());
addMouseListener (new DotsListener());
addMouseMotionListener (new DotsListener());
setBackground(Color.gray);
setPreferredSize(new Dimension(700, 500));
for(int i = 0; i < xarray.length; i++)
{
xarray[i] = rand.nextInt(7);
yarray[i] = rand.nextInt(7);
}
timer.start();
}
//-----------------------------------------------------------------
// Draws all of the dots stored in the list.
//-----------------------------------------------------------------
public void paintComponent(Graphics page)
{
super.paintComponent(page);
page.setColor(Color.BLUE);
for (Point spot : pointList)
{
page.fillRect(spot.x-SIZE, spot.y-SIZE, 25, 25);
page.drawString("Count: " + pointList.size(), 5, 15);
}
}
//*****************************************************************
// Represents the listener for mouse events.
//*****************************************************************
private class DotsListener implements MouseListener, MouseMotionListener
{
//--------------------------------------------------------------
// Adds the current point to the list of points and redraws
// the panel whenever the mouse button is pressed.
//--------------------------------------------------------------
public void mousePressed(MouseEvent event)
{
pointList.add(event.getPoint());
repaint();
}
public void mouseDragged(MouseEvent event)
{
// initially I had two xarray and yarray in here just like in
// mouseClicked
// but it did not change anything when removed
}
//--------------------------------------------------------------
// Provide empty definitions for unused event methods.
//--------------------------------------------------------------
public void mouseClicked(MouseEvent event)
{
xarray[i] = rand.nextInt(7);
yarray[i] = rand.nextInt(7);
}
public void mouseReleased(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
public void mouseMoved(MouseEvent e) {}
}
private class ReboundListener implements ActionListener
{
//--------------------------------------------------------------
// Updates the position of the image and possibly the direction
// of movement whenever the timer fires an action event.
//--------------------------------------------------------------
public void actionPerformed(ActionEvent event)
{
for (Point spot : pointList)
{
spot.x += xarray[i];
spot.y += yarray[i];
if (spot.x <= 0 || spot.x >= 700)
xarray[i] = xarray[i] * -1;
if (spot.y <= 0 || spot.y >= 500)
yarray[i] = yarray[i] * -1;
repaint();
}
}
}
}
ありがとうございました!あなたのコードは、私が指示でやるべきことより確かに論理的です。しかし、私がガイドラインを守らないと、私はその資料をさらに遅らせることができるのではないかと心配しています。同様に、この宿題のために同じ資料を含むかもしれない将来のプロジェクトの準備をしてはいけません。 xとyの動きの速度に対応するために、さらに2つの配列を作成する必要がありますか?または、描かれている各四角形を処理する配列を生成できますか?助けてくれてありがとう! – Jfwhyte8
@ Jfwhyte8、私はあなたの指針や例が教えようとしていることを理解していないので、私はあなたの質問に答えることができません。 – camickr
お手数をおかけしていただきありがとうございます。申し訳ありませんが、今返信しています。人生は控えめに言って忙しかったです。 私の目的は、タイマーを使ってアニメーションを追加して、描画時に四角形がすべて動くようにすることです。私は各方形をランダムな速度で(x方向とy方向に)移動させる必要があり、四角形を画面の4辺すべてから適切に「バウンス」させる必要があります。さらに、私の先生は、Xの動きとYの動きに対して1〜10のランダムな値を保持するには、サイズ1000の配列を2つ使用する必要があると言っていました。これにより、長方形が独立した移動速度を持つことが可能になるはずです。 – Jfwhyte8