Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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  
  LWJGL canvas in Swing app - resize problems  (Read 1384 times)
0 Members and 1 Guest are viewing this topic.
Offline Mac70
« Posted 2013-03-12 16:59:51 »

Like in topic. I am trying to embed LWJGL canvas into Swing app, but I am encountering problems. It seems that when you increase the size of applet window everything is resizing correctly, but when you are trying to shrink applet window canvas does not resize correctly (LWJGL Display still have the same size even if it is much bigger than program window). Is there anything I can do to fix this?

Check out my Devblog! Smiley
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 75
Projects: 15


★★★★★


« Reply #1 - Posted 2013-03-12 17:07:11 »

have you set the correct glViewport parameters after the resize?
Offline davedes
« Reply #2 - Posted 2013-03-12 17:08:26 »

Also you need to update your orthographic projection matrix with the new size.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mac70
« Reply #3 - Posted 2013-03-12 18:25:55 »

Still the same problem. Here is my code:

HouseCalc applet:

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  
package Form;

import Mapper.Mapper;

public class HouseCalc extends javax.swing.JApplet {

    Mapper mapper;
   
    @Override
    public void init() {
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
       /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(HouseCalc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(HouseCalc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(HouseCalc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(HouseCalc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    initComponents();
                    mapper = new Mapper();
                    mapper.run();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
   private void initComponents() {

        mainPane = new javax.swing.JPanel();
        interfacePane = new javax.swing.JPanel();
        programFrame = new java.awt.Canvas();

        mainPane.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                mainPaneComponentResized(evt);
            }
        });

        javax.swing.GroupLayout interfacePaneLayout = new javax.swing.GroupLayout(interfacePane);
        interfacePane.setLayout(interfacePaneLayout);
        interfacePaneLayout.setHorizontalGroup(
            interfacePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 250, Short.MAX_VALUE)
        );
        interfacePaneLayout.setVerticalGroup(
            interfacePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 600, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout mainPaneLayout = new javax.swing.GroupLayout(mainPane);
        mainPane.setLayout(mainPaneLayout);
        mainPaneLayout.setHorizontalGroup(
            mainPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPaneLayout.createSequentialGroup()
                .addComponent(interfacePane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(programFrame, javax.swing.GroupLayout.DEFAULT_SIZE, 540, Short.MAX_VALUE))
        );
        mainPaneLayout.setVerticalGroup(
            mainPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(interfacePane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(programFrame, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>

    private void mainPaneComponentResized(java.awt.event.ComponentEvent evt) {
        mapper.resizeRequest();
    }

    // Variables declaration - do not modify
   private javax.swing.JPanel interfacePane;
    private javax.swing.JPanel mainPane;
    public static java.awt.Canvas programFrame;
    // End of variables declaration
}


Mapper:

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  
package Mapper;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

public class Mapper {
   
    protected GLUtils util;
   
    private boolean resizeRequested = false;
   
    public Mapper() {}
   
    public void run() {
        Thread engine = new Thread() {
            public void run() {
                util = new GLUtils();
                util.initDisplay();
                util.initOpenGL();
                loop();
            }
        };
        engine.start();
    }
   
    private void loop() {
        while (true) {
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
            resize();
            draw();
            Display.update();
            Display.sync(30);
        }
    }
   
    private void draw() {
        GL11.glBegin(GL11.GL_TRIANGLES);
            GL11.glVertex2f(0.1f, 0.1f);
            GL11.glVertex2f(0.1f, 0.5f);
            GL11.glVertex2f(0.5f, 0.5f);
        GL11.glEnd();
    }
   
    public void resizeRequest() {
        resizeRequested=true;
    }
   
    private void resize() {
        if (resizeRequested) {
            util.refit();
            resizeRequested=false;
        }
    }
   
}


GLUtils:

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  
package Mapper;

import Form.HouseCalc;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

public class GLUtils {
   
    protected GLUtils() {}
   
    protected void initDisplay() {
        try {
            HouseCalc.programFrame.setIgnoreRepaint(true);
            HouseCalc.programFrame.setFocusable(true);
            Display.setParent(HouseCalc.programFrame);
            Display.setVSyncEnabled(true);
            Display.setResizable(true);
            Display.create();
        } catch (LWJGLException ex) {
            Logger.getLogger(GLUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
   
    protected void initOpenGL() {
        GL11.glClearColor(0, 0, 0, 0);
        GL11.glViewport(0, 0, HouseCalc.programFrame.getWidth(), HouseCalc.programFrame.getHeight());
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, 1, 0, 1, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
    }
   
    protected void refit() {
        GL11.glViewport(0, 0, HouseCalc.programFrame.getWidth(), HouseCalc.programFrame.getHeight());
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, 1, 0, 1, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
    }
   
}


In theory, everything should work correctly, but it isn't.

EDIT: it seems that the cause is canvas. Can I fix it or replace with something else?

Check out my Devblog! Smiley
Offline baitian3744

Innocent Bystander





« Reply #4 - Posted 2013-04-01 16:46:03 »

Firstly, sorry for my English, because English isn't my mother language.
I encountered this problem... several minutes ago, in my desktop application. In my application, Display -> Canvas -> Jpanel -> Jframe (-> means "is contained by"). The canvas cannot shrink accordingly, while increasing size works well (just like what you encourted), then I found that the canvas doesn't shrink with its parent(I make the canvas's backgroud color gray to figure it out). Since the code is automatically generated by WindowBuilder, I checked and altered the way how Jpanel is located in Jframe by modifying ".addComponent(panel, GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)" to ".addComponent(panel, GroupLayout.PREFERRED_SIZE, 404, Short.MAX_VALUE)", and it works. So I suppose whether your solution is similar to mine, whether your automatic tools (such as WindowBuilder or Jigloo) understand you acurately.
Here is the example duplicate the problem:
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  
package com.uestc.test;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Canvas;
import java.awt.Color;

public class TestScalableFrame extends JFrame {

   private JPanel contentPane;

   /**
    * Launch the application.
    */

   public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {
         public void run() {
            try {
               TestScalableFrame frame = new TestScalableFrame();
               frame.setVisible(true);
            } catch (Exception e) {
               e.printStackTrace();
            }
         }
      });
   }

   /**
    * Create the frame.
    */

   public TestScalableFrame() {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setBounds(100, 100, 450, 300);
      contentPane = new JPanel();
      contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
      setContentPane(contentPane);
     
      JPanel panel = new JPanel();
      GroupLayout gl_contentPane = new GroupLayout(contentPane);
      gl_contentPane.setHorizontalGroup(
         gl_contentPane.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_contentPane.createSequentialGroup()
               .addContainerGap()
               .addComponent(panel, GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)
               .addContainerGap())
      );
      gl_contentPane.setVerticalGroup(
         gl_contentPane.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_contentPane.createSequentialGroup()
               .addContainerGap()
               .addComponent(panel, GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
               .addContainerGap())
      );
      panel.setLayout(new BorderLayout(0, 0));
     
      Canvas canvas = new Canvas();
      canvas.setBackground(Color.LIGHT_GRAY);
      panel.add(canvas);
      contentPane.setLayout(gl_contentPane);
   }
}

and here is the one which increase size and shrink:
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  
package com.uestc.test;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Canvas;
import java.awt.Color;

public class TestScalableFrame2 extends JFrame {

   private JPanel contentPane;

   /**
    * Launch the application.
    */

   public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {
         public void run() {
            try {
               TestScalableFrame2 frame = new TestScalableFrame2();
               frame.setVisible(true);
            } catch (Exception e) {
               e.printStackTrace();
            }
         }
      });
   }

   /**
    * Create the frame.
    */

   public TestScalableFrame2() {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setBounds(100, 100, 450, 300);
      contentPane = new JPanel();
      contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
      setContentPane(contentPane);
     
      JPanel panel = new JPanel();
      GroupLayout gl_contentPane = new GroupLayout(contentPane);
      gl_contentPane.setHorizontalGroup(
         gl_contentPane.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_contentPane.createSequentialGroup()
               .addContainerGap()
               .addComponent(panel, GroupLayout.PREFERRED_SIZE, 404, Short.MAX_VALUE)
               .addContainerGap())
      );
      gl_contentPane.setVerticalGroup(
         gl_contentPane.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_contentPane.createSequentialGroup()
               .addContainerGap()
               .addComponent(panel, GroupLayout.PREFERRED_SIZE, 232, Short.MAX_VALUE)
               .addContainerGap())
      );
      panel.setLayout(new BorderLayout(0, 0));
     
      Canvas canvas = new Canvas();
      canvas.setBackground(Color.LIGHT_GRAY);
      panel.add(canvas);
      contentPane.setLayout(gl_contentPane);
   }
}
Offline Tyecon

Senior Newbie





« Reply #5 - Posted 2013-04-01 22:29:42 »

I did that, once I used a atomic reference to the window size and updated the viewport/projection when needed. However there was a bug where if you gave input to the lwjgl area it would hog the input from swing, is that fixed now?

Please have a baggy ready before looking at my code.
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.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36

Riven (45 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

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

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

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
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!