GUI Java with EasyApp

We spent a couple weeks writing command-line (text-mode) programs.  This is the "old-fashioned" style of user-interfaces

Modern operating systems use graphics mode almost all the time.  This is called GUI - Graphical User Interface.  The operating system displays Windows, Icons (buttons), Menus, and a Pointer (mouse), so it is also called a WIMP system.

Java includes libraries of prewritten code that can display normal GUI elements - buttons, menues, lists, text boxes, graphics images, etc.  Unfortunately these GUI elements are rather difficult for beginners to use.  But you can use EasyApp to make it reasonably simple. 

EasyApp was written by Dave Mulkey to enable beginning programmers (especially high school students) to work easily in a modern, familiar GUI environment.  You can read more about EasyApp here:  http://ibcomp.fis.edu/java/EasyApp.html

The main thing you need to know about EasyApp is how to use it.

  1. Download a copy of EasyApp here :  http://ibcomp.fis.edu/Java/EasyApp.java
    Save it in a folder on the hard-disk.
     
  2. Write a program like the one below and save it in the same folder with EasyApp.
     
  3. Compile your program - that's all there is to it (you might need to compile EasyApp first if your editor doesn't do this automatically).

The program below displays a window with 2 buttons - 1 for converting from Euros to Dollars, and another for converting Euros to Pounds.

import java.awt.*;

public class Changer extends EasyApp
{
   public static void main(String[] args)
   {
      new Changer();
   }  

   Button dollars = addButton("Dollars",50,50,100,30,this);
   Button pounds  = addButton("Pounds",50,100,100,30,this);
   
   public void actions(Object source,String command)
   {
      if (source == dollars)
      {
         output("1 dollar = 0.75 euros");  
      }
      
      if (source == pounds)
      {
         double e = inputDouble("How many Euros do you have?");
         double p = e / 1.5;
         output("That converts to " + p + " pounds");
      }
      
   }
}

Notice the following about the program:

You can add as many buttons as you want - just remember to also add an if... command to respond.  And be careful with the coordinates, so the buttons don't overlap on the screen.

Practice

(1) Download EasyApp, then copy the Changer program (above) and get it running.

(2) Change the Dollars code so that it asks how many Euros you have, and then changes that many Euros to Dollars.

(3) Add another Button for changing from Euros to some other currency.  Use Google to find the correct exchange rate.  Remember that you must make a new Button and ALSO add an if... command in the actions method.

(4) Add a fourth button that inputs a price and then adds sales tax to it.  For example, in California if you buy a shirt for 20 dollars, the store adds 8% sales tax, so you must pay 20 + 1.60 = 21.60 .