What am I doing?
A multiplayer 2D Videogame using sockets, finite state machine, jpanel, keylisteners, imageloader with hashtable and some other things.
The thing is that I want some gifs to render the player and enemies movements.
What have I tried?
How it looks right now
The white rectangle is suppose to be the player, also I can render the HUD with some messages from the server
Here is my code (I did not put all the code of the classes to avoid the fatigue of reading unnecessary things)
MyGraphics class (The player and enemies will extend this class)
import java.awt.Image;
import javax.swing.JComponent;
public class MyGraphics extends JComponent{
private static final long serialVersionUID = 1L;
protected int x1;
protected int y1;
protected int x2;
protected int y2;
protected int height;
protected int width;
protected Image image;
// Setters & getters are here
}
Player class (The user plays with it)
import java.awt.Graphics;
import java.util.ArrayList;
import Videogame.ImageLoader.ImageLoader;
import Videogame.Observer.Observer;
import Videogame.Observer.Subject;
import java.awt.Color;
public class Player extends MyGraphics implements Subject {
private static final long serialVersionUID = 1L;
private boolean isDead = false, falling = false, jumping = false, moving = false, standing = true;
private ArrayList<Observer> obs = new ArrayList<>();
public Player() {
// Configure player characteristics
setWidth(30);
setHeight(50);
setX1(20);
setY1(600);
image = ImageLoader.getInstance().getGif("playerIdle");
}
public static class SingletonHolder {
private static Player instance = new Player();
}
public static Player getInstance() {
return SingletonHolder.instance;
}
public void render(Graphics g) {
g.drawImage(image, x1, y1, width, height, null);
g.setColor(Color.white);
g.fillRect(x1, y1, width, height);
}
public void moveRight() {
changeImage("right");
this.setX1(this.getX1() + 3);
}
public void moveLeft() {
changeImage("left");
this.setX1(this.getX1() - 3);
}
public void moveUp(){
changeImage("up");
this.setY1(this.getY1() - 3);
}
public void moveDown(){
changeImage("down");
this.setY1(this.getY1() + 3);
}
public void changeImage(String movement) {
if (movement.equals("up")) {
image = ImageLoader.getInstance().getGif("playerIdle");
} else if (movement.equals("right")) {
image = ImageLoader.getInstance().getGif("playerWalkingRight");
} else if (movement.equals("left")) {
image = ImageLoader.getInstance().getGif("playerWalkingLeft");
} else if (movement.equals("down")) {
image = ImageLoader.getInstance().getGif("playerIdle");
}
}
}
ImageLoader (load and save images/gifs into hashtables)
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import java.awt.Image;
public class ImageLoader {
private Hashtable <String, BufferedImage> images;
private Hashtable <String, Image> gifs;
private ImageLoader(){
this.images = new Hashtable<String, BufferedImage>();
this.gifs = new Hashtable<String, Image>();
}
public static class SingletonHolder{
private static ImageLoader instance = new ImageLoader();
}
public static ImageLoader getInstance(){
return SingletonHolder.instance;
}
// Load images and gifs into their hashtables
public void load(){
try{
// Menu backgrounds
images.put("menu", ImageIO.read(ImageLoader.class.getResourceAsStream("../Resources/Backgrounds/menu.png")));
images.put("pause", ImageIO.read(ImageLoader.class.getResourceAsStream("../Resources/Backgrounds/pause.png")));
images.put("gameover", ImageIO.read(ImageLoader.class.getResourceAsStream("../Resources/Backgrounds/gameover.png")));
// In game backgrounds
images.put("fondo1", ImageIO.read(ImageLoader.class.getResourceAsStream("../Resources/Backgrounds/fondo1.png")));
images.put("fondo2", ImageIO.read(ImageLoader.class.getResourceAsStream("../Resources/Backgrounds/fondo2.png")));
// Player
gifs.put("playerIdle", new ImageIcon("../Resources/Gifs/Player/Player.gif").getImage());
gifs.put("playerWalkingRight", new ImageIcon("../Resources/Gifs/Player/WalkingRight.gif").getImage());
gifs.put("playerWalkingLeft", new ImageIcon("../Resources/Gifs/Player/WalkingLeft.gif").getImage());
// Boss
gifs.put("FlyingLeft", new ImageIcon("../Resources/Gifs/Boss/FlyingLeft.gif").getImage());
gifs.put("FlyingRight", new ImageIcon("../Resources/Gifs/Boss/FlyingRight.gif").getImage());
gifs.put("FlyingUp", new ImageIcon("../Resources/Gifs/Boss/FlyingUp.gif").getImage());
gifs.put("FlyingDown", new ImageIcon("../Resources/Gifs/Boss/FlyingDown.gif").getImage());
}catch(IOException e){
e.printStackTrace();
}
}
public Image getGif(String key){
System.out.println("Gif to load: " + key);
return gifs.get(key);
}
}
GamePanel (Has control over the animation)
public class GamePanel extends JPanel implements Runnable{
// Graphics
private Graphics dbg;
private Image dbImage = null;
public GamePanel(GameContext gameContext){
this.gameContext = gameContext;
setBackground(Color.white);
setPreferredSize(new Dimension(PanelWidth, PanelHeight));
setFocusable(true);
requestFocus();
addKeyListener(GameKeys.getInstance());
}
@Override
public void run() {
isRunning = true;
while(isRunning) {
this.gameUpdate();
this.gameRender();
this.paintScreen();
}
}
private void gameRender() {
if(dbImage == null){
dbImage = createImage(PanelWidth,PanelHeight);
if(dbImage == null) {
System.out.println("dbImage is null");
return;
}else {
dbg = dbImage.getGraphics();
}
}
gameContext.gameRender(dbg,PanelWidth,PanelHeight);
}
}
Play State (Has the logic of what to do when the state machine has current state of playing)
import Videogame.Figures.Player;
import Videogame.HUD.HUD;
import Videogame.ImageLoader.ImageLoader;
import java.awt.*;
public class PlayState extends InGameState{
@Override
public void gameUpdate(){
// Move level enemies, platforms, etc.
}
@Override
public void gameRender(Graphics g, int PanelWidth, int PanelHeight){
// Background
background = ImageLoader.getInstance().getImage("fondo1");
g.drawImage(background,0,0, PanelWidth, PanelHeight, null);
// HUD details
HUD.getInstance().render(g);
// Player movement
Player.getInstance().render(g);
}
@Override
public void play(){
// Nothing
}
@Override
public void pause(){
gameContext.setCurrentState(gameContext.getPauseState());
}
@Override
public void gameOver() {
gameContext.setCurrentState(gameContext.getGameOverState());
}
}
Hope someone can help me :)