Dilip's Java Applets



 

Here, you will find a list of applets that I have written (from scratch) using the Java programming language.  So far, they're pretty simple and unsophisticated because I'm still a beginner, but I'd like to think that I'm improving with time.  I've only been learning how to program since the first week of February (2011) so there are still many concepts that I have not yet fully understood and a whole ocean of tools and techniques that I have barely scratched the surface of.  Here are some of the things that I am currently familiar with:

- Declaring and assigning values to variables of primitive type like int, long, double, boolean, and char.
- Using basic control-flow structures like
while loops, for loops, and if statements (including nested combinations of such structures).
- Using basic relational and logical operators like
<, >, ==, %, &&, and ||.
- Working with variables of type String.
- Using static member functions from the standard class Math.
- Defining methods by specifying the return type and formal parameters and calling them from elsewhere in the program.
- Defining classes that specify instance variables, instance methods, and constructors for use in the creation of objects.
- Creating and destroying objects using the
new operator and manipulating the data stored in their instance variables.
- Using the
this command in a class definition.
- Defining a class that implements a given interface.
- Defining a class that extends another class.
- Creating a simple Graphical User Interface
(GUI) by extending JPanel, overriding its paintComponent() method, and then adding it to a JFrame object.
- Drawing simple geometric shapes using methods from the standard class Graphics.
- Manipulating colors by creating objects of type Color.
- Handling events generated by a mouse, keyboard, or Timer object.
- Working with basic Swing components like JButton, JMenu, and JTextField (with or without a LayoutManager).
- Declaring and initializing arrays of objects using the
new operator.
- Processing and manipulating the data stored in arrays by using
for
loops.
- Creating executable JAR files.
- Converting a stand-alone application into an applet for use in an HTML webpage.

Keep in mind that I do not fully understand most of the tools and concepts that I have listed above (especially those related to GUI programming).  I only know the very basicsI'm still not comfortable with the techniques of object-oriented programming.  I discuss some of my difficulties below.  I use GUI as my main training ground since it tends to require a strong object-oriented paradigm.  My main study resource is the free online textbook Introduction to Programming Using Java by David Eck.  I chose this book because it is very easy to read for an absolute beginner.  Unlike other programming textbooks, it introduces the reader to procedural programming techniques first before talking about objects and classes.  This approach has both advantages and disadvantages but I have found it pretty useful so far.  All of the applets that I have listed here are GUIs and not all of them are intended to be interactive (some of them just display an image).  I have included the complete source code under each applet for your reference.  If you would like to compile and run these applets as stand-alone applications from your command-prompt window, simply copy and paste the entire code into a text editor and rename the file as class-name.java (where class-name is the name given to the primary class of the program).

Please Note:  Some of these applets have bugs
in them that I have not been able to fix so far.


1. Numbers 1.0 
2. Calculator 2.0 

3. Moving Ball
4. Sub Destroyer

5. Multiple Falling Balls
6. Paint Brush
7. Text Typer
8. Colors and Fonts
9. Shape Drawer
10. Square Mover
11. Pair of Dice
12. Mandelbrot Set
13. ColorChanger 2.0
14. ColorChanger 1.0


Command-Line Programs

Although GUI programs can get pretty sophisticated and require a great deal of design and creativity, I originally started programming in Java using only command-line interfaces (CLIs).  Command-line programs helped me acquire the basic skills that I needed in order to be able to move on to GUIs.  Although command-line programs are not as fancy, they allowed me to gain more experience with problem-solving and algorithm development.  My initial programs focused mainly on solving mathematical or computational tasks and sorting algorithms.  I also tried to solve some of the simpler Project Euler problems.  Anyways, here are a list of some CLI programs that I have created in the past:

1. Sum Digits (My first program!)
2. Alphabetize
3. Square Root
4. Project Euler Problem # 1
5. Project Euler Problem # 2
6. Project Euler Problem # 5
7. Project Euler Problem # 8
8. Project Euler Problem # 12
9. Project Euler Problem # 14
10. Dilip (Collection of methods)


Programming Issues

As I continue to learn how to program using Java, I try to keep track of some of the difficulties that I experience along the way.  So far, my main issues are related to GUI programming.  Here is a short discussion:

Static vs. Non-Static

My main issue with respect to the static and non-static modifiers in a program is variable access.  I still don't fully understand how this works.  Which variables ought to be declared static and which ought to be declared non-static?  Well, I know that any variables that are declared within a class that is to be used specifically for the construction of objects have to be declared non-static.  However, what about variables that refer to major GUI components like JPanel and JFrame?  I noticed that, while using event-handlers, only information about the source of the event is avaliable for use within the event-handling methods.  How do we call the repaint() method of other components from within these methods without using static variable declarations for those components?

Another issue that I have encountered in this context is regarding nested classes.  I understand that there are two kinds of nested classes:  static and non-static.  Non-static nested classes are also known as "inner classes".  Apparently, inner classes can even be defined inside the main() method of a program in order to avoid some of the variable access problems that I talked about above.  I don't know how this works exactly.  I often get an error message that reads "non-static variable this cannot be referenced from a static context" while using nested classes.

The repaint() problem

I noticed that the repaint() method behaves in a strange and unpredictable way sometimes.  For instance, I noticed that for some reason a call to the repaint() method is always executed last, regardless of where the call is made within a set of instructions.  So, even if a call to this method is made before the values of certain instance variables have been changed, the method is only called after the changes have been made.  This can get pretty annoying sometimes.  It became a serious problem in a calculator applet that I was trying to create a short while ago.  While I was trying to implement a response (in the mousePressed() method) to when the user clicked on the "=" sign, I wanted the program to display the result of the computation on the screen by calling the repaint() method before resetting the value on the display area to "0" for the next computation.  However, the repaint() method was only called after the value was set to "0" which meant that the result would never be seen.  Also, more than one repaint() call within the same set of instructions didn't make a difference either.  All repaint() calls were executed last.  However, it is interesting to note that this strange behavior occurs only within the body of a particular event-handling method.  It happens anytime the computer has to execute a sequence of instructions which contain repaint() statements.

The Swing Timer problem

I also noticed that the Swing Timer behaves in odd ways.  In my programs I use a Timer object to drive all my animations.   However, I observed that the timekeeping is rarely consistent.  There is a great deal of variation depending on the number of timers used, the number of ActionListener objects registered with those timers, as well as the actual computer system used to run the program.  Consider my MultipleFallingBalls applet for instance.  I have a 64-bit operating system in which the balls move at a normal pace but when I run the same applet on a 32-bit system, the movement is way too fast to follow with a mouse cursor.  Now, if you study the code, you will notice that each Ball object is equipped with its own timer.  However, all of these timers are registered with a single ActionListener object which is defined outside the Ball class.  The more balls that are created and set into motion, the faster they move.  I think this is because the actionPerformed() method is called multiple times in each time interval which results in a ball's motion() method being called more times than necessary.  I tried to fix this problem by equipping each Ball object with its own ActionListener, but that actually had the opposite effect.  There was an actual time delay present in this case.