1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.tests.lwjgl; 18 19 import java.awt.BorderLayout; 20 import java.awt.Container; 21 22 import javax.swing.JFrame; 23 import javax.swing.SwingUtilities; 24 25 import com.badlogic.gdx.ApplicationAdapter; 26 import com.badlogic.gdx.Gdx; 27 import com.badlogic.gdx.backends.lwjgl.LwjglAWTCanvas; 28 import com.badlogic.gdx.graphics.GL20; 29 import com.badlogic.gdx.graphics.g2d.BitmapFont; 30 import com.badlogic.gdx.graphics.g2d.SpriteBatch; 31 import com.badlogic.gdx.tests.AnimationTest; 32 import com.badlogic.gdx.tests.MusicTest; 33 import com.badlogic.gdx.tests.UITest; 34 35 /** Demonstrates how to use LwjglAWTCanvas to have multiple GL widgets in a Swing application. 36 * @author mzechner */ 37 public class SwingLwjglTest extends JFrame { 38 LwjglAWTCanvas canvas1; 39 LwjglAWTCanvas canvas2; 40 LwjglAWTCanvas canvas3; 41 SwingLwjglTest()42 public SwingLwjglTest () { 43 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 44 45 Container container = getContentPane(); 46 canvas1 = new LwjglAWTCanvas(new MusicTest()); 47 canvas2 = new LwjglAWTCanvas(new UITest(), canvas1); 48 canvas3 = new LwjglAWTCanvas(new WindowCreator(), canvas1); 49 50 canvas1.getCanvas().setSize(200, 480); 51 canvas2.getCanvas().setSize(200, 480); 52 canvas3.getCanvas().setSize(200, 480); 53 54 container.add(canvas1.getCanvas(), BorderLayout.LINE_START); 55 container.add(canvas2.getCanvas(), BorderLayout.CENTER); 56 container.add(canvas3.getCanvas(), BorderLayout.LINE_END); 57 58 pack(); 59 setVisible(true); 60 setSize(800, 480); 61 } 62 63 class WindowCreator extends ApplicationAdapter { 64 SpriteBatch batch; 65 BitmapFont font; 66 67 @Override create()68 public void create () { 69 batch = new SpriteBatch(); 70 font = new BitmapFont(); 71 } 72 73 @Override dispose()74 public void dispose () { 75 font.dispose(); 76 batch.dispose(); 77 } 78 79 @Override render()80 public void render () { 81 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 82 batch.begin(); 83 font.draw(batch, "Click to create a new window", 10, 100); 84 batch.end(); 85 86 if (Gdx.input.justTouched()) { 87 createWindow(); 88 } 89 } 90 createWindow()91 private void createWindow () { 92 JFrame window = new JFrame(); 93 LwjglAWTCanvas canvas = new LwjglAWTCanvas(new UITest(), canvas1); 94 window.getContentPane().add(canvas.getCanvas(), BorderLayout.CENTER); 95 window.pack(); 96 window.setVisible(true); 97 window.setSize(200, 200); 98 } 99 } 100 main(String[] args)101 public static void main (String[] args) { 102 SwingUtilities.invokeLater(new Runnable() { 103 @Override 104 public void run () { 105 new SwingLwjglTest(); 106 } 107 }); 108 } 109 110 @Override dispose()111 public void dispose() { 112 canvas3.stop(); 113 canvas2.stop(); 114 canvas1.stop(); 115 116 super.dispose(); 117 } 118 } 119