我正在制作游戏,而我的JFrame仅添加了一个类,但我没有出错,但是未显示两个类之一。我试图使JFrame frame 2 = new JFrame();,但什么也没有。

Game.java:

import java.awt.Component;
import javax.swing.JFrame;

public class Game{

private static final int WIDTH = 800;
private static final int HEIGHT = 600;
private static final String TITLE = "Game Dev [ Week #1 ]";

public static void main(String[] args){
    JFrame frame = new JFrame();
    Player player = new Player();
    Rabbit rabbit = new Rabbit();

    // Draw on the map

    player.setPlayer(250,250);
    rabbit.setRabbit(200,200);

    // Draw on the map
    frame.add(player);
    frame.add(rabbit); // Add only this

    // Window
    frame.setVisible(true);
    frame.setSize(WIDTH,HEIGHT);
    frame.setTitle(TITLE);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}


Player.java:

public class Player extends JPanel implements ActionListener, KeyListener{

// Varibles
Timer time = new Timer(5, this);
static int x; double velX = 0;
static int y; double velY = 0;
private BufferedImage player;

boolean W = false;
boolean A = false;
boolean S = false;
boolean D = false;

public void setPlayer(int x, int y){
    this.x = x;
    this.y = y;
}

public Player(){
    time.start();
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);

    try {
        player = ImageIO.read(getClass().getResourceAsStream("/Player.png"));
    } catch (IOException e){
        e.printStackTrace();
    }
}

// Draw player
public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawImage(player, (int)x, (int)y, 100, 100, null);
}

// Set the start up position of player
public void actionPerformed(ActionEvent e){
    x += velX;
    y += velY;

    repaint();
}

// Functions for keyEvent
public void keyPressed(KeyEvent e){
    int key = e.getKeyCode();
    if(key == KeyEvent.VK_W) // UP
        velY = -0.5;
    if(key == KeyEvent.VK_A) // LEFT
        velX = -0.5;
    if(key == KeyEvent.VK_S) // DOWN
        velY = 1.5;
    if(key == KeyEvent.VK_D) // RIGHT
        velX = 1.5;
}

public void keyTyped(KeyEvent e){}

// IF any key released
public void keyReleased(KeyEvent arg0) {
    velX = 0;
    velY = 0;
    W = false;
    A = false;
    S = false;
    D = false;
}
}


Rabbit.java:

public class Rabbit extends JPanel implements ActionListener{

static int x; double velX = 0;
static int y; double velY = 0;
BufferedImage rabbit;

public void setRabbit(int x, int y){
    this.x = x;
    this.y = y;
}

public Rabbit(){
    try {
        rabbit = ImageIO.read(getClass().getResourceAsStream("/Rabbit.png"));
    } catch (IOException e){
        e.printStackTrace();
    }
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawImage(rabbit, (int)x, (int)y, 100, 100, null);
}

public void actionPerformed(ActionEvent e){
    x += velX;
    y += velY;
    repaint();
}
}

最佳答案

发生的事情是JFrame没有将Player和Rabbit的图形结合在一起;而是将播放器作为面板放置,然后放置兔子。因此,玩家和兔子总是分开的。您想要的是Player和Rabbit类,而不是扩展JPanel,而是要有一个名为paint(Graphics)的方法。这样,您可以创建一个扩展了GamePanel的名为JPanel的类,并创建新的方法setPlayer(Player)setRabbit(Rabbit)。 GamePanel类应如下所示:

public class GamePanel extends JPanel {
    Player player;
    Rabbit rabbit;
    public void setPlayer(Player player) {
        this.player = player;
    }
    public void setRabbit(Rabbit rabbit) {
        this.rabbit = rabbit;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        player.paint(g);
        rabbit.paint(g);
    }
}


并且您的Player类应如下所示:

public class Player {
    public void paint(Graphics g) {
        // How to paint the graphics in the player class
    }
}


并且Rabbit类的外观应与public class Rabbit相同。

希望有帮助!

编辑:完整脚本

package game;

import java.awt.Graphics;

public interface Displayable {
    public void paint(Graphics graphics);
}

package game;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Rabbit implements Displayable {
    private int x, y;
    private Image image;

    public Rabbit(int x, int y) throws IOException {
        this.x = x;
        this.y = y;
        this.image = ImageIO.read(new File("RABBIT_FILE")); // Replace
                                                            // "RABBIT_FILE"
                                                            // with the image
                                                            // file you have
    }

    public void paint(Graphics graphics) {
        graphics.drawImage(this.image, this.x, this.y, new ImageObserver() {
            public boolean imageUpdate(Image img, int infoflags, int x, int y,
                    int width, int height) {
                return false;
            }
        });

        /**
         * Optional: JDK 8 only:
         *
         * graphics.drawImage(this.image, this.x, this.y, (Image img, int
         * infoflags, int x0, int y0, int width0, int height0) -> false);
         * //Lambda Expression
         */
    }

    public void setCoordinates(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

package game;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Player implements Displayable {
    private int x, y;
    private Image image;

    public Player(int x, int y) throws IOException {
        this.x = x;
        this.y = y;
        this.image = ImageIO.read(new File("PLAYER_FILE")); // Replace
                                                            // "PLAYER_FILE"
                                                            // with the image
                                                            // file you have
    }

    public void paint(Graphics graphics) {
        graphics.drawImage(this.image, this.x, this.y, new ImageObserver() {
            public boolean imageUpdate(Image img, int infoflags, int x, int y,
                    int width, int height) {
                return false;
            }
        });

        /**
         * Optional: JDK 8 only:
         *
         * graphics.drawImage(this.image, this.x, this.y, (Image img, int
         * infoflags, int x0, int y0, int width0, int height0) -> false);
         * //Lambda Expression
         */
    }

    public void setCoordinates(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

package game;

import java.awt.Graphics;

import javax.swing.JPanel;

public final class GamePanel extends JPanel {
    private static final long serialVersionUID = -385535147711891740L;
    private Player player;
    private Rabbit rabbit;

    public void setPlayer(Player player) {
        this.player = player;
    }

    public void setRabbit(Rabbit rabbit) {
        this.rabbit = rabbit;
    }

    public GamePanel(Player player, Rabbit rabbit) {
        this.setPlayer(player);
        this.setRabbit(rabbit);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.player.paint(g);
        this.rabbit.paint(g);
    }
}

package game;

import javax.swing.JFrame;

public final class Main {
    public static void main(String... args) {
        try {
            JFrame frame = new JFrame("Player & Rabbit Game"); // Or whatever
                                                                // title
            // you have in mind
            GamePanel panel = new GamePanel(new Player(0, 0), new Rabbit(0, 0));
            frame.add(panel);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(3);
            frame.setExtendedState(6);
        } catch (Exception e) {
            // Handle Exception if file is corrupted, unable to be read to an
            // image, or does not exist
        }
    }
}

关于java - JFrame仅添加一个类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30006260/

10-09 04:08