summaryrefslogtreecommitdiffstats
path: root/src/demos/jgears
diff options
context:
space:
mode:
authorKenneth Russel <[email protected]>2005-08-17 21:41:40 +0000
committerKenneth Russel <[email protected]>2005-08-17 21:41:40 +0000
commitbdc9c5f4d9a9a428538a402c1248b4502415e6a6 (patch)
tree864f30d8b5fa820dacd473c4e68f2bc843dc6141 /src/demos/jgears
parentf7ab90c440fdd90deb9afe67b7e6707407b675be (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
Diffstat (limited to 'src/demos/jgears')
-rw-r--r--src/demos/jgears/JGears.java96
1 files changed, 88 insertions, 8 deletions
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);