Computer Science 120
Introduction to Programming

Spring 2012, Siena College

MakeOvals Demo

A working demo of MakeOvals will appear below. Click inside the applet to interact with it.



MakeOvals BlueJ Project

Click here to download a BlueJ project for MakeOvals.


MakeOvals Source Code

The Java source code for MakeOvals is below. Click on a file name to download it.


MakeOvals.java

import objectdraw.*;

import java.awt.*;

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.util.StringTokenizer;



/**

 * MakeOvals example demonstrating the use of a 

 * FileReader/BufferedReader for reading an input

 * file and a StringTokenizer to break it down

 * 

 * Primary author: Sharon Small

 * Modified by: Jim Teresco

 * CSIS 120, Siena College Spring 2012

 * 

 * $Id$

 */

public class MakeOvals extends WindowController {



    // when the mouse is clicked, read a file OvalInput.txt

    // that contains lines that each consist of 4 numbers

    // and a string.  The numbers should be x, y, width, height

    // of a FilledOval to be drawn, and the string should be

    // its color (one of "red", "green", or "blue").

    public void onMouseClick(Location point){



        try {

            // a combination of a FileReader and a BufferedReader

            // is an alternative mechanism you may see for reading

            // files in Java.

            FileReader fr = new FileReader("OvalInput.txt");

            BufferedReader bfr = new BufferedReader(fr);

            String inputLine;

            

            // the BufferedReader's readLine method will return

            // the next line of input if one exists, null if

            // the end of the input has been encountered.

            while((inputLine = bfr.readLine()) != null){

                //System.out.println(inputLine);

                

                // the input String needs to be broken into the 5

                // components that make it up so they can be passed

                // to the FilledOval constructor.  StringTokenizer

                // is a mechanism that can do this.

                

                // This constructs a tokenizer that will return

                // parts of inputLine that are separated by

                // a string consisting of a space

                StringTokenizer st = new StringTokenizer(inputLine, " ");



                // now, we can get each successive String out of the

                // tokenizer by calling nextToken.  This gives a String,

                // which we need as an int.  Passing this String to

                // the constructor of the Integer is another way to

                // convert this String into an appropriate Integer.

                Integer x = new Integer(st.nextToken());

                Integer y = new Integer(st.nextToken());

                Integer width = new Integer(st.nextToken());

                Integer height = new Integer(st.nextToken());

                FilledOval inputOval = new FilledOval(x,y,width,height,canvas);

                setOvalColor(st.nextToken(), inputOval);



            }

            bfr.close();

        }

        catch(IOException ioe){

            System.out.println("Problem opening input file: " + ioe);

        }



    }



    public void setOvalColor(String colorInput, FilledOval inputOval){

        if (colorInput.equals("red")){

            inputOval.setColor(Color.red);

        }

        else if (colorInput.equals("green")){

            inputOval.setColor(Color.green);

        }

        else if (colorInput.equals("blue")){

            inputOval.setColor(Color.blue);

        }



    }



}