现在我正在做一个看起来像这样的if语句:
if(Game.nflteams.subList(0, 1).equals("Arizona Cardinals"))
// do something like draw their logo to a made JFrame
每次Game.nflteams.subList(0, 1)
等于"Arizona Cardinals"
,或者到目前为止,我已经完成了3个小组中的一个,没有任何内容吸引到屏幕上。
我所有的课程都在下面:
我的主要课程:
public class Football {
public static void main(String[] args) {
Game game = new Game();
game.game();
GameII gamei = new GameII("Football Sim", 1000, 500);
gamei.start();
}
}
我的游戏课程:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class Game {
private static ArrayList<String> teams = new ArrayList<String>();
private Random random = new Random();
public void game() {
teams.add("New England Patriots (0 - 1)");
teams.add("Dallas Cowboys (0 - 1)");
teams.add("Oakland Raiders (1 - 0)");
teams.add("Philadelphia Eagles (0 - 1)");
teams.add("New York Giants (0 - 1)");
teams.add("Seattle Seahawks (1 - 0)");
teams.add("Pittsburgh Steelers (1 - 0)");
teams.add("Green Bay Packers (1 - 0)");
teams.add("Denver Broncos (0 - 1)");
teams.add("San Fransisco 49ers (0 - 1)");
teams.add("Chicago Bears (1 - 0)");
teams.add("Minnesota Vikings (0 - 1)");
teams.add("Carolina Panthers (1 - 0)");
teams.add("Cleveland Browns (0 - 1)");
teams.add("Los Angeles Rams (1 - 0)");
teams.add("Kansas City Cheifs (0 - 1)");
teams.add("Washington Redskins (1 - 0)");
teams.add("Atlanta Falcons (1 - 0)");
teams.add("Baltimore Ravens (1 - 0)");
teams.add("Houston Texans (0 - 1)");
teams.add("Detroit Lions (1 - 0)");
teams.add("New York Jets (0 - 1)");
teams.add("Los Angeles Chargers (0 - 1)");
teams.add("Arizona Cardinals (0 - 1)");
teams.add("Buffalo Bills (1 - 0)");
teams.add("New Orleans Saints (1 - 0)");
teams.add("Miami Dolphins (0 - 1)");
teams.add("Jacksonville Jaguars (1 - 0)");
teams.add("Cincinatti Bengals (0 - 1)");
teams.add("Tampa Bay Buccaneers (1 - 0)");
teams.add("Indianapolis Colts (0 - 1)");
teams.add("Tennessee Titans (1 - 0)");
Collections.shuffle(teams);
int t2touch = (random.nextInt(5) + 1) * 7;
int t2fielgoal = (random.nextInt(5) + 1) * 3;
int t2score = t2touch + t2fielgoal;
int t1touch = random.nextInt(5) * 7;
int t1fielgoal = random.nextInt(5) * 3;
int t1score = t1touch + t1fielgoal;
System.out.println(teams.subList(0, 1) + " (away) " + " vs " + teams.subList(1, 2) + " (home)");
if(t1score > t2score) {
System.out.println("The " + teams.subList(0, 1) + " win "
+ t1score + " (" + t1touch / 7 + " touchdowns, " + t1fielgoal / 3 + " field goals)" +
" to "
+ t2score + " (" + t2touch / 7 + " touchdowns, " + t2fielgoal / 3 + " field goals)" + "!");
} else if(t2score > t1score) {
System.out.println("The " + teams.subList(1, 2) + " win "
+ t2score + " (" + t2touch / 7 + " touchdowns, " + t2fielgoal / 3 + " field goals)" +
" to "
+ t1score + " (" + t1touch / 7 + " touchdowns, " + t1fielgoal / 3 + " field goals)" + "!");
} else {
System.out.println("It's a tie. "
+ teams.subList(0, 1) + " " + t1score + " (" + t1touch / 7 + " touchdowns, " + t1fielgoal / 3 + " field goals" +
") to "
+ teams.subList(1, 2) + " " + t2score + " (" + t2touch / 7 + " touchdowns, " + t2fielgoal / 3 + " field goals");
}
}
public static ArrayList<String> getTeams() {
return teams;
}
}
我的GameII课程:
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import foot.display.Display;
import foot.gfx.Assets;
public class GameII implements Runnable {
private Display display;
public int width, height;
public String title;
private boolean running = false;
private Thread thread;
private BufferStrategy bs;
private Graphics g;
public GameII(String title, int width, int height){
this.width = width;
this.height = height;
this.title = title;
}
private void init(){
display = new Display(title, width, height);
Assets.init();
}
private void tick(){
}
private void render(){
bs = display.getCanvas().getBufferStrategy();
if(bs == null){
display.getCanvas().createBufferStrategy(3);
return;
}
g = bs.getDrawGraphics();
//Clear Screen
g.clearRect(0, 0, width, height);
//Draw Here!
// team 1's
if(Game.getTeams().subList(0, 1).equals("Arizona Cardinals (0 - 1)"))
g.drawImage(Assets.cardinals, 100, 100, null);
if(Game.getTeams().subList(0, 1).equals("Jacksonville Jaguars (1 - 0)))
g.drawImage(Assets.jaguars, 100, 100, null);
if(Game.getTeams().subList(0, 1).equals("New England Patriots (0 - 1)"))
g.drawImage(Assets.patriots, 100, 100, null);
// team 2's
if(Game.getTeams().subList(1, 2).equals("Arizona Cardinals (0 - 1)"))
g.drawImage(Assets.cardinals, 400, 400, null);
if(Game.getTeams().subList(1, 2).equals("Jacksonville Jaguars (1 - 0)"))
g.drawImage(Assets.jaguars, 400, 400, null);
if(Game.getTeams().subList(1, 2).equals("New England Patriots (0 - 1)"))
g.drawImage(Assets.patriots, 400, 400, null);
//End Drawing!
bs.show();
g.dispose();
}
public void run(){
init();
while(running){
tick();
render();
}
stop();
}
public synchronized void start(){
if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
public synchronized void stop(){
if(!running)
return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
我的资产课程:
import java.awt.image.BufferedImage;
public class Assets {
private static final int width = 134, height = 100;
public static BufferedImage cardinals, jaguars, patriots, saints, browns, broncos;
public static void init(){
SpriteSheet sheet = new SpriteSheet(ImageLoader.loadImage("/textures/every nfl team.jpg"));
cardinals = sheet.crop(0, 0, width, height);
jaguars = sheet.crop(width, 0, width, height);
patriots = sheet.crop(width * 2, 0, width, height);
saints = sheet.crop(width * 4, 0, width, height);
browns = sheet.crop(width * 5, 0, width, height);
broncos = sheet.crop(width * 6, 0, width, height);
}
}
我的SpriteSheet类:
import java.awt.image.BufferedImage;
public class SpriteSheet {
private BufferedImage sheet;
public SpriteSheet(BufferedImage sheet){
this.sheet = sheet;
}
public BufferedImage crop(int x, int y, int width, int height){
return sheet.getSubimage(x, y, width, height);
}
}
我的ImageLoader类:
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageLoader {
public static BufferedImage loadImage(String path){
try {
return ImageIO.read(ImageLoader.class.getResource(path));
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
return null;
}
}
和我的Display类:
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Display {
private JFrame frame;
private Canvas canvas;
private String title;
private int width, height;
public Display(String title, int width, int height){
this.title = title;
this.width = width;
this.height = height;
createDisplay();
}
private void createDisplay(){
frame = new JFrame(title);
frame.setSize(width, height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
canvas = new Canvas();
canvas.setPreferredSize(new Dimension(width, height));
canvas.setMaximumSize(new Dimension(width, height));
canvas.setMinimumSize(new Dimension(width, height));
frame.add(canvas);
frame.pack();
}
public Canvas getCanvas() {
return canvas;
}
}
这些都是我的课程,应该足以重现这个问题
只是说:
理论上/逻辑上,我的公式应该有用
如果你考虑一下,我的步骤是完全合乎逻辑的:
1)创建一个字符串的ArrayList并将所有NFL的团队名称添加到其中
2)随机播放ArrayList
3)随机选择其中两个“球队”进行“比赛”
4)测试是否选择了特定的团队
5)如果是这样,请将该团队的徽标绘制到之前制作的JFrame
答案 0 :(得分:3)
这是很多代码。但我认为你想使用contains
而不是equals
:
if(Game.nflteams.subList(0, 1).contains("Arizona Cardinals"))