CSC941/CSC931 Autumn 2009
CSC931/CSC941 Autumn 2009
Java Practical 2: Simple applets and using BlueJ
Aims
· To learn how to use the BlueJ tool to make simple changes to an applet project.
Objectives
By the end of this session, students will be able to:
· Use BlueJ to compile and run an existing applet project.
· Write Java statements for drawing simple coloured shapes and messages on the screen.
Remember to register your practical attendance in the usual way!
Further Documentation
You will need to refer to the notes for Java lecture 3. These are available online at http://www.cs.stir.ac.uk/courses/CSC941/special/lectures/
After you have finished this practical, if you’re feeling very confident and want to learn more about drawing things in Java, have a look here: http://www.cs.stir.ac.uk/doc/java/jdk1.5/api/java/awt/Graphics.html
Copying an existing applet project with BlueJ
Most of the time it is more convenient to copy and modify an existing applet project rather than creating a new one from scratch.
· Look at your lecture notes showing how to use BlueJ (Java lecture 3). Those of you in the Monday morning practicals, which, unfortunately, take place before the lecture, will be supplied with an advance copy of the lecture notes to use in the practical.
Work carefully through lecture slides 3-15, carrying out the process described there.
When you have reached slide 15, close the HTML file, but do not close the project, nor BlueJ as there are now some exercises for you to attempt.
For the following steps I recommend that you carry out the compile/run/view/quit cycle after every step. This will make it easier to track down any mistakes that you might make. If you have any difficulties, especially with compiling errors, please ask a demonstrator for help.
Each time that you have finished viewing the applet, you should quit the Java applet viewer: click on the close box (X) in the applet viewer window. This does not quit the BlueJ system.
· Make a change to the applet so that the message “Hello World” appears in blue.
· After the statements that draw the red oval, write two statements to draw a green rectangle around the oval. (You will need to use setColor and drawRect).
· Change the red oval to a filled red oval (fillOval). When you view the applet, the message will have disappeared – it is behind the oval!
· Move the statements for drawing the oval to before those drawing the message. The message reappears. Why?
· Change the drawing of the green rectangle so that it is a filled rectangle, and so that the message and the red oval are still visible.
· Create a black border for the green rectangle by drawing four separate black lines for its sides. Does it matter whether the border is drawn before or after the rectangle itself? Find out by experiment. Can you think of an easier way to draw the border?
· Now, view the applet using a browser: type in the URL of the HelloWorld.html file.
· Next, add the applet directly to the applets.html document that you created in the first Java practical. Then view applets.html using a browser to make sure that your applet works OK.
Improvising your own picture
· Follow the lecture note instructions again to make a fresh copy of the HelloWorld project, but this time, save it with a different folder name – for example “MyPicture”.
· Replace the drawing statements in the paint method with your own drawing statements to draw a nice picture – you can choose a simple subject for the picture. Experiment with different colours and shapes and have fun!
That’s all!
Department of Computing Science & Mathematics, University of Stirling Page 1 of 2