what's up eveeryone?
I'm new into GUIs on java, and I've watched a video lesson on youtube teaching how to make some graphics interface with mouse and keyboard interacting.
On the lesson's video, the code worked PERFECTLY and I wrote just the same as the video but in my pc it didn't work.
It was supposed to move the box on the screen as we move it with the mouse and show a message "you clicked me" too. But in my pc the only thing that worked was the 'u clicked me' in the first loop and then, it didn't work anymore, and no dragging to.
so, here is the link of the video lessons:
#4 Input -- Java Game Development (LWJGL) - YouTube
the part where it shows the working program on the video is at 18:00 mins of video, and here, is the code that I wrote EXACTLY as the video:
import static org.lwjgl.opengl.GL11.*; import java.util.ArrayList; import java.util.List; import java.util.Random; import org.lwjgl.opengl.*; import org.lwjgl.*; import org.lwjgl.input.Mouse; import org.lwjgl.input.Keyboard; public class InputDemo { private List<Box> shapes = new ArrayList<Box>(16); private boolean somethingIsSelected = false; public InputDemo(){ try { Display.setDisplayMode(new DisplayMode(640, 480)); Display.setTitle("input demo"); Display.create(); } catch (LWJGLException e){ e.printStackTrace(); } shapes.add(new Box(15, 150)); shapes.add(new Box(100, 150)); //codigo de inicializar OPenGL glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, 640, 480, 0, 1, -1); glMatrixMode(GL_MODELVIEW); while(!Display.isCloseRequested()){ //render glClear(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); } //mouse.get pega a coordenada do mouse //mouse.getD pega quantos pixels o mouse se mecheu int mousey = 480 - Mouse.getY(); int mousex = Mouse.getX(); int dx = Mouse.getDX(); int dy = Mouse.getDY(); for(Box box: shapes){ if(Mouse.isButtonDown(0) && box.inBounds(Mouse.getX(), 480 - Mouse.getY()) && !somethingIsSelected){ somethingIsSelected = true; box.selected = true; System.out.println("You clicked me!"); } if(Mouse.isButtonDown(1)){ box.selected = false; somethingIsSelected = false; } if(box.selected){ box.update(Mouse.getDX(), Mouse.getDY()); } //box.randomizeColors(); 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 randomGenerator = new Random(); colorRed = randomGenerator.nextFloat(); colorBlue = randomGenerator.nextFloat(); colorGreen = randomGenerator.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 randomizeColors(){ Random randomGenerator = new Random(); colorRed = randomGenerator.nextFloat(); colorBlue = randomGenerator.nextFloat(); colorGreen = randomGenerator.nextFloat(); } void draw(){ glColor3f(colorRed, colorBlue, colorGreen); glBegin(GL_QUADS); glVertex2f(x, y); glVertex2f(x+50, y); glVertex2f(x+50, y+50); glVertex2f(x, y+50); glEnd(); } } public static void main(String[] args) { // TODO Auto-generated method stub new InputDemo(); } }