Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Active Rendering  (Read 1256 times)
0 Members and 1 Guest are viewing this topic.
Offline Javaisthefuture

Senior Newbie





« Posted 2007-11-22 17:39:35 »

Hi,
I recently made a game using the Repaint() method for my game animation, but then I read a chapter from Killer Game Programming about active rendering. I tried to implement this into my game code. It displays everything ok but my game no longer responds to my action listener. I have no idea what is wrong. There is no runtime or compilation errors.

Here is a snippet of my Active Rendering method.

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  
   public void mousePressed(MouseEvent event)
   {
      int mousex = event.getX();
      int mousey = event.getY();
     
      if(mousex >= 310 && mousex <= 470 && mousey >= 300 && mousey <= 335 && gameover && !credits && !loading)
      {
         gameover = false;
         firstlevel = true;
      }
      if(mousex >= 310 && mousex <= 470 && mousey >= 336 && mousey <= 370 && gameover && !credits && !loading)
      {
         credits = true;
      }
      if(mousex >= 200 && mousex <= 280 && mousey >= 225 && mousey <= 260 && gameover && credits && !loading)
      {
         credits = false;
      }
      if(mousex >= 150 && mousex <= 340 && mousey >= 250 && mousey <= 285 && !gameover && !credits && !loading && firstlevel && !firstlevelbegin)
      {
         firstlevel = false;
         firstlevelbegin = true;
      }
      if(mousex >= 200 && mousex <= 280 && mousey >= 300 && mousey <= 335 && !gameover && !credits && !loading && firstlevel && !firstlevelbegin)
      {
         firstlevel = false;
         gameover = true;
      }
   }
   public void mouseReleased(MouseEvent event)
   {
     
   }
   public void mouseClicked(MouseEvent event)
   {
     
   }
   public void mouseEntered(MouseEvent event)
   {
     
   }
   public void mouseExited(MouseEvent event)
   {
     
   }
   
   public void gameupdate()
   {
     
   }
   
   public void run()
   {
      long  starttime;
      while(running)
      {
         starttime=System.currentTimeMillis();
         gameupdate();
         gamerender();
         paintscreen();
         try
         {
            starttime += pausefor;
            Thread.sleep(Math.max(0, starttime-System.currentTimeMillis()));
         }
         catch(InterruptedException interruptedexception) { }
      }
   }
   
   public void gamerender()
   {
      if(dbImage == null)
      {
         dbImage = createImage(getSize().width, getSize().height);
         if(dbImage == null)
         {
            System.out.println("dbImage is null");
            return;
         }
         else
         dbg = dbImage.getGraphics();
         dbg.setColor(getBackground());
         dbg.fillRect(0, 0, getSize().width, getSize().height);
         
         if(loading)
         {
            loading(dbg);
         }
         if(gameover && !credits && !loading)
         {
            titlescreen(dbg);
         }
         if(gameover && credits && !loading)
         {
            credits(dbg);
         }
         if(!gameover && firstlevel && !loading && !credits && !firstlevelbegin)
         {
            firstlevel(dbg);
         }
         if(ispaused)
         {
            paused(dbg);
         }
      }
   }
   
   public void paintscreen()
   {
      Graphics g;
      try
      {
         g = this.getGraphics();
         if((g != null) && (dbImage != null))
         g.drawImage(dbImage, 0, 0, null);
         Toolkit.getDefaultToolkit().sync();
         g.dispose();
      }
      catch (Exception e)
      {
         System.out.println("Graphics context error: " + e);
      }
   }


Thanks.
Offline malberts

Junior Member





« Reply #1 - Posted 2007-11-22 20:14:37 »

The problem isn't in the snippet you gave here. Please post the entire class (it should be GPanel or something similar if you followed KGPJ)

Did you add the listeners onto the new GPanel class (the one made in KGPJ) or onto the parent JFrame or whatever?
Did you implement Runnable into the panel?
Do you mean action listener i.e. listeners on JButton's and stuff or the key and mouse listeners used in KGPJ?

After working through the 2D part of KGPJ a while I'm sure I'll be able to spot your problem if I see the rest of your code.

In space no-one can hear you System.out.println()
Offline Javaisthefuture

Senior Newbie





« Reply #2 - Posted 2007-11-22 21:18:59 »

Here is all of the code:

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  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
299  
300  
301  
302  
import java.applet.*;
import java.awt.*;
import java.applet.AudioClip;
import java.awt.geom.*;
import java.awt.event.*;

public class area51 extends Applet implements Runnable, MouseListener, FocusListener
{
   // Ints
  int myfps = 50;
   int pausefor;
   int onesecond = 1000;
   
   // Booleans
  private volatile boolean gameover = true;
   private volatile boolean running = true;
   private volatile boolean loading;
   private volatile boolean ispaused;
   private volatile boolean credits = false;
   private volatile boolean firstlevel = false;
   private volatile boolean firstlevelbegin = false;
   
   // Strings
 
   
   // Floats
  float size = 25f;
   
   // Fonts
  Font titlefont;
   
   // Arrays
 
   
   //Images
  private Image dbImage;
   private Graphics dbg;
   Image backgroundimage;
   Image blankbackground;
   Image firstlevelimage;

   // Sounds
  AudioClip xfiles;
   
   // Threads
  Thread runner;

   public void init()
   {
      pausefor = onesecond/myfps;
      xfiles = getAudioClip(getCodeBase(), "xfiles.mid");
      backgroundimage = getImage(getDocumentBase(), "area51.gif");
      blankbackground = getImage(getDocumentBase(), "blankbackground.gif");
      firstlevelimage = getImage(getDocumentBase(), "firstlevelimage.gif");
      MediaTracker mediatracker = new MediaTracker(this);
      mediatracker.addImage(backgroundimage, 0);
      mediatracker.addImage(blankbackground, 1);
      mediatracker.addImage(firstlevelimage, 2);
      try
      {
         loading = true;
         mediatracker.waitForID(0);
         mediatracker.waitForID(1);
         mediatracker.waitForID(2);
         loading = false;
      }
      catch(InterruptedException interruptedexception)
      {
         System.out.println("Download Error");
      }
   
      try
      {
         ClassLoader cl = this.getClass().getClassLoader();
         titlefont = Font.createFont(Font.TRUETYPE_FONT,
         cl.getResourceAsStream("Vera.ttf"));
         titlefont = titlefont.deriveFont(size);
      }
      catch(Exception e)
      {
         System.out.println(e);
      }
     
      addMouseListener(this);
      addFocusListener(this);
   }
   
   public void start()
   {
      setVisible(true);
      requestFocus();
      running = true;
      runner = new Thread(this);
      runner.start();
      xfiles.loop();
   }
   
   public void stopgame()
   {
      if(running != false)
      running = false;
   }
   
   public void destroy()
   {
      xfiles.stop();
   }
   
   public void focusGained(FocusEvent focusevent)
   {
      ispaused = false;
   }

   public void focusLost(FocusEvent focusevent)
   {
      ispaused = true;
   }
   
   public void mousePressed(MouseEvent event)
   {
      int mousex = event.getX();
      int mousey = event.getY();
     
      if(mousex >= 310 && mousex <= 470 && mousey >= 300 && mousey <= 335 && gameover && !credits && !loading)
      {
         gameover = false;
         firstlevel = true;
      }
      if(mousex >= 310 && mousex <= 470 && mousey >= 336 && mousey <= 370 && gameover && !credits && !loading)
      {
         credits = true;
      }
      if(mousex >= 200 && mousex <= 280 && mousey >= 225 && mousey <= 260 && gameover && credits && !loading)
      {
         credits = false;
      }
      if(mousex >= 150 && mousex <= 340 && mousey >= 250 && mousey <= 285 && !gameover && !credits && !loading && firstlevel && !firstlevelbegin)
      {
         firstlevel = false;
         firstlevelbegin = true;
      }
      if(mousex >= 200 && mousex <= 280 && mousey >= 300 && mousey <= 335 && !gameover && !credits && !loading && firstlevel && !firstlevelbegin)
      {
         firstlevel = false;
         gameover = true;
      }
   }
   public void mouseReleased(MouseEvent event)
   {
     
   }
   public void mouseClicked(MouseEvent event)
   {
     
   }
   public void mouseEntered(MouseEvent event)
   {
     
   }
   public void mouseExited(MouseEvent event)
   {
     
   }
   
   public void gameupdate()
   {
     
   }
   
   public void run()
   {
      long  starttime;
      while(running)
      {
         starttime=System.currentTimeMillis();
         gameupdate();
         gamerender();
         paintscreen();
         try
         {
            starttime += pausefor;
            Thread.sleep(Math.max(0, starttime-System.currentTimeMillis()));
         }
         catch(InterruptedException interruptedexception) { }
      }
   }
   
   public void gamerender()
   {
      if(dbImage == null)
      {
         dbImage = createImage(getSize().width, getSize().height);
         if(dbImage == null)
         {
            System.out.println("dbImage is null");
            return;
         }
         else
         dbg = dbImage.getGraphics();
         dbg.setColor(getBackground());
         dbg.fillRect(0, 0, getSize().width, getSize().height);
         
         if(loading)
         {
            loading(dbg);
         }
         if(gameover && !credits && !loading)
         {
            titlescreen(dbg);
         }
         if(gameover && credits && !loading)
         {
            credits(dbg);
         }
         if(!gameover && firstlevel && !loading && !credits && !firstlevelbegin)
         {
            firstlevel(dbg);
         }
         if(ispaused)
         {
            paused(dbg);
         }
      }
   }
   
   public void paintscreen()
   {
      Graphics g;
      try
      {
         g = this.getGraphics();
         if((g != null) && (dbImage != null))
         g.drawImage(dbImage, 0, 0, null);
         Toolkit.getDefaultToolkit().sync();
         g.dispose();
      }
      catch (Exception e)
      {
         System.out.println("Graphics context error: " + e);
      }
   }
   
   public void loading(Graphics g)
   {
      g.drawString("Loading...", 200, 200);
   }
   
   public void titlescreen(Graphics g)
   {
      g.drawImage(backgroundimage, 0, 0, this);
      g.setFont(titlefont);
      g.setColor(Color.green);
      g.drawString("Start Game", 320, 325);
      g.drawString("Credits", 320, 360);
      Graphics2D painter2D = (Graphics2D) g;
      Rectangle2D.Float box = new Rectangle2D.Float(310F, 300F, 160F, 35F);
      Rectangle2D.Float box1 = new Rectangle2D.Float(310F, 335F, 160F, 35F);
      painter2D.draw(box);
      painter2D.draw(box1);
   }
   
   public void firstlevel(Graphics g)
   {
      g.drawImage(blankbackground, 0, 0, this);
      g.setColor(Color.green);
      g.drawString("Your mission is to spy on Area 51.", 40, 70);
      g.drawString("To do this you will have to get", 40, 100);
      g.drawString("past the security and pretend you", 40, 130);
      g.drawString("are an engineer for the latest UFO!", 40, 160);
      g.drawString("Move with the arrow keys.", 80, 220);
      g.drawString("Start Mission! ", 160, 275);
      g.drawString("Back", 210, 325);
      Graphics2D painter2D = (Graphics2D) g;
      Rectangle2D.Float box = new Rectangle2D.Float(150F, 250F, 190F, 35F);
      Rectangle2D.Float box1 = new Rectangle2D.Float(200F, 300F, 80F, 35F);
      painter2D.draw(box);
      painter2D.draw(box1);

      if(firstlevelbegin && !firstlevel && !gameover && !loading && !credits)
      {
         g.drawImage(firstlevelimage, -250, -500, this);
      }
   }
   
   public void credits(Graphics g)
   {
      g.drawImage(blankbackground, 0, 0, this);
      g.setColor(Color.green);
      g.drawString("CREDITS", 190, 100);
      g.drawString("Ronan McCann-Jackson", 100, 150);
      g.drawString("Back", 210, 250);
      Graphics2D painter2D = (Graphics2D) g;
      Rectangle2D.Float box = new Rectangle2D.Float(200F, 225F, 80F, 35F);
      painter2D.draw(box);
   }
   
   public void paused(Graphics g)
   {
      g.setColor(Color.yellow);
      g.drawString("The game is paused!", 110, 20);
   }
}

Thanks.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline broumbroum

Junior Member





« Reply #3 - Posted 2007-11-23 02:13:05 »

does the start() method exits right after you called it or does it exit only after complete all Threading ops ? Swing and AWT can be blocked if your start() or loops do execute on the same thread.

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline malberts

Junior Member





« Reply #4 - Posted 2007-11-23 11:49:01 »

You had a bracket problem. In thhis part

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public void gamerender()
{
   if(dbImage == null)
   {
      dbImage = createImage(getSize().width, getSize().height);
      if(dbImage == null)
      {
         System.out.println("dbImage is null");
         return;
      }
      else
      dbg = dbImage.getGraphics();
      dbg.setColor(getBackground());
      dbg.fillRect(0, 0, getSize().width, getSize().height);
     
      .
      .
      .
   }
}


your code is only going to draw if dbImage  is null. The correction is:

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  
public void gamerender()
{
   if(dbImage == null)
   {
      dbImage = createImage(getSize().width, getSize().height);
      if(dbImage == null)
      {
         System.out.println("dbImage is null");
         return;
      }
      else
      dbg = dbImage.getGraphics();
   }
   dbg.setColor(getBackground());
   dbg.fillRect(0, 0, getSize().width, getSize().height);
   
   if(loading)
   {
      loading(dbg);
   }
   if(gameover && !credits && !loading)
   {
      titlescreen(dbg);
   }
   if(gameover && credits && !loading)
   {
      credits(dbg);
   }
   if(!gameover && firstlevel && !loading && !credits && !firstlevelbegin)
   {
      firstlevel(dbg);
   }
   if(ispaused)
   {
      paused(dbg);
   }
}

In space no-one can hear you System.out.println()
Offline Javaisthefuture

Senior Newbie





« Reply #5 - Posted 2007-11-23 16:34:59 »

Thanks!!! It works now. What are the advantages of using active rendering apart from full controll over the repainting of your app?
Thanks again!
Offline stef569

Junior Member





« Reply #6 - Posted 2007-11-24 00:26:22 »

It get's excecuted as you start the function, and not placed on a queu in a Swing Thread
Wich should give better performance.
Offline malberts

Junior Member





« Reply #7 - Posted 2007-11-24 07:25:42 »

The main difference is that it removes flickering by using an off-screen buffer (aka double buffering). When you use the normal paint() method for drawing then everytime you draw something it is drawn on the on-screen buffer. This means that everytime you draw the entire visible screen is redrawn. That is when you get flickering.

An off-screen buffer works by drawing on an image that's only in memory. Only when you have finished drawing everything is that image drawn onto the screen. Thus the visible screen is drawn to only once and there's no flicker.
(The KGPJ book explains this process with pictures if you want to see a visual representation of it)

In space no-one can hear you System.out.println()
Offline malberts

Junior Member





« Reply #8 - Posted 2007-11-24 07:40:01 »

The performance difference is not as a result of not being placed on a queue in a swing thread. The reason you get "faster drawing" is because you draw to the visible screen only once whereas in non-active rendering you would draw to visible screen after every item drawn.

Say for example you wanted to draw a stick man with a colored head and 5 lines. You would do something like
Set color
Fill head
Draw head
Draw line
Draw line
Draw line
Draw line
Draw line
= 8 instructions

Now in active rendering you would have the exact same code except 1 extra "draw to visible screen" instruction. This is because your drawing is done onto an Image that resides in memory. Since that Image is shown nowhere it need not be redrawn everytime you add something.

In non-active rendering there would be "draw to visible screen" after every instruction. Now when you get to drawing much more items its going to be slower. This happens because the Graphics object used in the paint method refers to the actual visible screen. So any change to the Graphics means a direct change to the screen and that can only be done by updating the whole Graphics to the screen and not just the last item drawn.

In space no-one can hear you System.out.println()
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.

BurntPizza (25 views)
2014-09-19 03:14:18

Dwinin (39 views)
2014-09-12 09:08:26

Norakomi (68 views)
2014-09-10 13:57:51

TehJavaDev (93 views)
2014-09-10 06:39:09

Tekkerue (47 views)
2014-09-09 02:24:56

mitcheeb (68 views)
2014-09-08 06:06:29

BurntPizza (51 views)
2014-09-07 01:13:42

Longarmx (38 views)
2014-09-07 01:12:14

Longarmx (44 views)
2014-09-07 01:11:22

Longarmx (40 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!