The First Oni Program |
Documentation Contents |
The first Oni program is OpenWindow.java, which draws a white triangle on the screen as show below. Press the 'q' key to terminate.
Below is the step-by-step guide to this source code.
Here are the steps to execute the OpenWindow class under Windows OS. First of all, the following are required:
- Java 2 SDK has been installed.
- Oni's Java runtime, oni.jar, is ready to be read by -classpass, etc. upon execution.
- Oni's DLLs (gl.dll, grt.dll) are ready to be read by setting PATH to a directory where these DLLs exist or by other ways.
To run OpenWindow, a Oni program, after compilation, execute the following command.
> java oni.runtime.OniRuntime OpenWindowUnlike execution of common Java programs, the program activates the oni.runtime.OniRuntime class and then passes a class to be executed to its argument.
import oni.runtime.OniRuntime; import oni.runtime.Application; import oni.gfx.GLGraphics; import oni.gfx.GLConstants; import oni.gfx.PixelFormat; import oni.runtime.event.KeyListener; import oni.runtime.event.KeyEvent;First, the classes that control the screen with Oni are imported. The Oni packages used here are:
public class OpenWindow implements Application { private int width = 640; private int height = 480; public void oniMain(OniRuntime oniRuntime, String[] args) { ... } public void initGraphics(GLGraphics glGraphics) { ... } public void update(GLGraphics glGraphics) { ... } }The class activated by Oni must implement the
oni.runtime.Applicationnterface. This class must be able to be renewed by no-argument constructor. There are three methods that must be implemented by oni.runtime.Application;
oniMain(OniRuntime oniRuntime, String[] args): is called only once when activated.initGraphics(GLGraphics glGraphics): is called when the initialization ofGLGraphicsis required (when the window is first displayed, or is changed to full-screen, etc.)update(GLGraphics glGraphics): is called when drawing on the screen.
public void oniMain(OniRuntime oniRuntime, String[] args) {
oniRuntime.addKeyListener(new DemoKeyListener(oniRuntime));
oniRuntime.createWindow("your first KEEL program", width, height);
oniRuntime.createGraphicsWithDefaultPixelFormat();
oniRuntime.run();
System.out.println("Never called.");
}
This program does the following.
- Specifies
oni.runtime.event.KeyListener
objects that handle key input events.
- Specifies a window title and screen size.
- 3. Defines the default display setting,
oni.gfx.PixelFormat.
When the Application class is activated, this method is called first. The
argument, oni.runtime.OniRuntime
is a class that controls the screen. To create an actual screen, an instance
of OniRuntime must be retrieved and handled. After completing the settings
to the OniRuntime instance, the screen is displayed by calling the
run
method. Once the run method is called, the program will not proceed
further than that statement.
Define the initGraphics
and update Methods
public void initGraphics(GLGraphics glGraphics) {
glGraphics.glOrtho(0, width, 0, height, -1, 1);
glGraphics.glScalef(1.0f, -1.0f, 1.0f);
glGraphics.glTranslatef(0, -height, 0);
}
public void update(GLGraphics glGraphics) {
glGraphics.glClear(GLConstants.GL_COLOR_BUFFER_BIT);
drawTriangle(glGraphics);
glGraphics.glFlush();
}
public void quitRequested() {
}
private void drawTriangle(GLGraphics glGraphics) {
int topY = height / 3;
int bottomY = (height / 3) * 2;
int centerX = width / 2;
int leftX = width / 3;
int rightX = (width / 3) * 2;
glGraphics.glBegin(GLConstants.GL_TRIANGLES);
glGraphics.glColor3f(1.0f, 1.0f, 1.0f);
glGraphics.glVertex2i(leftX, bottomY);
glGraphics.glVertex2i(rightX, bottomY);
glGraphics.glVertex2i(centerX, topY);
glGraphics.glEnd();
}
In this program, the initGraphics method sets the graphics coordinates to
upper-left (0,0), lower-right (width, height). The update method clears the
screen and then draws a white triangle polygon.
In the initGraphics and update methods, oni.gfx.GLGraphics
objects are passed. With GLGraphics objects, you can write the program in
the same manner as OpenGL programming.
The quitRequested method is a method to be called for termination. It does
not do anything here.
Define the KeyListener Interface
class DemoKeyListener implements KeyListener {
public DemoKeyListener(OniRuntime aOniRuntime) {
oniRuntime = aOniRuntime;
}
public void keyTyped(KeyEvent event) {
System.out.println(event);
}
public void keyPressed(KeyEvent event) {
System.out.println(event);
if (event.getKeyCode() == KeyEvent.VK_Q) {
oniRuntime.disposeDisplay();
System.exit(0);
}
}
public void keyReleased(KeyEvent event) {
System.out.println(event);
}
private OniRuntime oniRuntime;
}
The oni.runtime.event.KeyListener
interface can receive keyboard events that are input by registering to the
OniRuntime instance. In the above program, a oni.runtime.event.KeyEvent
object passed by each keyboard event is written to standard output. When the
'q' key is pressed, the screen is closed and the program is terminated.
Copyright © 2001-2002 CyberStep, Inc. All
Rights Reserved.

Oni Software