summaryrefslogtreecommitdiffstats
path: root/src/demos/jgears/JGears.java
blob: 059b2ea1c42fa6d4ea7a5f9064cde782d2d72432 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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.*;
import demos.gears.Gears;

/**
 * JGears.java <BR>
 * author: Brian Paul (converted to Java by Ron Cemer and Sven Goethel) <P>
 *
 * This version is equal to Brian Paul's version 1.2 1999/10/21
 */

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);
  }
  
  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,
                                                      getWidth(), getHeight(), Color.DARK_GRAY));
          g.fillRect(0, 0, getWidth(), getHeight());
        }
      };
    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);

    final JCheckBox checkBox = new JCheckBox("Transparent", true);
    checkBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          drawable.setOpaque(!checkBox.isSelected());
        }
      });
    frame.getContentPane().add(checkBox, BorderLayout.SOUTH);

    frame.setSize(300, 300);
    final Animator animator = new Animator(drawable);
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          // Run this on another thread than the AWT event queue to
          // make sure the call to Animator.stop() completes before
          // exiting
          new Thread(new Runnable() {
              public void run() {
                animator.stop();
                System.exit(0);
              }
            }).start();
        }
      });
    frame.show();
    animator.start();
  }
}