Introduction to Java Applets
Introduction
Here I have made a brief introduction to programming java applets, after a few requests by people to see source code. Because the people I am gearing this toward are scientists, they typically have programming experience for the command line in languages such as C or C++. Such programmes can be object oriented, but they are still rather linear. The programme starts at "main" and we follow if/then statements and ask at predictable times the user for input.
When we start having a graphical user interface (GUI) with buttons, and watching the mouse move, then when the we get user input is no longer predictible. We move to something termed event-driven programming. An event is a fancy word for what happens when you move the mouse or hit a button and expect the computer to respond.
(Of course, behind the scenes the programme works in the same way as we can write such programmes in C or C++. Take the OK button on a typical dialog on a computer. The computer sets up something called a thread that runs in the background and checks to see if the button has been clicked. If it has, the thread notifies the programme that it has been clicked, and that it should do something about it. Because the buisness of setting up threads and communicating between them is messy and hard, we abstract away this difficult behaviour. We deal with this abstracted behavour, and it is this that is referred to as event-driven programming)
What I hope to do is give some example code that will show you how event-driven programming works, and how to write java applets.
I am not
- trying to teach you how to program
- teaching you about object-oriented programming (although java is o-o)
- being complete
- being consistent between British and American English.
Worked examples
HelloWorld Applet
-
Prints hello world in the browser
Simple first applet
Learn how to compile your own java applet and run it in your browser
Learn how to draw on the screen
HelloOthers Applet
-
Prints hello to anyone. Includes a textbox to specify the name and radiobuttons to change the colour of the text.
Learn how to use an applet with multiple files for source code.
Learn how to give an applet controls like buttons, radiobuttons and textboxes
Learn how java lays out the controls, and get it to do what you want
Slope Applet
-
An applet that shows a ball rolling down a hill with different inclines, drawing force vectors
Learn how to create an animation.
Learn how to create different line thicknesses
Learn how to use sliders
An actual physics applet: see final output here.
Pong Applet
-
The classic game "pong" is recreated for 1 player, as an example of what can be done
Not implemented.