Java 在一系列设定方向上移动对象
我是新的编码和挣扎与一个项目。我不太懂Java 我的目标如下:监控和优化公司运营商(混凝土厂和卡车供应商)为施工现场提供的混凝土交付服务 我应该有一个混凝土厂,两辆卡车和几个网站,这是由于用户的点击创建。操作员可以随时看到其公司的卡车,这些卡车将混凝土运送到位于给定地理区域的工地。网站的数量也在不断变化(=用户点击屏幕) 变量: 混凝土厂->坐标(X坐标、Y坐标)+尺寸 站点(arraylist)->坐标(卡车类中的Site.x、Site.y或destination.x、destination.y)+大小 卡车(x2)->坐标(位置x,位置y);大小;起源;目的地 卡车的动作: 移动到站点(1) 到达现场后等待约3秒钟 转到列表的下一个站点(2)(+等待3秒) 到达2个地点后返回混凝土厂(等待3秒) 前往下一个现场(3),然后(4),返回混凝土厂等 (基本上,它有两个站点并返回,两个站点并返回等…它不能访问已经交付的站点) 这将是很好的,也有标志的网站改变,一旦它已经达到了一辆卡车 卡车的选择:我有两辆卡车,要选择哪辆卡车需要: 如果是免费的(尚未在前往现场的途中) 如果已满(已完成0或1个站点,或未完成2个站点) 最近的一个(测量距离) 与HTML的交互: 我在想,如果可能的话,可以测量每辆卡车行驶的距离,并将其显示在HTML屏幕上(如果可能的话,用按键保存数据) 因此,我开始考虑是否可以添加一辆卡车(不知道这是否是开始的正确部分,也许操作会更好)。我试着告诉它,如果站点号小于2,它将通过站点列表,但如果不是,新的目标是混凝土工厂。但它不起作用 到目前为止,这就是我的脚本(我已经用椭圆和矩形替换了混凝土、场地和卡车的图像,以便您可以运行它):Java 在一系列设定方向上移动对象,java,processing,Java,Processing,我是新的编码和挣扎与一个项目。我不太懂Java 我的目标如下:监控和优化公司运营商(混凝土厂和卡车供应商)为施工现场提供的混凝土交付服务 我应该有一个混凝土厂,两辆卡车和几个网站,这是由于用户的点击创建。操作员可以随时看到其公司的卡车,这些卡车将混凝土运送到位于给定地理区域的工地。网站的数量也在不断变化(=用户点击屏幕) 变量: 混凝土厂->坐标(X坐标、Y坐标)+尺寸 站点(arraylist)->坐标(卡车类中的Site.x、Site.y或destination.x、destination.
/*preload=“factory_12.png”*/
/*preload=“sign.png”*/
/*preload=“simple\u truck.png”*/
货车;
//PImage混凝土厂;
//普丰阿丰;
int xCoord;
国际协调;
阵列列表站点;
int siteSize=30;
void setup()//开始时调用一次的内容
{
大小(500500);
//concretePlant=loadImage(“factory_12.png”);
//aFont=createFont(“工业革命常规”,12);
//文本字体(aFont);
xCoord=int(宽度/2);
yCoord=int(高度/2);
//创建存储站点对象的空数组列表
sites=新的ArrayList();
//添加第一个站点
添加(新站点(随机(宽度)、随机(高度)、站点大小);
//储存卡车
货车=新货车(xCoord,yCoord);
}
void draw()//绘制背景和混凝土植物
{
背景(235247255);//浅蓝色背景,不在绘图中,因为它不允许站点图像保留
//图像(混凝土厂、xCoord、yCoord、60、60);
椭圆(xCoord,yCoord,60,60);
//填充(1);
//文本(“混凝土厂”,xCoord-20,yCoord+70);
//调用站点
对于(int i=sites.size()-1;i>=0;i--){
Site=sites.get(i);
site.displaySites();
}
//调用卡车函数
lorry.updateLorry();
}
void mousePressed(){
添加(新站点(mouseX、mouseY、siteSize));
}
课堂现场
{
浮动x,y;
浮子大小;
//皮梅格图片;
场地(浮心、浮阴、浮罪)
{
x=xin;
y=阴;
大小=罪恶;
//picture=loadImage(“sign.png”);
}
void displaySites()
{
//图像(图片,x,y,60,60);
rect(x,y,60,60);
}
}
班车
{
PVector位置1;
PVector位置2;
PVector混凝土厂;
//PVector速度;
//PImage混合器;
布尔变换方向;
int siteNumber=0;
站点目的地;
货车(浮式xCoord、浮式yCoord)
{
concretePlant=新PVector(xCoord,yCoord);//初始起点
location1=新的PVector(xCoord,yCoord);//初始起点
location2=新的PVector(xCoord,yCoord);//初始起点
//速度=新的PVector(2,2);
//mixer=loadImage(“simple_truck.png”);
目的地=sites.get(siteNumber);
改变方向=错误;
}
货车
{
//图像(混合器,位置1.x,位置1.y,30,30);
//图像(混合器,位置2.x,位置2.y,30,30);
椭圆(位置1.x,位置1.y,30,30);
椭圆(位置2.x,位置2.y,30,30);
}
无效移动()
{
float xdir1=destination.x-location1.x;
float ydir1=destination.y-location1.y;
PVector dir1=新的PVector(xdir1,ydir1);
dir1.normalize();
位置1.添加(目录1);
打印(“1going”);
float xdir2=destination.x-location2.x;
float ydir2=destination.y-location2.y;
PVector dir2=新的PVector(xdir2,ydir2);
dir2.normalize();
位置2.添加(目录2);
打印(“2Goong”);
}
void checkProgress()
{
对于(int siteNumber=0;siteNumber<2;siteNumber=siteNumber++);
if(距离(目的地x、目的地y、位置1.x、位置1.y)<1){
如果(siteNumber看一下,请阅读-总结是,这不是一种理想的解决志愿者问题的方法,并且可能会对获得答案产生反作用。请不要将此添加到您的问题中。这个问题有点难回答,因为这里有很多子问题,我不确定您会坚持哪一个。可以吗你把描述文字简化成一个小问题?你遇到的问题是位置访问算法吗?(如果这是一个学术作业,而你被
/*preload = "factory_12.png";*/
/*preload = "sign.png";*/
/*preload = "simple_truck.png";*/
Lorry lorry;
//PImage concretePlant;
//PFont aFont;
int xCoord;
int yCoord;
ArrayList<Site> sites;
int siteSize = 30;
void setup() // What is called once at the beginning
{
size (500, 500);
//concretePlant = loadImage("factory_12.png");
//aFont = createFont("IndustrialRevolution-Regular", 12);
//textFont(aFont);
xCoord = int(width/2);
yCoord = int(height/2);
//Creating empty Array List where store sites objects
sites = new ArrayList<Site>();
//Adding first site
sites.add(new Site(random(width), random(height), siteSize));
//storing lorries
lorry = new Lorry(xCoord, yCoord);
}
void draw() // Draw the background and concrete plant
{
background (235, 247, 255); //light blue background, not in draw as it wouldn't allow the site image to stay
//image(concretePlant, xCoord, yCoord, 60, 60);
ellipse(xCoord, yCoord, 60, 60);
//fill(1);
//text("Concrete Plant", xCoord-20, yCoord+70);
//Calling the sites
for (int i = sites.size () - 1; i>=0; i--) {
Site site = sites.get(i);
site.displaySites();
}
//calling the lorry functions
lorry.updateLorry();
}
void mousePressed() {
sites.add(new Site(mouseX, mouseY, siteSize));
}
class Site
{
float x,y;
float size;
//PImage picture;
Site (float xin, float yin, float sin)
{
x = xin;
y = yin;
size = sin;
//picture = loadImage("sign.png");
}
void displaySites()
{
//image(picture, x, y, 60, 60);
rect(x, y, 60, 60);
}
}
class Lorry
{
PVector location1;
PVector location2;
PVector concretePlant;
//PVector velocity;
//PImage mixer;
boolean changeDirection;
int siteNumber = 0;
Site destination;
Lorry(float xCoord, float yCoord)
{
concretePlant = new PVector(xCoord, yCoord); //Initial start point
location1 = new PVector(xCoord, yCoord); //Initial start point
location2 = new PVector(xCoord, yCoord); //Initial start point
//velocity = new PVector(2, 2);
//mixer = loadImage("simple_truck.png");
destination = sites.get(siteNumber);
changeDirection = false;
}
void displayLorry()
{
//image(mixer, location1.x, location1.y, 30, 30);
//image(mixer, location2.x, location2.y, 30, 30);
ellipse(location1.x, location1.y, 30, 30);
ellipse(location2.x, location2.y, 30, 30);
}
void Move()
{
float xdir1 = destination.x - location1.x;
float ydir1 = destination.y - location1.y;
PVector dir1 = new PVector (xdir1, ydir1);
dir1.normalize();
location1.add(dir1);
print("1going");
float xdir2 = destination.x - location2.x;
float ydir2 = destination.y - location2.y;
PVector dir2 = new PVector (xdir2, ydir2);
dir2.normalize();
location2.add(dir2);
print("2going");
}
void checkProgress()
{
for (int siteNumber = 0; siteNumber < 2; siteNumber = siteNumber++);
if (dist(destination.x, destination.y, location1.x, location1.y) < 1) {
if (siteNumber <sites.size() -1) {
siteNumber++; // siteNumber = siteNumber + 1;
destination = sites.get(siteNumber);
changeDirection = true;
println("1reached final site");
} else {
destination.x = concretePlant.x;
destination.y = concretePlant.y;
println ("1back to home");
}
println("1progress checked ");
}
if (dist(destination.x, destination.y, location2.x, location2.y) < 1) {
if (siteNumber <sites.size() -1) {
siteNumber++; // siteNumber = siteNumber + 1;
destination = sites.get(siteNumber);
changeDirection = true;
println("2reached final site");
} else {
destination.x = concretePlant.x;
destination.y = concretePlant.y;
println ("2back to home");
}
println("2progress checked ");
}
}
void updateLorry()
{
displayLorry();
Move();
checkProgress();
}
}