In the first two tutorials we used the Simple Turtle and the Turtle Graphic to draw something on the frame. We only could draw lines on the frame with the Turtle Graphic, but now we will see how to draw other figures than lines on the frame.
We will draw a rectangle, an ellipse and a self defined figure. The ellipse and the self defined figure will be filled out.
We have to program the following for this example:
#include "grubc/awt/frame.h" #include "grubc/graphic/shape.h" /* Declare the needed variables. */ Jvm jvm; Frame frame; Shape shape; jboolean exitprg = JNI_FALSE; /* Declare the needed listener callback function. */ void windowListener(jint event, Window window); int main(int argc, char *argv[]) { WindowListener wl; /* WindowListener Object. */ initJvm(&jvm, "./grubc.jar"); /* Initialize the JVM. */ createFrame(&frame); /* Create the frame. */ createShape(&shape); /* Create the shape. */ /* Register the callback functions as listeners. */ wl = frame.addWindowListener(&windowListener); /* Set the title and state of the frame. */ frame.setTitle("Tutorial 3"); frame.setState(FRAME_STATE_MAXIMIZED); frame.show(); /* Display the frame. */ /* Draw the predefined figures. */ frame.drawRectangle(10, 10, 100, 50, JNI_FALSE, &BLUE); frame.drawEllipse(10, 100, 200, 50, JNI_TRUE, &GREEN); /* Define how a non standard shape should look like. */ Color c = {255, 0, 0, 150}; /* Transparent red. */ shape.setColor(&c); /* Set the color. */ shape.setFilled(JNI_TRUE); shape.moveTo(300, 10); /* Move to starting point. */ shape.lineTo(300, 100); shape.curveTo(300, 200, 200, 100, 100, 300); shape.quadTo(100, 100, 200, 10); shape.closePath(); /* Makes a line back to the starting point. */ /* Draw the shape. */ frame.drawShape(&shape); /* Wait until exitprg is true. */ WAIT_UNTIL_TRUE(exitprg); frame.close(); /* Close the frame. */ /* Deregister the callback functions. */ frame.removeWindowListener(wl); /* Cleanup */ deleteShape(&shape); deleteFrame(&frame); return 0; } void windowListener(jint event, Window window) { if (frame.isSameWindow(window)) { switch (event) { case CB_WINDOW_OPENED: break; case CB_WINDOW_CLOSING: exitprg = JNI_TRUE; /* Frame should be closed. */ break; case CB_WINDOW_CLOSED: break; case CB_WINDOW_ICONIFIED: break; case CB_WINDOW_DEICONIFIED: break; case CB_WINDOW_ACTIVATED: break; case CB_WINDOW_DEACTIVATED: break; } } }
When you execute this program you will get a window which looks like this (the window will be maximized, this is not because of lack of space):