Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  Using BufferStrategy with Applet  (Read 2542 times)
0 Members and 1 Guest are viewing this topic.
Offline gamehacer2000

Senior Newbie




Me? I'm no one; yet, i'm somebody.


« Posted 2004-06-20 00:39:43 »

I do not know whether anyone has posted a way to use BufferStrategy with an Applet or a JApplet. However,
I have recently created a class called AppletGameCore which acts as a facility to easily create games with Applet. I apologize for the lack of detailed comments.
Here is the
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  
import java.awt.Canvas;
import java.awt.Graphics2D;
import java.awt.Dimension;
import java.awt.image.BufferStrategy;
import java.applet.Applet;

/**
 *AppletGameCore.java
 *@author David Graham[font=Verdana][/font]
 */

 
public abstract class AppletGameCore extends Applet implements Runnable
{
      private BufferStrategy bufferStrategy;
      private Canvas drawArea;/*Drawing Canvas*/
      private boolean stopped = false;/*True if the applet has been destroyed*/
      private int x = 0;
     
      public void init()
      {
            Thread t = new Thread(this);
            drawArea = new Canvas();
            setIgnoreRepaint(true);
            t.start();
      }
     
      public void destroy()
      {
            stopped = true;
           
            /*Allow Applet to destroy any resources used by this applet*/
            super.destroy();
      }
     
      public void update()
      {
            if(!bufferStrategy.contentsLost())
            {
                  //Show bufferStrategy
                 bufferStrategy.show();
            }
      }
     
      //Return drawArea's BufferStrategy
     public BufferStrategy getBufferStrategy()
      {
            return bufferStrategy;
      }
     
      //Create drawArea's BufferStrategies
     public void createBufferStrategy(int numBuffers)
      {
            drawArea.createBufferStrategy(numBuffers);
      }
     
      //Subclasses should override this method to do any drawing
     public abstract void draw(Graphics2D g);
     
      public void update(Graphics2D g)
      {
            g.setColor(g.getBackground());
            g.fillRect(0,0,getWidth(),getHeight());
      }
     
      //Update any sprites, images, or primitives
     public abstract void update(long time);
     
      public Graphics2D getGraphics()
      {
            return (Graphics2D)bufferStrategy.getDrawGraphics();
      }
     
      //Do not override this method      
     public void run()
      {
            drawArea.setSize(new Dimension(getWidth(),getHeight()));
            add(drawArea);
            createBufferStrategy(2);
            bufferStrategy = drawArea.getBufferStrategy();
           
            long startTime = System.currentTimeMillis();
            long currTime = startTime;
           
            //animation loop
           while(!stopped)
            {
                  //Get time past
                 long elapsedTime = System.currentTimeMillis()-currTime;
                  currTime += elapsedTime;
                 
                  //Flip or show the back buffer
                 update();
                 
                  //Update any sprites or other graphical objects
                 update(elapsedTime);
                 
                  //Handle Drawing
                 Graphics2D g = getGraphics();
                  update(g);
                  draw(g);
                 
                  //Dispose of graphics context
                 g.dispose();
            }

      }
}


Feel free to modify and use this code for your own purposes.
If you have any questions or comments, please post them.  Grin

Note: This code is only compatible with J2SE 1.4 and up.

Quote
True knowledge is admitting what you don't no.
Confucius I think...
Offline baylor

Innocent Bystander




Java games rock!


« Reply #1 - Posted 2004-09-13 04:09:10 »

David, first i wanted to say thanks. It sounds awful, but i spent 6 hours today looking for working code that shows an applet using BufferStrategy. i was begining to think it was impossible. So i was happy when i found this post

Now here's the problem - i don't exactly understand the code and i don't know how to use it. And it won't compile for me. Am i doing something wrong?

My biggest two questions:

1. How do things get drawn?
In normal applets, you call one of the repaint() methods which calls update() which clears the invalidated region then calls paint()
 repaint()->update()->paint()
But AppletGameCore doesn't have a repaint() or paint() method (you can not have a paint method?). And it has two update methods. It took me a while to realize that update(Graphics2D g) never gets called by the system. Because of the name i thought it was like update(), but it isn't, it just has the same name  Huh

So my best guess here is that you've completely disabled Java's drawing/painting and have chosen to handle it entirely manually. Is that correct?

If so, out of curiosity, why is the paint routine named draw() rather than paint()? Would it have confused the system if it had been called paint()?

2. Does the code compile?
My compiler (Sun's J2SE 1.4.2) doesn't like the method
 public Graphics2D getGraphics()
That's because java.awt.Component already has a method called getGraphics() but it returns a Graphics rather than a Graphics2D. And since you can't have two methods that are identical except for return type...

i really, really want this code to work, but i've been screwing with it for a while and i can't figure out how to do it. Help?

-baylor
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #2 - Posted 2004-09-13 20:50:02 »

Quote

So my best guess here is that you've completely disabled Java's drawing/painting and have chosen to handle it entirely manually. Is that correct?


That is always the case when doing "active" painting with a BufferStrategy

Quote

If so, out of curiosity, why is the paint routine named draw() rather than paint()? Would it have confused the system if it had been called paint()?


You don't usually want the two painting strategies (sytem triggered update-paint vs. active rendering) to be fighting over the same space.   Specially when you have active drawing in a separate thread from the AWT event thread where "normal" painting occurs..

Quote

2. Does the code compile?
My compiler (Sun's J2SE 1.4.2) doesn't like the method
 public Graphics2D getGraphics()
That's because java.awt.Component already has a method called getGraphics() but it returns a Graphics rather than a Graphics2D. And since you can't have two methods that are identical except for return type...


I have not tried the code, but perhaps you can just change the return type to Graphics and be done with it?

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

Senior Newbie




Me? I'm no one; yet, i'm somebody.


« Reply #3 - Posted 2004-09-25 23:08:21 »

First off, sorry for not replying. Been off for a while.
Thanks swpalmer for replying to baylor. For the most part your response was correct. Since you gave an good reply to baylor, I don't have to explain too much. The reason the getGraphics() method is problematic lies in the fact that you should not combine "active" and "passive" rendering. If you use paint and repaint, the methods request the getGraphics() of a component. To ensure that no calls request the standard getGraphics() method you should ignore repaint messages and stay away from overriding paint. Hope this is useful.  Wink
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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (44 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (62 views)
2014-09-21 02:42:18

BurntPizza (32 views)
2014-09-21 01:30:30

moogie (39 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
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!