2
我和一個合作伙伴一直在爲我們班級創作一款遊戲。雖然我們可以製作形狀,但我們無法加載精靈。我不確定所有這些代碼是否必要,但它似乎是必要的類,並且在這裏我們的代碼的任何其他幫助將不勝感激。Sprites not loading
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class BoardGraphics extends JPanel{
public BoardGraphics(){
initGraphics();
}
//Image Url's
String grassTS = "src/Resources/Sprites/GrassTile_1.png";
//Image objects
BufferedImage grassTI;
public void layTiles(Graphics g){
for(int i = 0; i < 21; i++){
for(int k = 0; k < 21; k++){
}
}
}
public void loadImage(String url, Image image){
try {
image = ImageIO.read(new File(url));
if(image != null){
System.out.println(url + ", has been loaded!");
}
}catch (IOException e){
e.printStackTrace();
}
}
public void initGraphics(){
setBackground(Color.WHITE);
setDoubleBuffered(true);
loadImage(grassTS, grassTI);
}
public void drawPlayer(Graphics g){
g.fillOval(5,5,32,32);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawImage(grassTI,21,21,null);
drawPlayer(g);
layTiles(g);
}
}
我的合作伙伴已經完成了大部分代碼,並且我對Java中的圖形化編程知之甚少。另外我試圖分開這兩個類。
import java.nio.file.Paths;
import javafx.embed.swing.*;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.Media;
import javax.swing.*;
import java.awt.*;
public class Game extends Canvas implements Runnable {
boolean running = true;
public final int Wid = 480;
public final int Hei = 480;
public final int scale = 3;
public final String name = "Hasty Harvester";
private JFrame frame;
JFXPanel in = new JFXPanel();
public Board b;
public KeyIn input;
public BoardGraphics g = new BoardGraphics();
public boolean start = true;
public Game(){
init();
frame = new JFrame(name);
frame.setPreferredSize(new Dimension(Wid, Hei));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
input = new KeyIn(in);
frame.add(in);
frame.add(g);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void run(){
long lastTime = System.nanoTime();
double ns = 100000000.0/60.0;
int frames = 0;
int ticks = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0;
Farm farmville = new Farm("src/Resources/Levels/levels.txt");
b = farmville.getMap(0);
music("src/Resources/Music/CasualGameTrack_Alexandr_Zhelanov_Ingame.mp3");
while(running) {
long now = System.nanoTime();
delta += (now - lastTime)/ns;
lastTime = now;
boolean shouldrender = false;
while (delta >= 1) {
ticks++;
tick(b);
delta--;
shouldrender = true;
}
if(shouldrender) {
frames++;
render();
}
//Per second record the amount of frames + ticks, and reset them
if (System.currentTimeMillis() - lastTimer >= 1000) {
lastTimer +=1000;
System.out.println("Frames: " + frames + ", Ticks:" + ticks);
frames = 0;
ticks = 0;
}
}
}
public void init(){
in.setFocusable(true);
}
public void music(String mus){
Media sung = new Media(Paths.get(mus).toUri().toString());
MediaPlayer med = new MediaPlayer(sung);
med.play();
}
public void tick(Board b){
if(input.up.isPressed() == true){
System.out.println("Up");
input.up.toggle(false);
b.movePlayer("Up");
}
if(input.down.isPressed() == true){
System.out.println("down");
input.down.toggle(false);
b.movePlayer("Down");
}
if(input.right.isPressed() == true){
System.out.println("right");
input.right.toggle(false);
b.movePlayer("Right");
}
if(input.left.isPressed() == true){
System.out.println("left");
input.left.toggle(false);
b.movePlayer("Left");
}
}
public void render(){
}
public void start(){
new Thread(this).start();
running = true;
}
public void stop(){
running = false;
}
public static void main(String[] args){
new Game().start();
}
}