net.bryanbergen.lwjgl.test.Main.java Source code

Java tutorial

Introduction

Here is the source code for net.bryanbergen.lwjgl.test.Main.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package net.bryanbergen.lwjgl.test;

import java.util.Random;
import net.bryanbergen.lwjgl.display.Color;
import net.bryanbergen.lwjgl.display.Window;
import net.bryanbergen.lwjgl.display.primitives.Dimension;
import net.bryanbergen.lwjgl.input.keyboard.KeyAdapter;
import net.bryanbergen.lwjgl.input.keyboard.KeyCode;
import net.bryanbergen.lwjgl.input.keyboard.KeyEvent;
import net.bryanbergen.lwjgl.input.mouse.MouseAdapter;
import net.bryanbergen.lwjgl.input.mouse.MouseButton;
import net.bryanbergen.lwjgl.input.mouse.MouseEvent;
import net.bryanbergen.lwjgl.logging.Logger;
import org.lwjgl.opengl.GL11;
import static org.lwjgl.opengl.GL11.GL_QUADS;

/**
 *
 * @author bryan
 */
public class Main {

    private static Color color = Color.RED;

    public static void main(String... args) {
        Dimension d = new Dimension(0, 0, 960, 600);
        Window.Builder builder = new Window.Builder(d);
        builder.setTitle("Test Window");
        builder.setFullscreen(false);
        builder.setResizable(true);
        builder.setVisible(true);
        builder.setVsync(true);
        builder.setBackground(Color.BLUE);

        Window window = builder.build();
        window.addKeyListener(new KeyAdapter() {
            @Override
            public void KeyPressed(KeyEvent event) {
                if (event.getKeycode() == KeyCode.KEY_ESCAPE) {
                    window.close();
                }
            }
        });
        window.addKeyListener(new ColorSwapper());
        window.addMouseListener(new ColorClicker(window));
        while (!window.shouldClose()) {
            window.clear();
            // immediate mode for testing only
            GL11.glColor3f((float) color.getRed(), (float) color.getGreen(), (float) color.getBlue());
            GL11.glBegin(GL_QUADS);
            GL11.glVertex2f(-.5f, -.5f);
            GL11.glVertex2f(-.5f, .5f);
            GL11.glVertex2f(.5f, .5f);
            GL11.glVertex2f(.5f, -.5f);
            GL11.glEnd();
            window.update();
        }
        window.dispose();
    }

    private static class ColorSwapper extends KeyAdapter {

        @Override
        public void KeyPressed(KeyEvent event) {
            if (event.getKeycode() == KeyCode.KEY_G) {
                color = Color.GREEN;
            } else if (event.getKeycode() == KeyCode.KEY_R) {
                color = Color.RED;
            }
        }
    }

    private static class ColorClicker extends MouseAdapter {

        private final Window window;

        public ColorClicker(Window window) {
            this.window = window;
        }

        @Override
        public void mouseDragged(MouseEvent event) {
            if (event.getButton() == MouseButton.BUTTON_LEFT) {
                color = new Random().nextBoolean() ? Color.WHITE : Color.BLACK;
            }
        }

        @Override
        public void mousePressed(MouseEvent event) {
            if (event.getButton() == MouseButton.BUTTON_RIGHT) {
                Logger log = Logger.getLogger(Main.class);
                log.warn("Testing Logging", new NullPointerException("Exception Message"));
            }
        }
    }
}