Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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 665 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1
« 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
« 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!
Legends of Yore - The Casual Retro Roguelike
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
« 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 Kernel


Medals: 106
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>

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

pw (26 views)
2014-07-24 01:59:36

Riven (25 views)
2014-07-23 21:16:32

Riven (19 views)
2014-07-23 21:07:15

Riven (22 views)
2014-07-23 20:56:16

ctomni231 (51 views)
2014-07-18 06:55:21

Zero Volt (46 views)
2014-07-17 23:47:54

danieldean (37 views)
2014-07-17 23:41:23

MustardPeter (40 views)
2014-07-16 23:30:00

Cero (56 views)
2014-07-16 00:42:17

Riven (55 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!