Somehow Managed to break my Paint() Method

I’ve been working on figuring out how to render maps, sprites and various other things to a JPanel and it’s been working great so-far but I just ran into a snag. For some reason, even though the code is running (I’ve done some println checks), nothing is being painted to the screen. If anyone can see where I messed up that’ll help.

I was thinking that the problem is with the while() loops but they’re working so that left me with checking the SpriteLoader and MapLoader classes but they’re working fine as well. I also checked that I was repainting the JPanel every time and I was so that wasn’t the problem. Now I’ve run out of ideas for what the problem could be. =/

Edit: There are no error messages when compiling either.

package Interface;

import Functions.KeyboardListener;
import Functions.MapLoader;
import Functions.SpriteLoader;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JPanel;

public class Game extends JPanel
{
    private KeyboardListener keyboard;
    private int spriteCounter = 0;
    private int loopCounter = 0;
    private boolean gamePaused = false, inventoryScreenPaused = true;
    
    public Game()
    {
        keyboard = new KeyboardListener();
        addKeyListener(keyboard);
        setFocusable(true);
        setDoubleBuffered(true);
    }
    
    public void paint(Graphics g)
    {
        //Check which screen to render.
        if(keyboard.getInventory() == true)
        {
            gamePaused = true;
            inventoryScreenPaused = false;
        }
        
        super.paint(g);
        Graphics2D graphics = (Graphics2D) g;

        RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        graphics.setRenderingHints(renderingHints);
        
        while(gamePaused == false)
        {
            
            int width = 10;
            int height = 10;

            //Render Map
            MapLoader mapLoader = new MapLoader(); //THIS SHOULD BE GIVEN TO THIS CLASS FROM ANOTHER CLASS SUCH AS THE GAME LOOP SO IT DOESN'T NEED A BAZILLION COPYS OF THE MAPLOADER CLASS.
            int map[][] = mapLoader.loadMap("TestMap", 0, 0);
            for(int row = 0; row < height; row++)
            {
                for(int column = 0; column < width; column++)
                {
                    if(map[column][row] == -1237980) //To change the movement speed just times all of the .getXpos and .getYpos parts by a speed multiplier
                    {
                        graphics.setColor(Color.RED);
                        graphics.fillRect(column*40 - keyboard.getXpos(), row*40 - keyboard.getYpos(), 40, 40);
                    }
                    else if(map[column][row] == -3584)
                    {
                        graphics.setColor(Color.YELLOW);
                        graphics.fillRect(column*40 - keyboard.getXpos(), row*40 - keyboard.getYpos(), 40, 40);
                    }
                    else if(map[column][row] == -16735512)
                    {
                        graphics.setColor(Color.BLUE);
                        graphics.fillRect(column*40 - keyboard.getXpos(), row*40 - keyboard.getYpos(), 40, 40);
                    }
                }
             }

            //Render Player
            SpriteLoader spriteLoader = new SpriteLoader();

            if (keyboard.getWalking() == false)
            {
                spriteCounter = 0;
            }
            else if(keyboard.getWalking() == true && spriteCounter == 0)
            {
                spriteCounter = 1;
            }

            int direction = keyboard.getDirection();
            if (spriteCounter == 0 && keyboard.getWalking() == false)
            {
                switch (direction)
                {
                    case 0: 
                        graphics.drawImage(spriteLoader.loadSprite("Left_Idle"), 628, 499, this);
                        break;
                    case 1:
                        graphics.drawImage(spriteLoader.loadSprite("Up_Idle"), 628, 499, this);
                        break;
                    case 2:
                        graphics.drawImage(spriteLoader.loadSprite("Right_Idle"), 628, 499, this);
                        break;
                    case 3:
                        graphics.drawImage(spriteLoader.loadSprite("Down_Idle"), 628, 499, this);
                        break;
                }
            }
            else if (spriteCounter == 1 && keyboard.getWalking() == true)
            {
                switch (direction)
                {
                    case 0: 
                        graphics.drawImage(spriteLoader.loadSprite("Left_Two"), 628, 499, this);
                        if(loopCounter >= 16)
                        {
                            spriteCounter++;
                            loopCounter = 0;
                        }
                        break;
                    case 1:
                        graphics.drawImage(spriteLoader.loadSprite("Up_One"), 628, 499, this);
                        if(loopCounter >= 16)
                        {
                            spriteCounter++;
                            loopCounter = 0;
                        }
                        break;
                    case 2:
                        graphics.drawImage(spriteLoader.loadSprite("Right_Two"), 628, 499, this);
                        if(loopCounter >= 16)
                        {
                            spriteCounter++;
                            loopCounter = 0;
                        }
                        break;
                    case 3:
                        graphics.drawImage(spriteLoader.loadSprite("Down_One"), 628, 499, this);
                        if(loopCounter >= 16)
                        {
                            spriteCounter++;
                            loopCounter = 0;
                        }
                        break;
                }
            }
            else if (spriteCounter == 2 && keyboard.getWalking() == true)
            {
                switch (direction)
                {
                    case 0: 
                        graphics.drawImage(spriteLoader.loadSprite("Left_Idle"), 628, 499, this);
                        if(loopCounter >= 16)
                        {
                            spriteCounter++;
                            loopCounter = 0;
                        }
                        break;
                    case 1:
                        graphics.drawImage(spriteLoader.loadSprite("Up_Idle"), 628, 499, this);
                        if(loopCounter >= 16)
                        {
                            spriteCounter++;
                            loopCounter = 0;
                        }
                        break;
                    case 2:
                        graphics.drawImage(spriteLoader.loadSprite("Right_Idle"), 628, 499, this);
                        if(loopCounter >= 16)
                        {
                            spriteCounter++;
                            loopCounter = 0;
                        }
                        break;
                    case 3:
                        graphics.drawImage(spriteLoader.loadSprite("Down_Idle"), 628, 499, this);
                        if(loopCounter >= 16)
                        {
                            spriteCounter++;
                            loopCounter = 0;
                        }
                        break;
                }
            }
            else if (spriteCounter == 3 && keyboard.getWalking() == true)
            {
                switch (direction)
                {
                    case 0: 
                        graphics.drawImage(spriteLoader.loadSprite("Left_One"), 628, 499, this);
                        if(loopCounter >= 16)
                        {
                            spriteCounter = 1;
                            loopCounter = 0;
                        }
                        break;
                    case 1:
                        graphics.drawImage(spriteLoader.loadSprite("Up_Two"), 628, 499, this);
                        if(loopCounter >= 16)
                        {
                            spriteCounter = 1;
                            loopCounter = 0;
                        }
                        break;
                    case 2:
                        graphics.drawImage(spriteLoader.loadSprite("Right_One"), 628, 499, this);
                        if(loopCounter >= 16)
                        {
                            spriteCounter = 1;
                            loopCounter = 0;
                        }
                        break;
                    case 3:
                        graphics.drawImage(spriteLoader.loadSprite("Down_Two"), 628, 499, this);
                        if(loopCounter >= 16)
                        {
                            spriteCounter = 1;
                            loopCounter = 0;
                        }
                        break;
                }
             }

            loopCounter++;
            repaint();
            
            //Cleanup
            graphics.dispose();
        }
        
        while(inventoryScreenPaused == false)
        {
            SpriteLoader spriteLoader = new SpriteLoader();
            graphics.drawImage(spriteLoader.loadImage("TestInventory_Main"), 628, 499, this);
            
            repaint();
        }
    }
}

Preeeeetty sure you’re not supposed to put your game loop in paint(), but it was a long time since I worked with Java2D. ^^ Paint() isn’t called automatically. You should use a BufferStrategy, AKA active rendering, instead.

EDIT:
And you should definitely load your images once and reuse them. Disk access != realtime.

I was thinking that earlier but I wasn’t sure how to take it out of the pain() method, thanks for the other two tips; I’ll go check them out now.

Edit: I couldn’t find anything about a way to keep the images loaded but I’m reading up on active rendering now.

I assume spriteLoader.loadSprite(“Left_Idle”) returns a BufferedImage, so just load them all in the beginning and reuse them later.


BufferedImage leftIdle = spriteLoader.loadSprite("Left_Idle");
BufferedImage rightIdle = spriteLoader.loadSprite("Right_Idle");
...


//When rendering:
graphics.drawImage(leftIdle, 628, 499, this);

Even better, I can see that direction is an int and 0 to 3 equals different directions:
0 = left
1 = up
2 = right
3 = down

A much cleaner way would be to simply dump them all into an array of BufferedImages (a BufferedImage[]). That way you can avoid the switch all together.

BufferedImage[] images = new BufferedImage[4];
images[0] = spriteLoader.loadSprite("Left_Idle");
images[1] = spriteLoader.loadSprite("Up_Idle");
images[2] = spriteLoader.loadSprite("Right_Idle");
images[3] = spriteLoader.loadSprite("Down_Idle");

Then when rendering, you can just call the following to render the sprite regardless of direction:


graphics.drawImage(images[direction], 628, 499, this);

Using that, you can get rid of all your switch-case expressions.

You can also draw your sprite with only a single graphics.drawImage() line if you use the same array trick for the animation, which would reduce the code between line 75 and 218 to just a few lines.

Wow, I just re-wrote the code and thanks to what you said it’s a hundred lines shorter and I found out why nothing was being rendered to the screen. The while() loops were somehow bugged so even though the code was running it wasn’t doing anything, everything else is working as intended. ^.^ I also just fixed up a few more lines related to loading the map so that it doesn’t keep doing a big operation to figure out what’s what and where it goes.

You really should use BufferStrategy instead though. It’ll give you better performance and you won’t have to worry about paint() methods in the first place. ^^

Here is a template from my Ludum Dare entry from 2012:
http://www.java-gaming.org/?action=pastebin&id=448

If you create your own [icode]public abstract class Screen[/icode] like this:


package org.matheusdev.screens;

import java.awt.Graphics2D;

public abstract class Screen {
	
	public abstract void tick();
	public abstract void render(Graphics2D g);

}

Then you only need to subclass that screen and feed it to your GameCanvas and GameFrame (I think…).

Hope this helps. The code should be cleaned up and pretty well written, but had no comments at all (of course… ludum dare, ya know ^^)

It even measures your FPS :smiley:

Just for fun, I’ve put the source on github