Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (492)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
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  
  Mixing Swing/java2D and OpenGL  (Read 1040 times)
0 Members and 1 Guest are viewing this topic.
Offline Luke

Senior Newbie




I love YaBB 1G - SP1!


« Posted 2004-07-29 17:44:15 »

I have been thinking about porting my game,  http://freerails.sourceforge.net/, to OpenGL directly rather than use Swing, since people keep saying its too slow:(  However, this seems like a lot of work and there are lots of things that Swing makes easy that would take a long time to get right if I were doing it myself.  So I have been looking at ways to mix Swing and OpenGL.

So far I have had a quick look at JOGL, but the most promising approach would seem to be using agile2D.  

What I have done so far is as follows:

I have added a method:


1  
GLFunc getGL(){

to the class
1  
 agile2d.opengl


This lets me mix java2D and direct use of OpenGL in JComponents.  For example
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  
import gl4java.GLEnum;
import gl4java.GLFunc;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

import agile2d.opengl.AGLGraphics2D;

public class TestJPanel extends JPanel implements GLEnum {
     
      protected void paintComponent(Graphics g) {

            super.paintComponent(g);
            g.setColor(Color.RED);
            g.drawString("This text was drawn by calling g.drawString(.) before the GL triangle.", 20, 20);

            GLFunc gl = ((AGLGraphics2D) g).getGL();

            gl.glBegin(GL_TRIANGLES);
            gl.glColor3f(0.0f, 0.0f, 1.0f); //blue
           gl.glVertex3i(20, 0, 0);
            gl.glVertex3i(0, 100, 0);      
            gl.glVertex3i(100, 100, 0);
            gl.glEnd();
           
            g.setColor(Color.WHITE);
            g.drawString("This text was drawn by calling g.drawString(.) after the GL triangle.", 20, 60);

      }
}


and

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  
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLayeredPane;

import agile2d.AgileJFrame;


public class TestFrame {

      public static void main(String[] args) {
             JFrame frame = new AgileJFrame();
             frame.setSize(400, 400);
     
             frame.getContentPane().add(new TestJPanel());
                         
             //Add an JInternalFrame - shows light weight components can be drawn on top
            //components rendered using direct OpenGL.            
            JInternalFrame jInternalFrame = new JInternalFrame("JInternalFrame");
             jInternalFrame.setSize(200, 200);
             frame.getLayeredPane().add(jInternalFrame,
                  JLayeredPane.MODAL_LAYER);              
             jInternalFrame.setVisible(true);
                                     
             frame.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                System.exit(0);
            }
        });
                         
            frame.show();                                    
      }            
}


Which produces the following...




The advantages of this approach seem to be:
(1)   Java2d calls and direct openGL calls can be freely mixed.
(2)   I don't need to worry about light and heavyweight components.
(3)   rendering of the swing components is h/w accelerated.


My questions
(1)  Does this sound sensible?  Has anyone else tried it?
(2)  How does the performance of agile2D compare with the _current_ performance of standard java2D?
(3)  Is the any benefit in trying to port AGLGraphics2D and other relevant classes to use JOGL instead of gl4java?

Luke
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.

Nickropheliac (16 views)
2014-08-31 22:59:12

TehJavaDev (24 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

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

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

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

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

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

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

BurntPizza (49 views)
2014-08-09 21:09:32
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!