java获取arraylist_从我的arrayList(Java)中获取特定的对象

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 21:04   4881   0

嗨,我的问题是,在我的代码我打电话的方法,计算2点之间的差异,然后如果该距离少于7,它会调用另一个类应该改变目标的颜色为红色的方法...我的问题是在我的arraylist我有3或5或取决于用户输入目标...所以我如何指定我的数组列表中的对象将要更改颜色> ???这是我的代码

package project1;

import java.util.*;

import javax.swing.*;

/**

*

* @author Elvis De Abreu

*/

public class TargetGallery

{

/**ArrayList of Targets initialize as a private*/

private ArrayList mytargets = new ArrayList<>();

/**Static object for the Target class*/

static Target tg = new Target();

/**Static object for the RifleSite class*/

static RifleSite rs = new RifleSite();

/**Static object for the TargetGallery class*/

static TargetGallery tgy = new TargetGallery();

/**the number of targets input by the user as private*/

private int number = 0;

/**array that store the distance between 2 point for each target*/

private double[] total;

/**

* Method that build the background of the canvas

* with a picture as a environment

*/

private void buildWorld()

{

StdDraw.setXscale(0, 250);

StdDraw.setYscale(0, 250);

StdDraw.picture(75, 130, "bath.jpeg", 450, 285);

}

/**

* Method that draw a weapon in the middle of the

* canvas as a shooter weapon

*/

private void drawShooter()

{

StdDraw.setXscale(0, 250);

StdDraw.setYscale(0, 250);

StdDraw.picture(125, 0, "weapon.png", 80, 45);

}

/**

* randomly generates X locations for the targets

* add them into the array list

*/

private void createTargets()

{

double x = 125;

double y = 175;

double radius = 7;

String input = JOptionPane.showInputDialog("Type a number" +

"between 2 and 5");

number = Integer.parseInt(input);

for(int i = 0; i < number; i++)

{

Target targ = new Target(x, y, radius);

mytargets.add(targ);

Random rand = new Random();

x = rand.nextInt(400) + 10;

for (Target e: mytargets)

{

if ((e.getX() <= (x+10)) || (e.getX() >= (x-10)))

{

mytargets.clear();

i--;

continue;

}

}

}

}

/**

* Method that run different methods which start the program

*/

public void run()

{

tgy.buildWorld(); //call the buildWorld method

tgy.drawShooter(); //call the drawShooter method

tgy.createTargets(); //call the createTarget method

tgy.simulate(); //call the simulate method

}

/**

* calculates the distance between the RifleSite and the Targets

*/

public void calcDistance()

{

//variable declaration/initialization

double distance;

double distance1;

int i = 0;

total = new double[number];

//for each loop to calculate x and y location of RifleSite and Targets

for (Target e: mytargets)

{

distance = Math.pow(e.getX()-rs.getX(), 2.0);

distance1 = Math.pow(e.getY()-rs.getY(), 2.0);

total[i++] = Math.sqrt(distance + distance1);

}

}

/**

* Method that simulates the game

*/

public void simulate()

{

//Variable declaration/initialization

boolean alive = true;

for(Target e: mytargets)

{

e.drawAlive();

}

rs.drawRifleSite();

//loop that will run while there is still targets alive or user press q

while(alive == true)

{

//if user press a key this

if (StdDraw.hasNextKeyTyped())

{

char ch = StdDraw.nextKeyTyped(); //store the key pressed

//if person press Q will quit the program

if (ch == 'q')

{

int done = JOptionPane.showConfirmDialog(null,

"The Program will close now bye :)");

System.exit(0);

}

else if (ch == 'f')

{

tgy.calcDistance(); //calculates the distance

//if statement to check if the distance if less than radius

for(int i = 0; i < number; i++)

{

if (total[i] <= 7)

{

//THIS IS WHERE MY METHOD SHOULD GO

//SHOULD BE SOMETHING LIKE E.drawDead

}

}

}

}

}

}

/**

* Method for the main of the Program

* @param args the command line arguments

*/

public static void main(String[] args)

{

}

}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP