SWING

 

Now that you have had a chance to introduce sequence and repetition in the “Easter” lab by teaching your students how Java uses multimedia it is time to get interactive with our applets.   Programmers like to interact with their programs because it allows them to trace and debug their code when learning a new programming language.  Fortunately, Java does provide us with the swing libraries to interface with our programs.

 

I have included the “Interactive GUI Demo” to demonstrate the use of javax.swing libraries.  You will note that the asterisk at the end of the statement means that all libraries are to be included (looks a lot like DOS to me).

 

Let’s study the code for this applet and make note of the additional libraries in this lab that are used to handle string methods (java.lang), graphics (java.awt) and interactive GUI (javax.swing) :

 

import java.applet.*;

import java.awt.*;

import java.awt.Graphics;

import java.awt.Color;

import javax.swing.*;

 

import java.lang.*;

 

 

 

 

 

 

public class Intermybatting extends Applet

{

 

//We need a global graphics object if we use functions which use graphics:

             

// *************   Global scope variables  -  ouput to browser  ************

 

       public Graphics g;

       public static String playername;

              // set variable for interactive use

             final static float hits = 56;

             public static float atbats = 99;

      

       //   - you can make the lab more interactive by getting user to input hits and atbats

       // float hits = Float.parseFloat(stringhits);

      

      

       public static float batave;

      

 

 

 

           

            public void init()

            {

                       

                       

        

            /*****************************************************************

             *

             * GUI Screen Output

             *

             *****************************************************************/

           

            JOptionPane.showMessageDialog (null, "GUI Greetings from the Batting Average program!");

                          

          

      // String aString;

            playername = JOptionPane.showInputDialog ("Your name, please:");

           

           

            String age;

            age = JOptionPane.showInputDialog("Your age, please: ");

            int newage =Integer.parseInt(age);

            if (newage > 15)

            {

                        System.out.println("You are one cool dude");

            }

            else

            {

                        JOptionPane.showMessageDialog (null, "Haa Haa, " + playername + " you are only " + age + "!");

            }

 

                       

            String username = playername;

           

            if (username.equals("Santa"))

            {

                        System.out.println("Hoo Hoo Hoo");

                        }

       

                                                           

        // calculate batting average

        batave = (hits / atbats);  // batave calc not working from here

                                              // in spite of global declaration

       

               if (batave > .300)

          {

               System.out.println("Good Hitter " + playername + "!  Your average is " + batave);

           }

         else

          {

              System.out.println("You need work " + playername);

          }

         

        

         

        System.out.println("Let's play ball");

       

      

    }  // end init()

   

   

    public void paint (Graphics g)

            {

                g.setFont(new Font("Helvetica",Font.PLAIN,16));

 

                g.setColor(Color.red);

                g.drawString(playername,100,100);

                

                g.drawString("Batter has " + hits + " hits",100, 120);

                

                g.drawString(playername + " has " + atbats + " atbats", 100, 140);

 

                batave = (hits / atbats);  // calc works when moved to here

               g.drawString("The batting average is " + batave, 100, 160);

           

           

           

           

           

                        System.exit(0);

            

                            

}

             

   

} // end of Intermybatting class

 

 

 

 

I created this lab to encourage students and myself to get familiar with syntax in Java.  This program features structured programming techniques and object-based programming.  Once the student is comfortable with syntax then he or she can begin to explore OOP qualities and coding techniques.

 

 

The user of the tutorial will note that there is a difference between the output that is sent to the browser or AppletViewer canvas and the DOS window running in the background.  It is critical that the student know the difference between output in an application and an applet.   A Java application will usually display output in a DOS window and the applet will display its output to the browser. 

 

The swing library can be useful for creating interactive applets that can be quite entertaining to a Java newbie.

 

In the next part of the tutorial I will write a bit more about using audio and image files in applets.