Swing 用Java设计一个自己运行的GUI
我创建了一个程序,将“点”的三个子类随机分布在一个100 x 100的网格中:糖、蚂蚁和蜘蛛。蚂蚁向糖走去,蜘蛛向蚂蚁走去,当蚂蚁吃光了所有的糖后,它们就跑开了。我有一个“while”循环,每次最多移动蚂蚁一个单位,蜘蛛至少移动两个单位。蚂蚁不可避免地全部被吃掉,这终止了“while”循环。现在,我想用GUI直观地表示这个程序,但我不知道如何去做。我非常确定我需要将窗格布局设置为“null”,这样我就不会被迫每个区域只使用一个对象。除此之外,我不太确定 我会发布所有代码,但有点冗长。我真正想知道的是如何一次表示所有这些类型的点的所有实例,以及如何根据游戏类的“while”循环的原则移动它们(自行,无需人工点击“next turn”,一直到没有蚂蚁离开为止) }Swing 用Java设计一个自己运行的GUI,swing,user-interface,draw,Swing,User Interface,Draw,我创建了一个程序,将“点”的三个子类随机分布在一个100 x 100的网格中:糖、蚂蚁和蜘蛛。蚂蚁向糖走去,蜘蛛向蚂蚁走去,当蚂蚁吃光了所有的糖后,它们就跑开了。我有一个“while”循环,每次最多移动蚂蚁一个单位,蜘蛛至少移动两个单位。蚂蚁不可避免地全部被吃掉,这终止了“while”循环。现在,我想用GUI直观地表示这个程序,但我不知道如何去做。我非常确定我需要将窗格布局设置为“null”,这样我就不会被迫每个区域只使用一个对象。除此之外,我不太确定 我会发布所有代码,但有点冗长。我真正想知道
公共类蚂蚁扩展点{
私人双身份证;
私人双重计算;
私家布尔中毒;
私人内部蜘蛛座;
公共蚂蚁(双inX,双inY,双身份){
超级(inX,inY);
这个.x=inX;
这个.y=inY;
this.moveable=true;
this.ID=身份;
这个.calCount=0;
这个。中毒=错误;
这一点。蜘蛛座=0;
}
公共双getID(){
返回此.ID;
}
公共双精度getCalCount(){
返回此.calCount;
}
公共布尔值已中毒(){
归还这个。中毒;
}
公共的双重捕食者(){
把这个还给蜘蛛座;
}
公共双距离糖(糖甜){
返回此值。距离(甜蜜);
}
公共空间设置计数(双倍食物){
这是。calCount+=食物;
}
公共空间中毒(){
这是真的;
}
公共空食(糖醋){
如果(此距离(aSugar)<0.01){
aSugar.antCounter();
}
}
公共无效蜘蛛计数器(){
++蜘蛛座;
}
公共糖类(糖类aSug){
吃过的;
if(aSug.findPoison()){
这个。toxify();
}
}
公共双关糖厂(ArrayList sugars){
双糖浆=1000;
对于(int i=0;i
}
公共类Spider扩展点{
私人双身份证;
私人双spiCalCount;
公共蜘蛛(双inX,双inY,双身份){
超级(inX,inY);
这个.x=inX;
这个.y=inY;
this.moveable=true;
this.ID=身份;
这个.spiCalCount=0;
}
公共双getID(){
返回此.ID;
}
公共食品账户(双倍食品){
这个。spiCalCount+=食物;
}
公共双精度getSpiCalCount(){
返回此.spiCalCount;
}
公共双距离蚂蚁(Ant-Ant){
返回此值。距离(ant);
}
公共空食(蚂蚁){
如果(该距离(ant)<0.01){
ant.spiderCounter();
}
}
公共空间蚂蚁(蚂蚁){
ant.toxify();
}
公共双关闭列表(ArrayList ants){
双蚂蚁距离=1000;
对于(int i=0;ipublic 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;
}
class Sugar {
public void draw(Graphics g) {
...
}
}
class Spider{
public void draw(Graphics g) {
...
}
}
class Ant{
public void draw(Graphics g) {
...
}
}