Java Applets: Interactive Programming

Lab/Assignments


The purpose of this assignment is


Instructions:


Objectives:

You're going to take the skeleton of a bar graph program and add the necessary code to get it look like the following image:

This is a bit of a bigger program, so it's important to break it down into small steps.
Get one of the steps to work, then move on to the next. Don't try to do everything at once! It'll take you MUCH longer!

  1. To start out, type in the following program and run it.
    Yes, you must type in the comments as well!
    import javax.swing.*;
    import java.awt.*;
    
    /** Lab on drawing with Graphics methods
     */
    public class BarGraph extends JApplet
    {
    	// define variables
    	int aValue = 97;
    	int bValue = 47;
    	int cValue = 38;
    	int dValue = 80;
    	
    	public void paint(Graphics g)
    	{
    		// a value
    		// set the color to magenta
    
    		// draw a 4*aValue by 40  pixel colored bar
    		//      down 20 pixels from the top of the graph in magenta
    		g.fillRect(40,70,4*aValue,40);
    		
    		
    		// b value
    		// set the color to green
    
    		// draw a 4*bValue by 40 pixel colored bar
    		//      down 20 pixels from the top of the graph in green
    		
    
    		// c value
    		// set the color to yellow
    
    		// draw a 4*cValue by 40 pixel colored bar
    		//      down 20 pixels from the top of the graph in yellow
    		
    
    		// d value
    		// set the color to red
    
    		// draw a 4*dValue by 40  pixel colored bar
    		//      down 20 pixels from the top of the graph in red
    		
    		// reset color to black
    
    		// draw outline of graph
    		g.drawRect(40,40,400,300);	// height is 250
    	}
    }
    
  2. set the background color to cyan
    		// set the background color to cyan
    
    Hint: fill in a big rectangle the size of the applet
    Compile and run the program again.
  3. Calculate what the x and y coordinates and width and height of each of the bars should be. Add the code to display the bars.
  4. Now let's add the title that goes above the graph. Find a place in the code to add the title, but make sure you write it in black!
    		// write title above graph, replacing Cookie Monster with your name
    
  5. At the end of the program, after you draw the outline of the graph, add the following comments.
    
    		// write "a" to the left of the bar representing aValue
    		
    		// write "b" to the left of the bar representing bValue
    	
    		// write "c" to the left of the bar representing cValue
    		
    		// write "d" to the left of the bar representing dValue
    
    Now figure out what the x and y coordinates need to be to get the "a" to print in the correct location. Work only on "a" first, until you are happy with it. Then try to get the "b" to print, then "c", then "d".

    Notice that one of your coordinates doesn't change! Which is it?


Copyright © 2006-2007: E.S.Boese All rights reserved.