package examples; import java.util.ArrayList; import java.util.List; import java.util.Random; import org.lwjgl.LWJGLException; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.GL11; import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; public class MouseExample { private List shapes = new ArrayList(16); private boolean sis = false; private boolean cct = false; public void start() { try { Display.setDisplayMode(new DisplayMode(900,600)); Display.setTitle("Mouse Example"); Display.create(); } catch (LWJGLException e) { e.printStackTrace(); System.exit(0); } shapes.add(new Box(15,15)); // init OpenGL GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GL11.glOrtho(0, 900, 600, 0, 1, -1); GL11.glMatrixMode(GL11.GL_MODELVIEW); while (!Display.isCloseRequested()) { // Clear the screen and depth buffer GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); while(Keyboard.next()) { if(Keyboard.getEventKey() == Keyboard.KEY_C && Keyboard.getEventKeyState()) { shapes.add(new Box(15,15)); } } if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { Display.destroy(); System.exit(0); } for(Box box : shapes) { if (Mouse.isButtonDown(0) && box.inBounds(Mouse.getX(), 600 - Mouse.getY()) && !sis ) { sis = true; box.selected = true; System.out.println("clicked"); } if (Mouse.isButtonDown(1)) { sis = false; box.selected = false; } if (Mouse.isButtonDown(2) && box.inBounds(Mouse.getX(), 600 - Mouse.getY()) && !sis) { box.randColors(); cct = true; new Thread( new Runnable(){ @Override public void run(){ try { Thread.sleep(200); }catch(InterruptedException e){ e.printStackTrace(); }finally{ cct = false; } } }).run(); } if (box.selected){ box.update(Mouse.getDX(), -Mouse.getDY()); } box.draw(); } Display.update(); Display.sync(60); } Display.destroy(); } private static class Box { public int x , y; public boolean selected = false; private float colorRed , colorBlue , colorGreen; Box(int x , int y) { this.x = x; this.y = y; Random randgen = new Random(); colorRed = randgen.nextFloat(); colorBlue = randgen.nextFloat(); colorGreen = randgen.nextFloat(); } boolean inBounds(int mousex , int mousey) { if (mousex > x && mousex < x + 50 && mousey > y && mousey < y + 50) return true; else return false; } void update(int dx , int dy ) { x += dx; y += dy; } void randColors() { Random randgen = new Random(); colorRed = randgen.nextFloat(); colorBlue = randgen.nextFloat(); colorGreen = randgen.nextFloat(); } void draw() { GL11.glColor3f(colorRed, colorGreen, colorBlue); GL11.glBegin(GL11.GL_QUADS); GL11.glVertex2f(x, y); GL11.glVertex2f(x + 50, y); GL11.glVertex2f(x + 50, y + 50); GL11.glVertex2f(x, y + 50); GL11.glEnd(); } } public static void main(String[] argv) { MouseExample mouseExample = new MouseExample(); mouseExample.start(); } }