CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2012
    Posts
    1

    Cool bug on lightwave java game library

    heym what's up everyone?
    So, i'm new in this stuff of writing GUIs on java and I was watching some video lessons on youtube on the subject.
    I wrote a code, and I wrote EXACTLY like the lesson but it didn't work on my pc although it worked well on the lesson.
    Anyway, the code is some boxes that pops up on the screen and I configured the mouse click to when the box is clicked it shows "you clicked me" and also a code to drag the box on the screen. The dragging didn't work and the 'u clicked me' worked only in the first loop...

    so, here is the code, I'll also post the link for the lesson:

    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();

    }

    }

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: bug on lightwave java game library

    Your pots has been validated. You should be able to see it now.

    Cimperiali
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured