This week's tutorial will actually be a lab and you should go the piano lab (for this week only - week 3 onwards will be in your regular tutorial room).
Your exercise for this lab is to create a simple polygon drawing app like this one. Although the color menu is optional.
Note: To run this code from command line type
export CLASSPATH=$CLASSPATH:/home/cs3421/jogamp/jar/jogl-all.jar:/home/cs3421/jogamp/jar/gluegen-rt.jar java -cp "$CLASSPATH:Drawing.jar" lab2.DrawingView
Go to Project > Properties > Java Buld Path > Libraries and add the jar files /home/cs3421/jogamp/jar/jogl-all.jar and /home/cs3421/jogamp/jar/gluegen-rt.jar to you build path.
Download Mouse.java and import it into your project.
We will give instruct you how to use this code later in the lab instructions
Create three classes:
This class tracks the state of the model. It contains the following information
Add appropriate accessor methods to allow other classes to read and write this data appropriately.
This class implements the GLEventListener to draw the model. It should store a reference to the model. Implement the display() method to:
Write a main() method in this class which:
Test this code by adding some polygons to your model and seeing if they are displayed correctly.
Mouse events have coordinates in viewport-coordinates; converting to world coordinates is tricky so I've provided some code for you. To use it:
Mouse.update(gl);
Where gl is the GL2 object.
You should do this in display before you draw your polygons, to make sure you are drawing the updated mouse co-ordinates.
panel.addMouseMotionListener(Mouse.theMouse);
float scale[] = new float[2]; //assuming your GLJPanel variable name is 'panel' panel.getCurrentSurfaceScale(scale); Mouse.setSurfaceScale(scale);
You can now call Mouse.getPosition() from any class to get the most recent mouse position in world coordinates.
You also need to create the DrawingController class.
This class should implement MouseListener and MouseMotionListener. (Hint: You could do this by extending a MouseAdapter)
You will also need to add this as a MouseListener and a MouseMotionListener to your panel in your drawingView.
Experiment with your program, drawing different shapes. Do they always come out the way you expect them to?
Experiment with your program by resizing the window. What happens if you make the window wider vs taller?
Add a menu item that allows you to set the background or foreground colors using a colorpicker, or whatever method you can think of
Add a menu item allowing you to erase the screen
Modify the code in some way to also allow drawing of circles.