Java In Chapter 2

        Here are some of the details of the java code used in the chapter 2 java programs. Most of the code that runs the actual simulations is very similar to the c code that runs the same simulation, so i will only talk about the differences here.

        First of all, in the applets i make i usually use a GridBagLayout, which can be a little confusing. Basically this just allows me to place the various components on the page however i want to. The line that says "setLayout(new GridBagLayout())" just tells the applet that i will be using a gridbaglayout. When i actually add the components i add both the component itself and the constraints that tell the browser where to place the component. I have made a method called "SetupGBC" that works as a short cut so that i can easily add more components. The parameter of the SetupGBC method is an array of numbers that indicate the specific position of the component.

...
    setLayout( new GridBagLayout());
    add(component, SetupGBC(...) );
...

        When i graph the data from a run of one of the simulations, i use a component called a Canvas. This allows me to draw points and lines in order to create a normal coordinate axis system or a postscript-like picture relatively easily. As with an applet, i make a class that "extends" the Canvas class. This tells the browser to make a generic canvas but allows me to work with it as i want to. In all of the canvas classes that i make there is a method called "paint." This is the method that the browser automatically calls when the canvas needs to be drawn. It will always looks something like this:

...
    public void paint (Graphics g)
    {
        g.setColor(...);
        g.drawLine(...);
    }
...

        The Graphics object is what is used to do the actual work, so that saying "g.setColor()" will set the current color and "g.drawLine()" draws a line on the canvas. For all of the things that you can do with a Graphics object take a look at the documentation for it (the link is at the bottom of this page). Most (if not all) of the graphics classes i use can be found in the "java.awt" package, which is why i have to import it at the beginning of each class i make.

        The class that actually runs the simulation (usually titled the same as the simulation, for example, the bd sim is in bd.java) is fairly close to the c version, except that i have added methods to make the class into an object and deal with transferring data around so that i can display it. The simulation classes therefore have a constructor and a method usually called "Start," which is called from the applet and runs the simulation. The output is piped directly to the text area that will display it. The text area object (usually called 'display') uses a method called "append" to print strings and numbers.

...
    TextArea Display = new TextArea(...);
    Display.append("Hello the time is:" + 9 + "o'clock");
...

        The entire parameter of the append method is a string, here composed of a series of strings and numbers added together with the + operator.

        Sometimes doubles or ints need to be printed with specific precision, such as if i wanted to print 0.0500 with exactly that many zeroes. Normally, just appending the number wouldn't do that (it would print 0.05), so i have made a class that will format the numbers properly. It it called "NumFix.java" and is used in almost every one of my programs. Unlike the applets and simulation classes, the NumFix class has "static" in front of all its variables and methods. What this means is that instead of creating an object and using that object to call the methods of the class, i can just call the methods directly. Calling the "Round" method i would say "NumFix.Round(...)" with the numbers i want. The method you will see most frequently is the "Output" method, which formats the numbers and outputs them directly. From the simulation class, it would be used like this:

...
    TextArea Display = new TextArea(...);
    NumFix.Output(Display, new double[] { x, total }, 6, 2);
...

        The line looks complicated, but basically all it does is prints out two variables, the doubles 'x' and 'total,' with two white spaces to the left of the decimal and six places to the right of the decimal.



java class documentation