Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package p5.test_awt_canvas;
- import java.applet.Applet;
- import java.awt.Canvas;
- import java.awt.Frame;
- import java.awt.GraphicsDevice;
- import java.awt.GraphicsEnvironment;
- import java.awt.Insets;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.GL11;
- public class MiniPApplet extends Applet {
- private static final long serialVersionUID = 1L;
- /////////////////////////////////////////////////////////////
- //
- // Test parameters
- public int frameRate = 120;
- public boolean resizeableFrame = false;
- /////////////////////////////////////////////////////////////
- //
- // Internal variables
- int width;
- int height;
- private Frame frame;
- private Canvas canvas;
- private long beforeTime;
- private long overSleepTime;
- private long frameRatePeriod = 1000000000L / frameRate;
- private boolean initialized = false;
- private double theta = 0;
- private double s = 0;
- private double c = 0;
- private long millisOffset;
- private int fcount, lastm;
- private float frate;
- private int fint = 3;
- void run() {
- Thread loop = new Thread("Animation Thread") {
- public void run() {
- int frameCount = 0;
- while (true) {
- if (!initialized) {
- setup();
- }
- draw();
- Display.update();
- clock();
- frameCount++;
- }
- }
- };
- loop.start();
- }
- void setup() {
- millisOffset = System.currentTimeMillis();
- // Frame setup ----------------------------------------------------------
- width = 300;
- height = 300;
- MiniPApplet applet = this;
- GraphicsEnvironment environment =
- GraphicsEnvironment.getLocalGraphicsEnvironment();
- GraphicsDevice displayDevice = environment.getDefaultScreenDevice();
- frame = new Frame(displayDevice.getDefaultConfiguration());
- frame.setTitle("MiniPApplet");
- frame.setLayout(null);
- frame.add(applet);
- frame.pack();
- frame.setResizable(resizeableFrame);
- Insets insets = frame.getInsets();
- int windowW = applet.width + insets.left + insets.right;
- int windowH = applet.height + insets.top + insets.bottom;
- int locationX = 100;
- int locationY = 100;
- frame.setSize(windowW, windowH);
- frame.setLocation(locationX, locationY);
- int usableWindowH = windowH - insets.top - insets.bottom;
- applet.setBounds((windowW - applet.width)/2, insets.top + (usableWindowH - applet.height)/2, applet.width, applet.height);
- frame.add(this);
- frame.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- frame.setVisible(true);
- // Canvas setup ----------------------------------------------------------
- canvas = new Canvas();
- applet.add(canvas);
- canvas.setBounds(0, 0, applet.width, applet.height);
- canvas.setFocusable(true);
- canvas.requestFocus();
- canvas.setIgnoreRepaint(true);
- try {
- Display.setParent(canvas);
- Display.create();
- Display.setVSyncEnabled(false);
- } catch (LWJGLException e) {
- e.printStackTrace();
- }
- initialized = true;
- }
- void draw() {
- GL11.glClearColor(0, 0, 0, 1);
- GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
- theta += 0.01;
- s = Math.sin(theta);
- c = Math.cos(theta);
- GL11.glBegin(GL11.GL_TRIANGLES);
- GL11.glColor3f(1, 0, 0);
- GL11.glVertex2d(-c, -c);
- GL11.glColor3f(0, 1, 0);
- GL11.glVertex2d(0, c);
- GL11.glColor3f(0, 0, 1);
- GL11.glVertex2d(s, -s);
- GL11.glEnd();
- GL11.glFlush();
- fcount += 1;
- int m = (int) (System.currentTimeMillis() - millisOffset);
- if (m - lastm > 1000 * fint) {
- frate = (float)(fcount) / fint;
- fcount = 0;
- lastm = m;
- System.out.println("fps: " + frate);
- }
- }
- void clock() {
- long afterTime = System.nanoTime();
- long timeDiff = afterTime - beforeTime;
- long sleepTime = (frameRatePeriod - timeDiff) - overSleepTime;
- if (sleepTime > 0) { // some time left in this cycle
- try {
- Thread.sleep(sleepTime / 1000000L, (int) (sleepTime % 1000000L));
- } catch (InterruptedException ex) { }
- overSleepTime = (System.nanoTime() - afterTime) - sleepTime;
- } else { // sleepTime <= 0; the frame took longer than the period
- overSleepTime = 0L;
- }
- beforeTime = System.nanoTime();
- }
- public static void main(String[] args) {
- MiniPApplet mini;
- try {
- Class<?> c = Thread.currentThread().getContextClassLoader().loadClass(MiniPApplet.class.getName());
- mini = (MiniPApplet) c.newInstance();
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- if (mini != null) {
- mini.run();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement