diff options
author | Kenneth Russel <[email protected]> | 2005-08-17 21:41:40 +0000 |
---|---|---|
committer | Kenneth Russel <[email protected]> | 2005-08-17 21:41:40 +0000 |
commit | bdc9c5f4d9a9a428538a402c1248b4502415e6a6 (patch) | |
tree | 864f30d8b5fa820dacd473c4e68f2bc843dc6141 | |
parent | f7ab90c440fdd90deb9afe67b7e6707407b675be (diff) |
Revised JGears demo to add FPS counter and logos drawn with Java2D and
changed JRefract and JGearsFullscreen demos to use JGears instead.
Fixed bugs in fullscreen demos where workarounds were lost during last
refactoring; created new FullscreenWorkaround GLEventListener which
implements the workaround.
git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/../svn-server-sync/jogl-demos/branches/JSR-231@114 3298f667-5e0e-4b4a-8ed4-a3559d26a5f4
-rwxr-xr-x | src/demos/data/images/java_logo.png | bin | 0 -> 12715 bytes | |||
-rwxr-xr-x | src/demos/data/images/opengl_logo.png | bin | 0 -> 8740 bytes | |||
-rwxr-xr-x | src/demos/fullscreen/FullscreenWorkaround.java | 93 | ||||
-rwxr-xr-x | src/demos/fullscreen/GearsFullscreen.java | 4 | ||||
-rwxr-xr-x | src/demos/fullscreen/GearsFullscreen2.java | 1 | ||||
-rwxr-xr-x | src/demos/fullscreen/JGearsFullscreen.java | 6 | ||||
-rw-r--r-- | src/demos/jgears/JGears.java | 96 | ||||
-rwxr-xr-x | src/demos/jrefract/JRefract.java | 21 |
8 files changed, 195 insertions, 26 deletions
diff --git a/src/demos/data/images/java_logo.png b/src/demos/data/images/java_logo.png Binary files differnew file mode 100755 index 0000000..94350e5 --- /dev/null +++ b/src/demos/data/images/java_logo.png diff --git a/src/demos/data/images/opengl_logo.png b/src/demos/data/images/opengl_logo.png Binary files differnew file mode 100755 index 0000000..b6eacb3 --- /dev/null +++ b/src/demos/data/images/opengl_logo.png diff --git a/src/demos/fullscreen/FullscreenWorkaround.java b/src/demos/fullscreen/FullscreenWorkaround.java new file mode 100755 index 0000000..f76e710 --- /dev/null +++ b/src/demos/fullscreen/FullscreenWorkaround.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ + +package demos.fullscreen; + +import java.awt.*; +import javax.swing.*; + +import net.java.games.jogl.*; + +/** Class which implements workaround for full-screen bugs on Windows + when <code>-Dsun.java2d.noddraw=true</code> is specified as well + as a similar bug on Mac OS X. This code currently expects that the + GLAutoDrawable will be placed in a containing Frame. */ + +public class FullscreenWorkaround implements GLEventListener { + private int width; + private int height; + + /** Creates a full-screen workaround with the specified width and + height to set the full-screen window to later. */ + public FullscreenWorkaround(int width, int height) { + this.width = width; + this.height = height; + } + + public void init(GLAutoDrawable drawable) { + // Find parent frame if any + final Frame frame = getParentFrame((Component) drawable); + if (frame != null) { + EventQueue.invokeLater(new Runnable() { + public void run() { + frame.setVisible(false); + frame.setBounds(0, 0, width, height); + frame.setVisible(true); + frame.toFront(); + } + }); + } + } + + public void display(GLAutoDrawable drawable) {} + public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {} + public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {} + + //---------------------------------------------------------------------- + // Internals only below this point + // + + private static Frame getParentFrame(Component c) { + while (c != null && + (!(c instanceof Frame))) { + c = c.getParent(); + } + return (Frame) c; + } +} diff --git a/src/demos/fullscreen/GearsFullscreen.java b/src/demos/fullscreen/GearsFullscreen.java index 54d7aa1..71a2a84 100755 --- a/src/demos/fullscreen/GearsFullscreen.java +++ b/src/demos/fullscreen/GearsFullscreen.java @@ -51,9 +51,11 @@ public class GearsFullscreen { if (newMode != null) { frame.setUndecorated(true); } - GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities()); + final GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities()); canvas.addGLEventListener(new Gears()); + canvas.addGLEventListener(new FullscreenWorkaround(initWidth, initHeight)); + frame.add(canvas); frame.setSize(initWidth, initHeight); animator = new Animator(canvas); diff --git a/src/demos/fullscreen/GearsFullscreen2.java b/src/demos/fullscreen/GearsFullscreen2.java index 401ee62..ff02df2 100755 --- a/src/demos/fullscreen/GearsFullscreen2.java +++ b/src/demos/fullscreen/GearsFullscreen2.java @@ -55,6 +55,7 @@ public class GearsFullscreen2 { GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities()); canvas.addGLEventListener(new Gears()); + canvas.addGLEventListener(new FullscreenWorkaround(initWidth, initHeight)); frame.getContentPane().setLayout(new BorderLayout()); ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false); diff --git a/src/demos/fullscreen/JGearsFullscreen.java b/src/demos/fullscreen/JGearsFullscreen.java index ce232ad..759c50f 100755 --- a/src/demos/fullscreen/JGearsFullscreen.java +++ b/src/demos/fullscreen/JGearsFullscreen.java @@ -5,7 +5,7 @@ import java.awt.event.*; import javax.swing.*; import net.java.games.jogl.*; -import demos.gears.Gears; +import demos.jgears.JGears; import demos.util.*; /** @@ -52,8 +52,8 @@ public class JGearsFullscreen { if (newMode != null) { frame.setUndecorated(true); } - GLJPanel drawable = GLDrawableFactory.getFactory().createGLJPanel(new GLCapabilities()); - drawable.addGLEventListener(new Gears()); + GLJPanel drawable = new JGears(); + drawable.addGLEventListener(new FullscreenWorkaround(initWidth, initHeight)); frame.getContentPane().setLayout(new BorderLayout()); diff --git a/src/demos/jgears/JGears.java b/src/demos/jgears/JGears.java index 42e9dea..059b2ea 100644 --- a/src/demos/jgears/JGears.java +++ b/src/demos/jgears/JGears.java @@ -2,6 +2,11 @@ package demos.jgears; import java.awt.*; import java.awt.event.*; +import java.awt.geom.*; +import java.awt.image.*; +import java.io.*; +import java.text.*; +import javax.imageio.*; import javax.swing.*; import net.java.games.jogl.*; @@ -14,16 +19,68 @@ import demos.gears.Gears; * This version is equal to Brian Paul's version 1.2 1999/10/21 */ -public class JGears { - public static void main(String[] args) { - JFrame frame = new JFrame("Gear Demo"); - frame.getContentPane().setLayout(new BorderLayout()); - GLCapabilities caps = new GLCapabilities(); +public class JGears extends GLJPanel { + private static GLCapabilities caps; + private long startTime; + private int frameCount; + private float fps; + private static Font fpsFont = new Font("SansSerif", Font.BOLD, 24); + private DecimalFormat format = new DecimalFormat("####.00"); + private BufferedImage javaImage; + private BufferedImage openglImage; + + static { + caps = new GLCapabilities(); caps.setAlphaBits(8); - final GLJPanel drawable = GLDrawableFactory.getFactory().createGLJPanel(caps); - drawable.setOpaque(false); - drawable.addGLEventListener(new Gears()); + } + + public JGears() { + super(caps, new DefaultGLCapabilitiesChooser(), null); + addGLEventListener(new Gears()); + try { + InputStream in = JGears.class.getClassLoader().getResourceAsStream("demos/data/images/java_logo.png"); + BufferedImage image = ImageIO.read(in); + javaImage = scaleImage(image, 0.25f, 0.25f); + + in = JGears.class.getClassLoader().getResourceAsStream("demos/data/images/opengl_logo.png"); + image = ImageIO.read(in); + openglImage = scaleImage(image, 0.45f, 0.45f); + } catch (Exception e) { + e.printStackTrace(); + } + } + + + public void paintComponent(Graphics g) { + super.paintComponent(g); + if (startTime == 0) { + startTime = System.currentTimeMillis(); + } + + if (++frameCount == 30) { + long endTime = System.currentTimeMillis(); + fps = 30.0f / (float) (endTime - startTime) * 1000; + frameCount = 0; + startTime = System.currentTimeMillis(); + } + + if (fps > 0) { + g.setColor(Color.WHITE); + g.setFont(fpsFont); + g.drawString("FPS: " + format.format(fps), getWidth() - 140, getHeight() - 30); + } + int sp = 10; + if (javaImage != null) { + g.drawImage(javaImage, sp, getHeight() - javaImage.getHeight() - sp, null); + if (openglImage != null) { + g.drawImage(openglImage, sp + javaImage.getWidth() + sp, getHeight() - openglImage.getHeight() - sp, null); + } + } + } + + // Helper routine for various demos + public static JPanel createGradientPanel() { JPanel gradientPanel = new JPanel() { public void paintComponent(Graphics g) { ((Graphics2D) g).setPaint(new GradientPaint(0, 0, Color.WHITE, @@ -32,6 +89,29 @@ public class JGears { } }; gradientPanel.setLayout(new BorderLayout()); + return gradientPanel; + } + + private BufferedImage scaleImage(BufferedImage img, float xScale, float yScale) { + BufferedImage scaled = new BufferedImage((int) (img.getWidth() * xScale), + (int) (img.getHeight() * yScale), + BufferedImage.TYPE_INT_ARGB); + Graphics2D g = scaled.createGraphics(); + g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); + g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); + g.drawRenderedImage(img, AffineTransform.getScaleInstance(xScale, yScale)); + return scaled; + } + + public static void main(String[] args) { + JFrame frame = new JFrame("Gear Demo"); + frame.getContentPane().setLayout(new BorderLayout()); + GLCapabilities caps = new GLCapabilities(); + caps.setAlphaBits(8); + final GLJPanel drawable = new JGears(); + drawable.setOpaque(false); + + JPanel gradientPanel = createGradientPanel(); frame.getContentPane().add(gradientPanel, BorderLayout.CENTER); gradientPanel.add(drawable, BorderLayout.CENTER); diff --git a/src/demos/jrefract/JRefract.java b/src/demos/jrefract/JRefract.java index 5b8e4c1..ac9c93a 100755 --- a/src/demos/jrefract/JRefract.java +++ b/src/demos/jrefract/JRefract.java @@ -46,10 +46,10 @@ import javax.swing.event.*; import net.java.games.jogl.*; import net.java.games.jogl.util.*; -import demos.gears.Gears; import demos.hdr.HDR; import demos.hwShadowmapsSimple.HWShadowmapsSimple; import demos.infiniteShadowVolumes.InfiniteShadowVolumes; +import demos.jgears.JGears; import demos.proceduralTexturePhysics.ProceduralTexturePhysics; import demos.util.*; import demos.vertexBufferObject.VertexBufferObject; @@ -104,13 +104,13 @@ public class JRefract { inner.setVisible(true); GLCapabilities caps = new GLCapabilities(); - if (which == GEARS) { - caps.setAlphaBits(8); - } if (which == INFINITE) { caps.setStencilBits(16); } - final GLJPanel canvas = GLDrawableFactory.getFactory().createGLJPanel(caps); + final GLJPanel canvas = + (which == GEARS) ? + new JGears() : + GLDrawableFactory.getFactory().createGLJPanel(caps); final DemoListener demoListener = new DemoListener() { public void shutdownDemo() { removeJPanel(canvas); @@ -128,7 +128,7 @@ public class JRefract { switch (which) { case GEARS: { - canvas.addGLEventListener(new Gears()); + // GLEventListener already added break; } @@ -217,14 +217,7 @@ public class JRefract { } else if (which == GEARS) { // Provide control over transparency of gears background canvas.setOpaque(false); - JPanel gradientPanel = new JPanel() { - public void paintComponent(Graphics g) { - ((Graphics2D) g).setPaint(new GradientPaint(0, 0, Color.WHITE, - getWidth(), getHeight(), Color.DARK_GRAY)); - g.fillRect(0, 0, getWidth(), getHeight()); - } - }; - gradientPanel.setLayout(new BorderLayout()); + JPanel gradientPanel = JGears.createGradientPanel(); inner.getContentPane().add(gradientPanel, BorderLayout.CENTER); gradientPanel.add(canvas, BorderLayout.CENTER); |