在Java中设计自己运行的GUI

时间:2014-07-07 07:06:44

标签: swing user-interface draw

我创建了一个程序,在100 x 100网格中随机分散三个“点”子类:糖,蚂蚁和蜘蛛。蚂蚁向糖中移动,蜘蛛向蚂蚁移动,当蚂蚁吃掉所有的糖时,它们就会逃离蜘蛛。我有一个“while”循环,每次最多移动蚂蚁一个单位,蜘蛛至少移动两个单位。蚂蚁不可避免地全部被消耗,这终止了“while”循环。现在,我想用GUI直观地表示这个程序,我不知道如何去做。我很确定我需要将窗格布局设置为“null”,这样我就不会强制每个区域只使用一个对象。除此之外,我不太确定。

我会发布所有代码,但它有点冗长。我真正想知道的是如何一次表示所有这些类型的点的所有实例,以及如何根据游戏类“while”循环的原理移动它们(单独使用,没有人为点击“下一步转过来,“一直到没有蚂蚁离开。”

public class Sugar extends Point {
private double calories; 
private double calories;
private boolean eaten;
private int antsEating;
private boolean isPoisonous;
public Sugar(double inX, double inY, double inCal, boolean poison) {
    super(inX, inY);
    x = inX;
    y = inY;
    moveable = false;
    calories = inCal;
    eaten = false;
    antsEating = 0;
    isPoisonous = poison;
}
public double getCalories() {
    return this.calories;
}
public boolean eatenYet() {
    return this.eaten;
}
public double getEaters() {
    return this.antsEating;
}
public void antCounter() {
    ++antsEating;
}
public void eaten() {
    this.eaten = true;
}
public boolean findPoison() {
    return this.isPoisonous;
}

}

public class Ant extends Point{
private double ID;
private double calCount;
private boolean poisoned;
private int spidersEating;

public Ant(double inX, double inY, double identity) {
    super(inX, inY);
    this.x = inX;
    this.y = inY;
    this.moveable = true;
    this.ID = identity;
    this.calCount = 0;
    this.poisoned = false;
    this.spidersEating = 0;
}

public double getID() {
    return this.ID;
}

public double getCalCount() {
    return this.calCount;
}

public boolean wasPoisoned() {
    return this.poisoned;
}

public double getPredators() {
    return this.spidersEating;
}

public double distanceToSugar(Sugar sweet) {
    return this.distance(sweet);
}

public void setCalCount(double food) {
    this.calCount += food;
}

public void toxify() {
    this.poisoned = true;
}

public void feeding(Sugar aSugar) {
    if (this.distance(aSugar) < 0.01) {
        aSugar.antCounter();
    }
}

public void spiderCounter() {
    ++spidersEating;
}

public void eatSugar(Sugar aSug) {
    aSug.eaten();
    if (aSug.findPoison()) {
        this.toxify();
    }
}

public double closestSugarDist(ArrayList<Sugar> sugars) {
    double sugarDist = 1000;
    for (int i = 0; i < sugars.size(); i++) {
        if (this.distanceToSugar(sugars.get(i)) < sugarDist) {
            sugarDist = this.distanceToSugar(sugars.get(i));
        }
    }
    return sugarDist;
}

public Sugar getClosestSugar(ArrayList<Sugar> sugars) {
    Sugar sucrose = new Sugar(0, 0, 0, false);
    double sugarDist = this.closestSugarDist(sugars);
    for (int i = 0; i < sugars.size(); i++) {
        if (sugarDist == this.distanceToSugar(sugars.get(i))) {
            sucrose = sugars.get(i);
        }
    }
    return sucrose;
}

public void moveAnt(Sugar aSugar) {
    double sugarDistX = (aSugar.getX() - this.getX());
    double sugarDistY = (aSugar.getY() - this.getY());
    double sugarDistZSquare = ((sugarDistX * sugarDistX) + (sugarDistY * sugarDistY));
    double sugarDistZ = Math.sqrt(sugarDistZSquare);
    double newX = (sugarDistX/sugarDistZ);
    double newY = (sugarDistY/sugarDistZ);
    if (this.distance(aSugar) < 1) {
        this.translate(sugarDistX, sugarDistY);
    }
    else {
        this.translate(newX, newY);
    }

    System.out.println("Ant " + this.ID + " is at: " + this.toString());
    System.out.println("Ant " + this.ID + " ate this much: " + this.getCalCount());
}

public Spider averageSpider(ArrayList<Spider> spiders) {
    Spider spy = new Spider(0, 0, 0);
    double spyX = 0;
    double spyY = 0;
    for (int i = 0; i < spiders.size(); ++i) {
        spyX += spiders.get(i).getX();
        spyY += spiders.get(i).getY();
    }
    spyX = (spyX / spiders.size());
    spyY = (spyY / spiders.size());
    spy.translate(spyX, spyY);
    return spy;
}

public void runAway(Spider aSpider) {
    double spiderDistX = (aSpider.getX() - this.getX());
    double spiderDistY = (aSpider.getY() - this.getY());
    double spiderDistZSquare = ((spiderDistX * spiderDistX) + (spiderDistY * spiderDistY));
    double spiderDistZ = Math.sqrt(spiderDistZSquare);
    double newX = -(spiderDistX/spiderDistZ);
    double newY = -(spiderDistY/spiderDistZ);
    if (this.distance(aSpider) < 1) {
        this.translate(spiderDistX, spiderDistY);
    }
    else {
        this.translate(newX, newY);
    }

    System.out.println("Ant " + this.ID + " is at: " + this.toString());
    System.out.println("Ant " + this.ID + " ate this much: " + this.getCalCount());
}

}

public class Spider extends Point{
private double ID;
private double spiCalCount;

public Spider(double inX, double inY, double identity) {
    super(inX, inY);
    this.x = inX;
    this.y = inY;
    this.moveable = true;
    this.ID = identity;
    this.spiCalCount = 0;
}

public double getID() {
    return this.ID;
}

public void setSpiCalCount(double food) {
    this.spiCalCount += food;
}

public double getSpiCalCount() {
    return this.spiCalCount;
}

public double distanceToAnt(Ant ant) {
    return this.distance(ant);
}

public void feeding(Ant ant) {
    if (this.distance(ant) < 0.01) {
        ant.spiderCounter();
    }
}

public void eatAnt(Ant ant) {
    ant.toxify();
}

public double closestAntDist(ArrayList<Ant> ants) {
    double antDist = 1000;
    for (int i = 0; i < ants.size(); i++) {
        if (this.distanceToAnt(ants.get(i)) < antDist) {
            antDist = this.distanceToAnt(ants.get(i));
        }
    }
    return antDist;
}

public Ant getClosestAnt(ArrayList<Ant> ants) {
    Ant worker = new Ant(0, 0, 0);
    double antDist = this.closestAntDist(ants);
    for (int i = 0; i < ants.size(); i++) {
        if (antDist == this.distanceToAnt(ants.get(i))) {
            worker = ants.get(i);
        }
    }
    return worker;
}

public void moveSpider(Ant ant) {
    double antDistX = (ant.getX() - this.getX());
    double antDistY = (ant.getY() - this.getY());
    double antDistZSquare = ((antDistX * antDistX) + (antDistY * antDistY));
    double antDistZ = Math.sqrt(antDistZSquare);
    //changes these to include the spiCalCount
    double newX = (2 + (this.getSpiCalCount() / 5)) * (antDistX/antDistZ);
    double newY = (2 + (this.getSpiCalCount() / 5)) * (antDistY/antDistZ);
    //change this to include spiCalCount
    if (this.distance(ant) < (2 + (this.getSpiCalCount() / 5))) {
        this.translate(antDistX, antDistY);
    }
    else {
        this.translate(newX, newY);
    }

    System.out.println("Spider " + this.ID + " is at: " + this.toString());
    System.out.println("Spider " + this.ID + " ate this much: " + this.getSpiCalCount());
}

}

public class Game {
public static void main(String[] args) {
    if (args.length > 3) {
        System.err.println("You can't have more than three parameters!");
        System.exit(0);
    }
    if (args.length < 3) {
        System.err.println("You can't have less than three parameters!");
        System.exit(0);
    }
    for (int i = 0; i < args.length; ++i) {
        try {
            Integer.parseInt(args[i]);
        } catch (NumberFormatException nfe) {
            System.err.println("Please only enter integers.");
            System.exit(0);
        }
    }
    for (int j = 0; j < args.length; ++j) {
        if (!(Integer.parseInt(args[j]) > 0)) {
            System.err.println("Please enter only positive integers.");
            System.exit(0);
        }
    }

    int Ant = Integer.parseInt(args[0]);
    int Sug = Integer.parseInt(args[1]);
    int Spi = Integer.parseInt(args[2]);

    ArrayList<Ant> antList = new ArrayList<Ant>();
    for (int i = 0; i < Ant; ++i) {
        double hunna = (Math.random() * 100);
        double newHun = (Math.random() * 100);
        antList.add(new Ant(hunna, newHun, i));
        //antList.add(new Ant(0, 0, i));
    }

    ArrayList<Sugar> sugarList = new ArrayList<Sugar>();
    for (int j = 0; j < Sug; ++j) {
        double hunna = (Math.random() * 100);
        double newHun = (Math.random() * 100);
        double fiddy = (Math.random() * 4 + 1);
        boolean toxin = getRandomBoolean();


        sugarList.add(new Sugar(hunna, newHun, fiddy, toxin));
    }

    ArrayList<Spider> spiderList = new ArrayList<Spider>();
    for (int k = 0; k < Spi; ++k) {
        double hunna = (Math.random() * 100);
        double newHun = (Math.random() * 100);
        spiderList.add(new Spider(hunna, newHun, k));
    }
    feedingTime(antList, sugarList, spiderList);
}

public static void feedingTime(ArrayList<Ant> ants, ArrayList<Sugar> sugars, ArrayList<Spider> spiders) {
    int counter = 0;

    while (ants.size() > 0) {
        ++counter;
        //moves the spiders
        for (int a = 0; a < spiders.size(); a++) {
            Ant closeAnt = (spiders.get(a).getClosestAnt(ants));
            spiders.get(a).moveSpider(closeAnt);
        }
        //counts how many spiders are feeding on each ant
        for (int b = 0; b < spiders.size(); b++) {
            Ant closeAnt = (spiders.get(b).getClosestAnt(ants));
            spiders.get(b).feeding(closeAnt);
        }
        //poisons ants if they are too close to the spiders, marks eaten ants as "poisoned"
        for (int c = 0; c < spiders.size(); c++) {
            Ant closeAnt = (spiders.get(c).getClosestAnt(ants));
            if (spiders.get(c).distanceToAnt(closeAnt) < 0.01) {
                spiders.get(c).eatAnt(closeAnt);
                spiders.get(c).setSpiCalCount((closeAnt.getCalCount())/(closeAnt.getPredators()));
            }
        }
        //clears away any spider-poisoned ants
        killAnts(ants);
        //does the same thing but with ants instead of spiders and sugar instead of ants
        if (sugars.size() > 0) {
            for (int i = 0; i < ants.size(); i++) {
                Sugar closeSug = (ants.get(i).getClosestSugar(sugars));
                ants.get(i).moveAnt(closeSug);
            }
            for (int j = 0; j < ants.size(); j++) {
                Sugar closeSug = (ants.get(j).getClosestSugar(sugars));
                ants.get(j).feeding(closeSug);
            }
            for (int k = 0; k < ants.size(); k++) {
                Sugar closeSug = (ants.get(k).getClosestSugar(sugars));
                if (ants.get(k).distanceToSugar(closeSug) < 0.01) {
                    ants.get(k).eatSugar(closeSug);
                    ants.get(k).setCalCount((closeSug.getCalories())/(closeSug.getEaters()));
                }
            }
        }
        //after all the sugar has been eaten, the ants run away from the spider
        else {
            for (int i = 0; i < ants.size(); i++) {
                Spider spyder = (ants.get(i).averageSpider(spiders));
                ants.get(i).runAway(spyder);
            }
        }
        //clears away all sugar-poisoned ants and eaten sugar
        killAnts(ants);
        removeSugar(sugars);
    }
    Spider fatSpi = (fattestSpider(spiders));
    System.out.println("The fattest Spider is: " + fatSpi.getID() + " with cals: " + fatSpi.getSpiCalCount());
    System.out.println("Turns executed: " + counter);
}


public static void removeSugar(ArrayList<Sugar> sugars) {
    int i = 0;
    while (i < sugars.size()) {
        if (!(sugars.get(i).eatenYet())) {
            ++i;
        }
        else {
            sugars.remove(i);
        }
    }
}

public static void killAnts(ArrayList<Ant> ants) {
    int i = 0;
    while (i < ants.size()) {
        if (!(ants.get(i).wasPoisoned())) {
            ++i;
        }
        else {
            ants.remove(i);
        }
    }
}

public static Spider fattestSpider(ArrayList<Spider> someSpiders) {
    double cals = -1;
    Spider dummySpi = new Spider (0,0,0);
    for (int i = 0; i < someSpiders.size(); ++i) {
        if (someSpiders.get(i).getSpiCalCount() > cals) {
            cals = someSpiders.get(i).getSpiCalCount();
        }
    }
    for (int j = 0; j < someSpiders.size(); ++j) {
        if (someSpiders.get(j).getSpiCalCount() == cals) {
            return someSpiders.get(j);
        }
    }
    return dummySpi;
}

public static boolean getRandomBoolean() {
    return Math.random() < 0.5;
}

}

1 个答案:

答案 0 :(得分:0)

我过去做过类似的事情,我要说的一点是不要从Point延伸,而是要有类似的东西:

class Sugar {

    public void draw(Graphics g) {
        ...
    }
}


class Spider{

    public void draw(Graphics g) {
        ...
    }
}


class Ant{

    public void draw(Graphics g) {
        ...
    }
}

原因是,如果在某个时间点而不是某个点,你想画一条线,或者是蜘蛛或蚂蚁的图像呢?

一般来说,拥有这种战略模式也更好。这些类型需要知道如何绘制自己。