Java 2D Game Programming Episode 7 - Font and Stuff

preview_player
Показать описание
So... this video was something else. Basically, this video shows you how to make a font class based on a .png image file

Рекомендации по теме
Комментарии
Автор

I don't know why but when I follow along, I ended with an error that says Exception in thread "GameThread" in 5 different spots in the code. 6 if you in include Is there any way you could help one on one?

vater
Автор

It doesnt work with any numbers or sumbols?
Whats the reason?

crew
Автор

You could do not remove "final" from BufferedImage because it will never be changed.
To avoid the error you could remove " = null " and initialize the BufferedImage in the constructor.

crew
Автор

To everybody who also dont know why there dont space in the text u must do
x += xOffset;
y+ = yOffset;
out of
for(int i = 0; i < word.length(); i++) {
if(word.charAt(i) != 32) {
g.drawImage(f.getFont(word.charAt(i)), (int) x, (int) y, width, height, null);


}

Have a good Day :)

narukxge-
Автор

Muito obrigado! O melhor professor! Estou aprendendo muito!

EduardoLima-llyt
Автор

i know this is a bit late lol but loving the series, Exception in thread "GameThread" x lies outside the raster, i know its due to it being bigger than the img but cant find where its going wrong?

olivermetz
Автор

How do space work for you? I had to explisit say that if charAt == 32, then x = 2; y= 7; But you have nothing, and it still works.

Donslayer
Автор

how can ı make my own letters ?is there any idea that which program can be usefull for me?

serkanefe
Автор

i get error : Exception in thread "GameThread" / by zero
at wLetter = FONTSHEET.getWidth() / w;
hLetter = FONTSHEET.getHeight() / h;

lapnghiepshopee
Автор

Exception in thread "Game Thread" java.lang .nullPointerException occurs even after making res as source

amanthapa
Автор

What's the benefit of doing it this way over using a font file?

beonoc
Автор

Hello, you figured out a way to do this with symbols as well?

zdenduk
Автор

the letters are still in a little blob for me. any ideas?

Therman
Автор

Just right click on the variable, refactor, rename, it will fix everything instead of making you do everything individually.

thedemonicstaff
Автор

I like your videos. Can you send me zelda font please?)

Rock-off
Автор

Whats wrong whit this code?

Playstate.java
package states;
import util.KeyHandler;




import util.MouseHandler;
import util.Vector2f;

import java.awt.Color;
import java.awt.Graphics2D;

import graphics.Font;
import graphics.sprite;


public class PlayState extends GameState {
private Font font;

public PlayState(GameStateManager gam) {
super(gam);
font = new Font("res/good.png", 16, 16);
}
public void update(){

}

public void input(MouseHandler mouse, KeyHandler key) {



}

public void render(Graphics2D g) {
sprite.drawArray(g, font, "CZEMU", new Vector2f(100, 100), 32, 32, 16, 0);

}





}

Font.java

package graphics;




import java.awt.image.BufferedImage;
import graphics.sprite;
import javax.imageio.ImageIO;

public class Font {

private final BufferedImage FONTSHEET;
private BufferedImage[][] spriteArray;
private final int TILE_SIZE = 32;
public int w;
public int h;
private int wLetter;
private int hLetter;

public Font(String file) {
w = TILE_SIZE;
h = TILE_SIZE;

" + file + "...");
FONTSHEET = loadFont(file);

wLetter = FONTSHEET.getWidth() / w;
hLetter = FONTSHEET.getHeight() / h;
loadSpriteArray();

}

public Font(String file, int w, int h) {
this.w = w;
this.h = h;
" + file + "...");
FONTSHEET = loadFont(file);

wLetter = FONTSHEET.getWidth() / w;
hLetter = FONTSHEET.getHeight() / h;
loadSpriteArray();


}

public void setSize(int width, int height) {
setWidth(width);
setHeight(height);



}

public void setWidth(int i) {
w = i;
wLetter = FONTSHEET.getWidth() / w;
}
public void setHeight(int i) {
h = i;
hLetter = FONTSHEET.getHeight() / h;

}
public int getWidth() {return w;}
public int getHeight() {return h;}

private BufferedImage loadFont(String file) {
BufferedImage sprite = null;
try {
sprite =
} catch(Exception e) {
System.out.println("ERROR: nie można załadować sprite " + file);
}

return sprite;

}

public void loadSpriteArray() {
spriteArray = new

for(int x = 0; x < wLetter; x++) {
for(int y = 0; y < hLetter; y++) {
spriteArray[x][y] = getLetter(x, y);

}

}
}


public BufferedImage getFONTSHEET() {return FONTSHEET;}

public BufferedImage getLetter(int x, int y) {
return FONTSHEET.getSubimage(x * w, y * h, w, h);
}

public BufferedImage getFont(char letter) {
int value = letter - 65;

int x = value % wLetter;
int y = value / hLetter;

return getLetter(x, y);
}

}

sprite.java

package graphics;




import java.awt.image.BufferedImage;
import graphics.sprite;
import javax.imageio.ImageIO;

public class Font {

private final BufferedImage FONTSHEET;
private BufferedImage[][] spriteArray;
private final int TILE_SIZE = 32;
public int w;
public int h;
private int wLetter;
private int hLetter;

public Font(String file) {
w = TILE_SIZE;
h = TILE_SIZE;

" + file + "...");
FONTSHEET = loadFont(file);

wLetter = FONTSHEET.getWidth() / w;
hLetter = FONTSHEET.getHeight() / h;
loadSpriteArray();

}

public Font(String file, int w, int h) {
this.w = w;
this.h = h;
" + file + "...");
FONTSHEET = loadFont(file);

wLetter = FONTSHEET.getWidth() / w;
hLetter = FONTSHEET.getHeight() / h;
loadSpriteArray();


}

public void setSize(int width, int height) {
setWidth(width);
setHeight(height);



}

public void setWidth(int i) {
w = i;
wLetter = FONTSHEET.getWidth() / w;
}
public void setHeight(int i) {
h = i;
hLetter = FONTSHEET.getHeight() / h;

}
public int getWidth() {return w;}
public int getHeight() {return h;}

private BufferedImage loadFont(String file) {
BufferedImage sprite = null;
try {
sprite =
} catch(Exception e) {
System.out.println("ERROR: nie można załadować sprite " + file);
}

return sprite;

}

public void loadSpriteArray() {
spriteArray = new

for(int x = 0; x < wLetter; x++) {
for(int y = 0; y < hLetter; y++) {
spriteArray[x][y] = getLetter(x, y);

}

}
}


public BufferedImage getFONTSHEET() {return FONTSHEET;}

public BufferedImage getLetter(int x, int y) {
return FONTSHEET.getSubimage(x * w, y * h, w, h);
}

public BufferedImage getFont(char letter) {
int value = letter - 65;

int x = value % wLetter;
int y = value / hLetter;

return getLetter(x, y);
}

}

pls HELP

Hhwhjsjuw