Display.setParent() problem

Started by byyyk, December 21, 2008, 21:28:11

Previous topic - Next topic

byyyk

Hi, i'm new into LWJGL and i try to run my first app in a JFrame. I looked at some of other problems with this method but haven't found anything similiar. I add the canvas to JFrame and set is as parent (Display.setParent(canvas)) and it crashes at creating the display. This is the error:
org.lwjgl.LWJGLException: Parent.isDisplayable() must be true
        at org.lwjgl.opengl.Display.createWindow(Display.java:303)
        at org.lwjgl.opengl.Display.create(Display.java:816)
        at org.lwjgl.opengl.Display.create(Display.java:744)
        at org.lwjgl.opengl.Display.create(Display.java:725)
        at szachy.Game.init(Main.java:235)
        at szachy.Game.start(Main.java:211)
        at szachy.Game.<init>(Main.java:189)
        at szachy.MainFrame.<init>(Main.java:68)
        at szachy.Main.main(Main.java:25)


Don't know how to make it displayable, here's the code, hope you guys can help:

class Game {
    public Game(JFrame parent) {
        canvas = new Canvas();
        canvas.setSize(parent.getWidth(), parent.getHeight());
        canvas.setFocusable(true);
        canvas.setIgnoreRepaint(true);
        //System.out.println(canvas.isDisplayable()); returns false
        parent.add(canvas);
        start();
    }
  /** Game title */
  public static final String GAME_TITLE = "My Game";
 
  /** Desired frame time */
  private static final int FRAMERATE = 60;
 
  /** Exit the game */
  private static boolean finished;
 
  /** Angle of rotating square */
  private static float angle;
 
  /**
   * Application init
   * @param args Commandline args
   */
  public void start() {
    boolean fullscreen = false;
 
    try {
      init(fullscreen);
      run();
    } catch (Exception e) {
      e.printStackTrace(System.err);
      Sys.alert(GAME_TITLE, "An error occured and the game will exit.");
    } finally {
      cleanup();
    }
    System.exit(0);
  }
 
  /**
   * Initialise the game
   * @throws Exception if init fails
   */
  private static void init(boolean fullscreen) throws Exception {
    // Create a fullscreen window with 1:1 orthographic 2D projection (default)
    Display.setTitle(GAME_TITLE);
    //Display.setFullscreen(fullscreen);
    Display.setParent(canvas); //++++++++++++++++++++++++++++++++++++++++++++++++++++++++this is the problem
    // Enable vsync if we can (due to how OpenGL works, it cannot be guarenteed to always work)
    Display.setVSyncEnabled(true);
    // Create default display of 640x480
    Display.create(); //++++++++++++++++++++++++++++++++++++++++++++++++++++++++crushes here
  }
 
  /**
   * Runs the game (the "main loop")
   */
  private static void run() {
 
    while (!finished) {
      // Always call Window.update(), all the time - it does some behind the
      // scenes work, and also displays the rendered output
      Display.update();
 
      // Check for close requests
      if (Display.isCloseRequested()) {
	finished = true;
      } 
 
      // The window is in the foreground, so we should play the game
      else if (Display.isActive()) {
        logic();
        render();
        Display.sync(FRAMERATE);
      } 
 
      // The window is not in the foreground, so we can allow other stuff to run and
      // infrequently update
      else {
        try {
          Thread.sleep(100);
        } catch (InterruptedException e) {
        }
        logic();
 
	// Only bother rendering if the window is visible or dirty
        if (Display.isVisible() || Display.isDirty()) {
          render();
        }
      }
    }
  }
 
  /**
   * Do any game-specific cleanup
   */
  private static void cleanup() {
    // Close the window
    Display.destroy();
  }
 
  /**
   * Do all calculations, handle input, etc.
   */
  private static void logic() {
    // Example input handler: we'll check for the ESC key and finish the game instantly when it's pressed
    if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
      finished = true;
    }
 
    // Rotate the square
    angle += 2.0f % 360;
  }
    private static void render() {
    // clear the screen
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
 
    // center square according to screen size
    GL11.glPushMatrix();
    GL11.glTranslatef(Display.getDisplayMode().getWidth() / 2, Display.getDisplayMode().getHeight() / 2, 0.0f);
 
      // rotate square according to angle
      GL11.glRotatef(angle, 0, 0, 1.0f);
      GL11.glBegin(GL11.GL_QUADS);
        GL11.glVertex2i(-50, -50);
        GL11.glVertex2i(50, -50);
        GL11.glVertex2i(50, 50);
        GL11.glVertex2i(-50, 50);
      GL11.glEnd();
 
    GL11.glPopMatrix();
  }
  static private Canvas canvas;
}


This code is a slightly changed wiki example: http://gpwiki.org/index.php/OpenGL:Tutorials:Java:LWJGL:Introduction
The other thing is if i don't set the parent it works. But it goes to fullscreen even if i set it to false and i don't see anything happen but the black screen and cursor.

princec

By default JFrames aren't actually visible when constructed. You need to call setVisible(true) on them first.

Cas :)

byyyk

It worked! But i'm a bit suprised cause i did set it visible but in the main method. After adding the same line to the Game constructor it stared to work. Thank you very much I can finally go on with the project :)
Also if anyone could refer to my second problem... I'm going to read through nehe tutorials now anyway so maybe i'll find the glitch on my own but i would be glad if someone would point that one for me so that i can see anything in 3d :)

EDIT: Okay, I don't know why won't this work... I tried some of the examples from nehe both on linux and windows, and every time i get only black screen. Anyone knows what might be the cause? At first i thought that maybe it is rendering the objects (because it is entering the render() method) but the color is the same as the background so I also tried examples with coloring and i still get the same, doesn't matter if i run it in JFrame or like in the tutorials. I'd appreciate any help, thanks.