Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未加载的精灵_Java_Image_Graphics - Fatal编程技术网

Java 未加载的精灵

Java 未加载的精灵,java,image,graphics,Java,Image,Graphics,我和一个搭档一直在为我们班制作一个游戏。虽然我们能够制作形状,但无法加载精灵。我不确定所有这些代码是否都是必需的,但它似乎是必需的类,如果您对我们的代码有任何其他帮助,我们将不胜感激 import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException;

我和一个搭档一直在为我们班制作一个游戏。虽然我们能够制作形状,但无法加载精灵。我不确定所有这些代码是否都是必需的,但它似乎是必需的类,如果您对我们的代码有任何其他帮助,我们将不胜感激

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);

}
}

我发现问题出在loadImage方法中,因为它正在将图像加载到新的图像对象中

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();

}

}