Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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 and swing  (Read 2402 times)
0 Members and 1 Guest are viewing this topic.
Offline raft

Senior Newbie




aptalkarga.com


« Posted 2005-07-23 17:39:35 »

hi,

what is the correct way of active rendering in swing ? such an animation loop doesnt cause the component (a custom JPanel) to be repainted immediately ?
1  
2  
3  
4  
5  
while(running) {
    // game and render stuff here
    //..
    component.repaint(); // this wont make the component to be painted immediately, but later
}

repaint(0) wont help either since loop will continue running and repaint(0) requests will coalesce

if component.repaint() is replaced with
1  
component.paintImmediately(component.getBounds());


this also fails since animation loop doesnt run in AWT-Thread's context. If we run these in AWT-Thread then we block the AWT-Thread and cause AWT and Swing events not to be dispatched. i looked for a method to dispatch all waiting AWT events and return but couldnt find.

well, infact running this loop in another thread will succesfully and actively draw the component but that way we lack swing layout features. to be precise, i use the component at bottom of a JLayeredPane and there are other components on top of that. since we call component.paintImmediately() in another thread swing gets out of sync and those other components disappear or flicker

the only solution i can think of is to use:
1  
2  
3  
4  
5  
SwingUtilities.invokeAndWait(new Runnable() {
    public void run() {
        component.paintImmediately(component.getBounds());
    }
});

instead of component.repaint(). but this is error-prone and one can easily fall into deadlock trap (as i did Roll Eyes), and worse enough it is too expensive because of context switching

so what is the solution ?

thx
r a f t

Offline SluX

Junior Devvie





« Reply #1 - Posted 2005-07-24 12:14:15 »

In order to do active rendering u must get Graphics of desired component with getGraphics() method. I used to do this only with frames, but panels can do fine i think.
Then u use normal drawing procs and it will apear on the screan instantly(actively rendered).
Note that u should do double buffering.

"Intelligence is the most beautiful gift and the greatest temptation which one life can receive from the gods."Me Cheesy
Play strategic football
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #2 - Posted 2005-07-24 12:36:13 »

If you're using 1.4 or later, the BufferStratergy class lets you have manual control over a Canvas or Window's display surface, including when it gets swapped to screen. Easiest way to do active rendering.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline raft

Senior Newbie




aptalkarga.com


« Reply #3 - Posted 2005-07-26 00:51:37 »

In order to do active rendering u must get Graphics of desired component with getGraphics() method. I used to do this only with frames, but panels can do fine i think.
Then u use normal drawing procs and it will apear on the screan instantly(actively rendered).
Note that u should do double buffering.
sorry for late answer. some how forum engine didnt notify me Huh

if you do rendering over what getGraphics() return, then you loose swing layout features. compile and run the following sample. the JLabel over the custom graphics component always remains there, without flicker. then run it the other way with getGraphics, JLabel will dissapper or flicker

note this is a basic sample so calling paintImmediately either in AWT-Thread or not does not matter that much. in a more complicated gui, swing simply gets out of sync. that part shows up here etc

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  
import java.awt.*;
import javax.swing.*;

public class Active extends JPanel {
   
    public void run() {
        while (true) {
            //render(getGraphics()); // --- > uncomment this
            paintImmediately(getBounds()); // --> comment this
            try {
                Thread.sleep(10);
            } catch (Exception e) {}
        }
    }
    protected void paintComponent(Graphics g) {
        render(g);
    }
   
    int textPosition = 0;
    void render(Graphics g) {
        Dimension size = getSize();
       
        g.setColor(Color.ORANGE);
        g.fillRect(0, 0, size.width, size.height);
       
        textPosition += 3;
        textPosition = (textPosition > size.width) ? 0 : textPosition;
       
        g.setColor(Color.RED);
        g.drawString("moving string", textPosition, textPosition);
    }
   
    public static void main(String[] args) throws Exception {
        JFrame frame = new JFrame("active rendering test");
        JLayeredPane layeredPanel = new JLayeredPane();

        Active active = new Active();
        layeredPanel.add(active, JLayeredPane.DEFAULT_LAYER);
        active.setBounds(0, 0, 400, 400);
       
        JLabel label = new JLabel("a long long label");
        layeredPanel.add(label, JLayeredPane.DRAG_LAYER); // above active
        label.setBounds(0, 0, 200, 40);
       
        layeredPanel.setPreferredSize(new Dimension(400, 400));
        frame.add(layeredPanel);
        frame.pack();
        frame.setVisible(true);
       
        active.run();
    }
}


r a f t
have a look at my application at www.aptalkarga.com you will see a deep hierarchy of semi-transparent components (even a JLayeredPane in a JLayeredPane) where all depend on swing layout managers

edit: updated the link

Offline raft

Senior Newbie




aptalkarga.com


« Reply #4 - Posted 2005-07-26 01:00:31 »

If you're using 1.4 or later, the BufferStratergy class lets you have manual control over a Canvas or Window's display surface, including when it gets swapped to screen. Easiest way to do active rendering.
i use java5. i cant use Canvas since i use other swing components over my graphics component. if you mix awt and swing components in a JLayeredPane, awt components simply hide swing ones independent of their layers (heavy vs light-weigt)

similarly i cant use BufferStrategy since there seems no way (or i couldnt find yet) to get a BufferStrategy for a swing component, except top level containers. i dont want to draw to JFrame's surface but to a JPanel's as i need layering described above

r a f t

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.

trollwarrior1 (28 views)
2014-11-22 12:13:56

xFryIx (70 views)
2014-11-13 12:34:49

digdugdiggy (49 views)
2014-11-12 21:11:50

digdugdiggy (43 views)
2014-11-12 21:10:15

digdugdiggy (37 views)
2014-11-12 21:09:33

kovacsa (61 views)
2014-11-07 19:57:14

TehJavaDev (65 views)
2014-11-03 22:04:50

BurntPizza (63 views)
2014-11-03 18:54:52

moogie (79 views)
2014-11-03 06:22:04

CopyableCougar4 (78 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!