Note: in order to use the graphics classes, you will need to download graphics.jar. In addition, you will have to setup the classpath to use the graphic's package. Do this using
export CLASSPATH=graphics.jar:.
Exercises on the graphics classes.
Draw the extended snowman below:
It's easier if you work out how to draw the diagram first.
hint.
Draw the bullseye below
using either a for loop or a while loop.
Hint: you need to plan carefully to work out how to draw this.
Write code to time the double clicking of a mouse. You can read the timer value by using the method System.currentTimeMillis(). Note however, that this returns a long integer, so you will have to use long to read it. If you want to measure the time of something, use the code
long start;
long stop;
start = System.currentTimeMillis();
:
:
stop = System.currentTimeMillis();
System.out.println("Time required was " + (stop - start) + " milliseconds.");
You should also look at the isPressed method of the Window class.
Ideally the whole thing would be in a loop so you could measure any improvement in your double clicking speed.
Here's a hint.
Draw a graph of the function y = sin(x). You can do this by finding the points to draw. You will need to translate the the mathematical point x, y to the pixel coordinates. Here is a hint.
Note that there is a method Math.sin() which will return the sin of a number.
Try and vary the scaling of x and y so that the graph looks better. Use some other methods. E.g. square root, Math.sqrt(), Math.cos(), y = x2