Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Somehow Managed to break my Paint() Method  (Read 342 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1

Senior Member


Medals: 3
Projects: 1



« Posted 2013-03-02 15:57:04 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
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();
        }
    }
}
Offline theagentd
« Reply #1 - Posted 2013-03-02 16:37:21 »

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.

Myomyomyo.
Offline tyeeeee1

Senior Member


Medals: 3
Projects: 1



« Reply #2 - Posted 2013-03-02 16:42:06 »

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.
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline theagentd
« Reply #3 - Posted 2013-03-02 18:20:29 »

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

1  
2  
3  
4  
5  
6  
7  
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.

1  
2  
3  
4  
5  
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:
1  
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.

Myomyomyo.
Offline tyeeeee1

Senior Member


Medals: 3
Projects: 1



« Reply #4 - Posted 2013-03-02 19:28:51 »

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.
Offline theagentd
« Reply #5 - Posted 2013-03-02 19:41:38 »

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. ^^

Myomyomyo.
Offline matheus23

JGO Wizard


Medals: 71
Projects: 3


You think about my Avatar right now!


« Reply #6 - Posted 2013-03-02 20:17:49 »

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
public abstract class Screen
like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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 ^^)

<edit>It even measures your FPS Cheesy</edit>

<edit number="2">Just for fun, I've put the source on github</edit>

Take a look at my development Blog: http://matheusdev.tumblr.com
Also look at my RPG Ruins of Revenge
Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Try the Free Demo of Revenge of the Titans

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (31 views)
2013-05-17 21:29:12

alaslipknot (40 views)
2013-05-16 21:24:48

gouessej (70 views)
2013-05-16 00:53:38

gouessej (69 views)
2013-05-16 00:17:58

theagentd (78 views)
2013-05-15 15:01:13

theagentd (72 views)
2013-05-15 15:00:54

StreetDoggy (113 views)
2013-05-14 15:56:26

kutucuk (136 views)
2013-05-12 17:10:36

kutucuk (136 views)
2013-05-12 15:36:09

UnluckyDevil (145 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.702 seconds with 21 queries.