gpt4 book ai didi

java - 如何将 "Player image"添加到 Java 中?

转载 作者:行者123 更新时间:2023-12-02 03:44:21 24 4
gpt4 key购买 nike

对了,我正在开发一个新的 2d/自顶向下 Java 游戏。最近,我一直在自学如何用 Java 编写代码,但是我确实遇到了一个问题...我的窗口中有一个框,我可以使用 W、A、S 和 D 键来移动它。我想知道的是如何用图像替换那个黑框(然后担心动画)。这是我的代码(顺便说一句,这个代码也是游戏引擎)。这也是我试图添加的图像 Player image

如果有人能帮助我,我将不胜感激。谢谢。

Game.java

import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.*;

public class Game {

private final JFrame window = new JFrame();
private final ScreenFactory screenFactory;
private final GameThread gameThread;
private final KeyboardListener keyboardListener;
private final MousepadListener mousepadListener;

public Game(int windowX, int windowY, String title){
window.setSize(windowX, windowY);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setFocusable(true);
window.setLocationRelativeTo(null);
window.setTitle("2D Game");
window.setVisible(true);
screenFactory = new ScreenFactory(this);
gameThread = new GameThread(this);
keyboardListener = new KeyboardListener();
mousepadListener = new MousepadListener();


window.add(gameThread);
window.addKeyListener(keyboardListener);
window.addMouseListener(mousepadListener);

new Thread(gameThread).start();

}

public MousepadListener getMousepadListener(){
return mousepadListener;
}

public KeyboardListener getKeyboardListener(){
return keyboardListener;
}


public ScreenFactory getScreenFactory(){
return screenFactory;
}


public JFrame getWindow(){
return window;
}

}

GameThread.java

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JPanel;

public class GameThread extends JPanel implements Runnable{

private final Game game;

public GameThread(Game game){
this.game = game;
setFocusable(true);
}

public void run() {
while (true){
try {
if(game.getScreenFactory().getCurrentScreen() !=null)
game.getScreenFactory().getCurrentScreen().onUpdate();
Thread.sleep(10);
} catch (Exception e){
e.printStackTrace();
}
}
}

public void paint (Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if(game.getScreenFactory().getCurrentScreen() !=null)
game.getScreenFactory().getCurrentScreen() .onDraw(g2d);
repaint();
}

}

Screen.java

import java.awt.Graphics2D;

public abstract class Screen {

private final ScreenFactory screenFactory;

public Screen(ScreenFactory screenFactory ){
this.screenFactory = screenFactory;
}

public abstract void onCreate();

public abstract void onUpdate();

public abstract void onDraw(Graphics2D g2d);

public ScreenFactory getScreenFactory(){
return screenFactory;
}

}

ScreenFactory.java

public class ScreenFactory {

private final Game game;
private Screen screen;

public ScreenFactory(Game game){
this.game = game;
}

public void showScreen(Screen screen){
this.screen = screen;
this.screen.onCreate();
}

public Screen getCurrentScreen(){
return screen;
}

public Game getGame(){
return game;
}

}

键盘监听器

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class KeyboardListener implements KeyListener {

private boolean[]keys = new boolean[256];

@Override
public void keyPressed(KeyEvent event) {
keys[event.getKeyCode()] = true;
}

@Override
public void keyReleased(KeyEvent event) {
keys[event.getKeyCode()] = false;
}

@Override
public void keyTyped(KeyEvent event) {

}

public boolean isKeyPressed(int key){
return keys[key];
}

public boolean isKeyReleased(int key){
return !keys[key];
}

}

MousepadListener.java

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MousepadListener implements MouseListener{

private int mouseX, mouseY;
private boolean clicked;

@Override
public void mouseClicked(MouseEvent event) {
mouseX = event.getX();
mouseY = event.getY();
clicked = true;
}

@Override
public void mouseEntered(MouseEvent event) {

}

@Override
public void mouseExited(MouseEvent event) {

}

@Override
public void mousePressed(MouseEvent event) {
mouseClicked(event);
clicked = true;
}

@Override
public void mouseReleased(MouseEvent event) {
clicked = false;
}

public boolean isMousePressed(){
return clicked;
}

public int getX(){
return mouseX;
}

public int getY(){
return mouseY;
}

}

游戏

public class TheGame {

private Game game;

public TheGame(){
game = new Game(1280, 720, "2D Game");
game.getScreenFactory()
.showScreenMy(newScreen(game.getScreenFactory()));
}

public static void main(String[] args){
new TheGame();
}
}

我的屏幕

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;

public class MyScreen extends Screen{

private int x = 0, y = 0;

public MyScreen(ScreenFactory screenFactory){
super(screenFactory);
}

@Override
public void onCreate() {
System.out.println("Creating!...");
}

@Override
public void onUpdate() {
if (getScreenFactory()
.getGame().getKeyboardListener().isKeyPressed(KeyEvent.VK_A))
x -= 2;

if (getScreenFactory()
.getGame().getKeyboardListener().isKeyPressed(KeyEvent.VK_D))
x += 2;

if (getScreenFactory()
.getGame().getKeyboardListener().isKeyPressed(KeyEvent.VK_W))
y -= 2;

if (getScreenFactory()
.getGame().getKeyboardListener().isKeyPressed(KeyEvent.VK_S))
y += 2;

if (y >= 720 - 72)
y = 720 - 72;
if (y <= 0)
y = 0;
if (x >= 1280 - 72)
x = 1280 - 72;
if (x <= 0)
x = 0;

}

@Override
public void onDraw(Graphics2D g2d) {
g2d.setColor(Color.black);
g2d.fillRect(x, y, 72, 72);
}
}

最佳答案

首先将您的图像读取为 BufferedImage。然后通过drawImage方法进行绘制。试试这个:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class MyScreen extends Screen {

private int x = 0, y = 0;

private BufferedImage image;

public MyScreen(ScreenFactory screenFactory) {
super(screenFactory);

try {
image = ImageIO.read(getClass().getResource("imagepath"));
} catch (IOException e) {
// TODO: handle exception
}

}

@Override
public void onCreate() {
System.out.println("Creating!...");
}

@Override
public void onUpdate() {
if (getScreenFactory().getGame().getKeyboardListener().isKeyPressed(KeyEvent.VK_A))
x -= 2;

if (getScreenFactory().getGame().getKeyboardListener().isKeyPressed(KeyEvent.VK_D))
x += 2;

if (getScreenFactory().getGame().getKeyboardListener().isKeyPressed(KeyEvent.VK_W))
y -= 2;

if (getScreenFactory().getGame().getKeyboardListener().isKeyPressed(KeyEvent.VK_S))
y += 2;

if (y >= 720 - 72)
y = 720 - 72;
if (y <= 0)
y = 0;
if (x >= 1280 - 72)
x = 1280 - 72;
if (x <= 0)
x = 0;

}

@Override
public void onDraw(Graphics2D g2d) {
g2d.setColor(Color.black);
g2d.fillRect(x, y, 72, 72);
g2d.drawImage(image, x, y, null);
}
}

关于java - 如何将 "Player image"添加到 Java 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36461641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com