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

时间:2012-03-08 02:34:17

标签: arraylist draw

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

package project1;

import java.util.*;
import javax.swing.*;

/**
 *
 * @author Elvis De Abreu
 */
public class TargetGallery
{
    /**ArrayList of Targets initialize as a private*/
    private ArrayList<Target> 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)
    {

    }
}

1 个答案:

答案 0 :(得分:0)

像这样:mytargets.get(INDEX GO HERE) 我希望有所帮助

(索引从0开始)

例如:

Target t=mytargets.get(0);
if ((t.getX() > 10) && (t.getY() > 10))
{
    //blablabla
}