Java2D drawString?

Hi

I’m in the early development of a game and I ran into a problem that has never occurred before. When I call the Graphics2D#drawString() method, the game stops - the game loop that is. There are no errors printed in the console, the string will be displayed on the screen, but the game loop doesn’t continue.

I’m just wondering if anyone has encountered this before? There’s no mention of anything like this happening in the docs and Google doesn’t come up with anything.

Other methods work, such as fillRect, composites, color, etc. It’s just this one problem.

	
        @Override
	public void render(UnitedTD game, Screen screen) {
		//screen.applyOpacity(alpha); //work
		//screen.setColor(Color.RED); //work
		//screen.setFont(new Font("Arial", 0, 70)); //work
		//screen.fillRect(150, 150, 150, 150); //work
                //screen.drawString("Hello World", 150, 150); // doesn't work
		//screen.getGraphics2DObjectFromBufferedImage().drawString("test", 1, 1); //also doesn't work
	}

I’ve tried switching stuff around, but it keeps stopping at drawString. Thanks in advance!

Maybe a font problem?
Can you check how many fonts you have available?

String fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for ( int i = 0; i < fonts.length; i++ )
{
  System.out.println(fonts[i]);
}

What is the code for the screen class drawString method?

you are hiding too much code here. We need to know what screen is and how you are rendering. Active? Passive? Any tricks?

It calls the Graphics draw string method.

    public void drawString(String str, float x, float y) {
        g.drawString(str, x, y);
    }

The screen class holds the BufferedImage, the Graphics2D object, and other stuff to act as a camera, but I may just separate it, so there will just be a Graphics2D and Camera parameters in the render methods. However, methods like applyOpacity, enableAntialiasing, are quire useful.

The game loop looks like:

		while(running) {
			// work out how long its been since the last update, this
			// will be used to calculate how far the entities should
			// move this loop
			long now = System.nanoTime();
			long updateLength = now - lastLoopTime;
			lastLoopTime = now;
			double delta = updateLength / ((double) OPTIMAL_TIME);

			// update the frame counter
			lastFpsTime += updateLength;
			fps++;
			ups++;

			// update our FPS counter if a second has passed since
			// we last recorded
			if (lastFpsTime >= 1000000000) {
				System.out.println("(FPS: " + fps + ") : " + "(UPS: " + ups + ")");
				lastFpsTime = 0;
				fps = ups = 0;
			}

			// update the game logic
			states[currentState].update(this, delta);

			// being game drawing
			screen.startDrawing();
			states[currentState].render(this, screen);
			//screen.update(this);
			screen.finishDrawing();
			
			// display graphics to screen.
			getGraphics().drawImage(screen.getImage(), 0, 0, getWidth(), getHeight(), this);
			try {
				Thread.sleep((lastLoopTime - System.nanoTime() + OPTIMAL_TIME) / 1000000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

The beginDrawing method just creates a Graphics object from the image and fills it black, while the finishDrawing method disposes it.

EDIT: Ok, so I found out why it stops, it stops during the first loop when sleeping since the duration becomes negative when calling drawString the first time, but then on the next loops, all the numbers are positive. My quick fix was just to check if the time to sleep should is less than 0.

I’m still unsure why this happens though :confused: