julian
julian

Reputation: 4714

Java - write text on the screen

I am making a game with Java and LWJGL..
Basically I want to write my game's fps on the screen, but I really don't understand how i should do that.

I am thinking its something related to the Graphics class, am i right?

Graphics g = new Graphics();
g.setColor(Color.YELLOW);
g.setFont(new Font("Impact", Font.PLAIN, 20));
g.drawString(fps + " FPS", 20, 30);
g.dispose();

This code returns me an error..it says that the constructor of the graphics class isn't implemented right.

Any ideas?

Upvotes: 0

Views: 2908

Answers (2)

Pavlo K.
Pavlo K.

Reputation: 371

You can't just create new Graphics instance from scratch and hope it will draw what you want and where you want.

Graphics contexts are obtained from other graphics contexts or are created by calling getGraphics on a component.

If you want to put sime text on the screen with LWJGL then search exact and you will find a lot of solutions. Even on youtube.

Upvotes: 0

BlackBox
BlackBox

Reputation: 2233

To draw, you need the draw handle for the original Graphics object and not by creating a new one yourself.

As far as I can tell, for a basic game, your controlling class should be extending BasicGame which provides a render method, from which you can grab a Graphics object.

See: BasicGame and render

I imagine what you need to do is override that method, like this:

public void render(GameContainer container, Graphics g) throws SlickException {
    //grab graphics g here and use it.
}

For a basic template, look here.

Upvotes: 2

Related Questions