1

私はJavaに比較的慣れていないので、まっすぐな道でオブジェクト(車など)の動きをシミュレートしようとしています。オブジェクトを行の最後のポイントだけに表示するのではなく、出力内で段階的に移動させたいと考えています。

シミュレーション環境を作成するために、Veh.java (車両オブジェクトと SimuFrame.java) の 2 つのクラスを使用しました。

アイデアについては、いくつかのオンライン チュートリアルを参照しまし 。ランダムな方向ではなく)

どこが間違っているのか、次に何をすべきかを理解するのを手伝ってください..

どうもありがとう。

これが私のコードです:

import java.awt.*;
import java.awt.event.*;


public class Veh extends Rectangle{

    int uLeftX, uLeftY; //upper LH Position for Rectangle
    static int height = 20;
    static int width = 20;
    int[] pathCoords=new int[1000]; 

    int startPosY;  // start position of the objet - anywhere on the left bounday of the frame.
    int goalPosY; // end position of the objet - anywhere on the right boundary of the frame.

//Constructor to Create a new Veh
    public Veh(int startPosY,int goalPosY){ 

    //Create a new rectangle vehicle from super class constructor
    super(0, startPosY, height, width);  

    this.startPosY=startPosY;

    this.goalPosY=goalPosY;

    this.pathCoords = Pathmove();

    }


    //Calculating the 1000 points on the line joining (0,startPosY) and  (goalPosY,999)
        int[] Pathmove(){

//Slope calculation
    float s=(float)(this.goalPosY-this.startPosY)/999;

    pathCoords[0]=this.startPosY;
        System.out.println("First xy pair is: 0," +this.pathCoords[0]);
    for(int m=1; m<1000; m++){
    pathCoords[m]= (int)(m*s)-(int)((m-1)*s)+ pathCoords[m-1];
    }
        return pathCoords;
    }

    //Function to move the Reactangular object along the line using the Y coordinate   values from Pathmove()

    void move(){
    int[] a = (int[])this.pathCoords.clone();
    for (int c=0; c<a.length;c++){
    this.setLocation(c,a[c]);
    }
    } 



} 

これは、シミュレーション環境を作成するためのコードです。

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class SimuFrame extends JFrame{

public static int frameWidth=1000;
public static int frameHeight=1000;

public static void main(String[] args){

new SimuFrame();
}

public SimuFrame(){
this.setSize(frameWidth,frameHeight);
this.setTitle("Path Planning Results");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

SimuObject SO=new SimuObject();
this.add(SO);
// Used to execute code after a given delay
// The attribute is corePoolSize - the number of threads to keep in 
// the pool, even if they are idle
ScheduledThreadPoolExecutor executor= new ScheduledThreadPoolExecutor(5);

executor.scheduleAtFixedRate(new RepaintTheFrame(this), 0L, 20L,    TimeUnit.MILLISECONDS); 

this.setVisible(true);

}

}

// Class implements the runnable interface
// By creating this thread I want to continually redraw the screen
// while other code continues to execute

class RepaintTheFrame implements Runnable{

SimuFrame theFrame;

public RepaintTheFrame(SimuFrame theFrame){
}

@Override
public void run() {

    theFrame.repaint();
    }

}

class SimuObject extends JComponent{

//Holds every Veh created

public ArrayList<Veh> vehs=new ArrayList<Veh>();

public SimuObject(){

    int startPosY = (int)(Math.random()*999);
    int goalPosY = (int)(Math.random()*999);
    vehs.add(new Veh(startPosY,goalPosY));

}

public void paint(Graphics g){

    // Allows me to make many settings changes in regards to graphics
    Graphics2D graphicSettings = (Graphics2D)g;
    // Draw a background that is as big as the Simu board
    graphicSettings.setColor(Color.WHITE);
    graphicSettings.fillRect(0, 0, getWidth(), getHeight());
    // Set rendering rules
    graphicSettings.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    // Set the drawing color to red
    graphicSettings.setPaint( Color.RED);
    // Cycle through all of the Rock objects
    for(Veh veh : vehs){
        // Move the vehicle
        veh.move();
        graphicSettings.draw(veh);

    }
}

}

4

2 に答える 2