Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
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 AND Passive Rendering? JPanel  (Read 2617 times)
0 Members and 1 Guest are viewing this topic.
Offline Kappers

Junior Newbie




Affective Computing MIT <3


« Posted 2011-09-03 08:40:15 »

[size=10pt]Greetings all! [/size] Cool

At the moment I have created a physics engine which I am using to produce both a game and simulation. For my simulation, I need to use quite a lot of swing, and am having to created the program using an applet rather than straight JFrame etc.

The program itself is a JPanel and I'm merely opening it through a separate class which extends applet, I'm painting by overriding the paintComponent method and calling it through repaint in actionPerformed (using a swing timer obviously). However, the issue is, even after altering freedom of the components contained by the JPanel program to repaint, and after implementing a basic buffer using BufferedImage alone, there is lots of typical applet flashing when the timer delay is low (frame rate is high)! All my painting is being done in paintComponent as well, therefore I'm not using any form of rendering/game loop that seems modern formality in Java animation that doesn't use swing!

I wondered if there is a possible approach I can take to this using both active and passive rendering, and the most important bit, complies with the fact my main program body is a JPanel! I've endlessly struggle to employ BufferStrategies etc, but keep returning to what I thought would mean having to rewrite my program heavily as a JFrame.


This is the way in which I am opening the program's JPanel body,

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  
public class Simulation extends JApplet
{

    public static void main(String []args)
    {

        EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {

                    JFrame window = new JFrame();
                    window.setDefaultCloseOperation(3);
                    window.setTitle("Kinetic Theory Simulator");

                    JApplet applet = new Simulation();
                    applet.init();
                    window.getContentPane().add(applet);
                    window.pack();
                    window.setVisible(true);
                }
            }
        );

    }  

    public void init()
    {  
        JPanel panel = new KineticSimulation();
        getContentPane().add(panel);
    }

}


1  
class KineticSimulation  extends JPanel implements MouseListener, MouseMotionListener, KeyListener, ActionListener



This is the relevant painting code,

( this initial code snippet is in the KineticSimulation() constructor)
1  
2  
3  
4  
        _delay = 1000 / _initFrameRate;
        timer = new Timer(_delay, this);
        timer.setCoalesce(true);
        timer.start();


1  
2  
3  
4  
5  
6  
7  
    public void actionPerformed(ActionEvent e) 
    {
        collisionTest();
        reCalculateAll();
        updateSimulationSpeed();
        repaint();
    }


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
    public void paintComponent(Graphics g) 
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;

        g2 = (Graphics2D)buffer.getGraphics();

          //this is where all the painting code is

        getGraphics().drawImage(buffer, 0, 0, null);

    }


If I haven't made myself clear feel free to ask me to clarify,

Thanks in advance!  Smiley



Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-09-03 09:17:50 »

1. You shouldn't use getGraphics().drawImage, you should just use the Graphics object given to you as a parameter.
2. JPanels are automatically double buffered so you can just draw directly to the Graphics object they give you.
3. Could you give us an example (an applet?) where it flashes?

Offline Kappers

Junior Newbie




Affective Computing MIT <3


« Reply #2 - Posted 2011-09-03 09:22:32 »

http://www.realapplets.com/tutorial/DoubleBuffering.html

The 3 boxes depict it well, although due to the black background they seem slightly harmless as the flashes themselves are black.


So are you suggesting I just forget about double buffering? :/ And see what happens if I let it automatically deal with it?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2011-09-03 21:40:21 »

First one flickers badly, second flickers sometimes, and the third one doesn't flicker at all.

Swing automatically double buffers using BufferStrategy so when drawing on a JComponent or any of its subclasses, you shouldn't worry about double buffering. It's when you start drawing on any AWT Component (which is recommended for games since you are skipping Swing's bloat), that's when you should start double buffering.

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.

atombrot (20 views)
2014-08-19 09:29:53

Tekkerue (21 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (55 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (28 views)
2014-08-08 02:01:56

Norakomi (35 views)
2014-08-06 19:49:38

BurntPizza (64 views)
2014-08-03 02:57:17
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!