PreAP Computer Science / Graphics Lab 07
Practice/Perform Major Assignment
The Graphics Array / 80, 90, 100 & 110 Point Versions
Assignment Purpose:
The purpose of this program is to demonstrate knowledge of graphically representing the information stored within an array.

For this lab assignment you need to create an array of random numbers between 10 and 640. Then you will graphically display the array. Each number in the array is represented by a red vertical bar. Bigger numbers will have taller bars. Smaller numbers will have shorter bars.

This assignment will also help prepare you for Graphics Lab 8, in which you will not only display a random array graphically; you will also sort it graphically. But that comes later. Right now focus on this lab.

GraphicsLab07 Student Version / Do not copy this file, which is provided.
// GraphicsLab07st.java
// The Graphics Array
// This is the student, starting file for Graphics Lab 07.
import javax.swing.*;
import java.awt.*;
import java.applet.*;
public class GraphicsLab07st extends Applet
{
private int numBars; // number of bars to be displayed
int barHeight[]; // array of bar heights
int swapDelay; // The delay between the original display and the second display.
int bar1, bar2; // Indexes of 2 bars to be swapped.
public void init()
{
numBars = enterIntGUI("How many bars will be displayed? {1-1000}");
bar1 = enterIntGUI("What is the index of the 1st bar to be swapped? {0-"+(numBars-1)+"}");
bar2 = enterIntGUI("What is the index of the 2nd bar to be swapped? {0-"+(numBars-1)+"}");
swapDelay = 3000;
barHeight = new int[numBars];
getBarValues();
}
public void getBarValues()
{
}
public void paint(Graphics g)
{
displayBars(g);
Expo.delay(swapDelay);
swapBars(bar1,bar2);
Expo.setBackground(g,Expo.white);
displayBars(g);
}
public static int enterIntGUI(String prompt)
// Allows GUI keyboard input of an integer in a graphics program.
{
String tempString = JOptionPane.showInputDialog(prompt);
int temp = Integer.parseInt(tempString);
return temp;
}
public void displayBars(Graphics g)
{
}
private void swapBars(int x, int y)
{
}
}

Program Input

The input is handled by the init and enterIntGUI methods, both of which are provided for you.

All versions of this program have the same input. You will enter 1000, followed by 250 and 750.

80 Point Version Specifics

The 80 point version requires you to write both the getBarValues and the displayBars methods. getBarValues needs to traverse the newly created array and assign a random integer between 10 and 640 to every element in the array. displayBars needs to draw a red vertical line on the screen for each and every integer in the array. Remember, lines are created from 2 coordinate points. Each coordinate point has a horizontal value (x value) and a vertical value (y value). The x values of these vertical lines is based on the index of each integer. The first y value is 0 and the second is the actual int value that is stored at that particular index in the array. This will cause all of the vertical bars to “hang down” from the top of the screen.

NOTE: For the 80 and 90 point versions, the 2nd and 3rd input values are not used. After the array is displayed for 3 seconds, the screen will flash and the same array is display again.

80 Point Version Output

90 Point Version Specifics

The 90 point version is VERY similar to the 80 point version with one difference. In the displayBars method, the red vertical lines need to start at the BOTTOM of the screen and be drawn UP. Make sure you are properly representing the data! For example, if you are storing a value of 10 at index 742, you might be tempted to draw the line from point (742,650) to (742,10). This would be very wrong because the line would be almost as tall as the screen, when it is suppose to be a very short line, only 10 pixels tall. You will have to find a way to deal with that.

Your instructor will check your code to make sure your graphics display properly represents the values in the array.

NOTE: For the 80 and 90 point versions, the 2nd and 3rd input values are not used. After the array is displayed for 3 seconds, the screen will flash and the same array is display again.

90 Point Version Output

100 Point Version Specifics

The 100 point version requires everything from the 90 point version. After that you need to write the swapBars method which will swap 2 values in the array. The indexes of these values are the 2nd and 3rd number you entered at the beginning of the program and are stored in variables bar1 and bar2. You also need to add some code to the displayBars method. After all the values in the array are displayed as red bars, the values of bar1 and bar2 need to be displayed again as blue bars. This will make those 2 values stick out. Then after a 3 second delay, and after the swapBars method is called the displayBars method is called again which re-displays the array. If done properly the 2 blue bars will now have switched places.

100 Point Version Starting Output

↑ Here are the 2 blue lines at indexes 250 750. ↑

100 Point Version Ending Output

↑ After the swap, the lines have switched places. ↑


110 Point Version Specifics

For the 110 Point Version, we want bars that actually look like bars and not mere lines. The bars should be 10 pixels thick and there should be another 10 pixels of space separating the bars. When you run the program, specify 47 bars, and that you will swap the bars at indexes 15 and 30.

110 Point Version Starting Output

↑ ↑

Here are the 2 blue bars at indexes 15 & 30.


110 Point Version Ending Output

↑ ↑

After the swap, the bars have switched places

Exposure Java 2013, PreAPCS Edition Graphics Lab 07 Page 1 05-22-13