Java Tutorial Exercise 1 (Introduction To Java) : Topics: Java Libraries - Swing Event Driven Programming
Java Tutorial Exercise 1 (Introduction To Java) : Topics: Java Libraries - Swing Event Driven Programming
Goals: Upon successful completion of this tutorial you should be able to:
1. Write a simple event driven Java program using the Swing libraries
2. The remainder of this tutorial exercise should be executed on the local linux or DOS platforms.
You can store your directories and files in the local student directory (in which case you will need
to delete them at the end of the tutorial session) or on a flash drive. Completed exercises should
be uploaded to the server for submission using scp or ftp. Create a new directory called Tutorial1;
3. Enter the following Java code in a file called Tutorial1a.java in your Tutorial1 directory.
// file: Tutorial1a.java
import java.awt.*;
import javax.swing.*;
public Tutorial1a ()
{
hwX = 300;
hwY = 300;
helloWorld = "Hello World";
repaint();
}
You have now written your first Java program (for this course)!
// file: Tutorial1b.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public Tutorial1b ()
{
hwX = 300;
hwY = 300;
helloWorld = "Hello World";
red = green = blue = 30;
theButton = new JButton ("Change Color");
add (theButton);
theButton.addActionListener (this);
addMouseMotionListener(this);
repaint();
}
10. Modify the mouseExited and mouseEntered methods to prevent the user from dragging the
text outside the bounds of the window frame.
Goals: Upon successful completion of this tutorial you should be able to:
1. Create an array based list in a java program
2. Randomly populate a list of integers
3. Sort the list of integers using your simple and advanced sorts
Create a new subdirectory called Tutorial2 and enter the following java program as RList.java
// file: RList.java
import java.lang.*;
import java.util.Random;
public RList ( )
{
}
Random List 2
0 1 1 4 7 10 0 1 2 3
0 1 1 4 7 10 0 1 2 3
Note that the lists are not sorted…..
2. Modify the Sort method of the RList class to sort the list into ascending order using your simple sort
if the input parameter is ASCENDING and into descending order using your advanced sort if the
input parameter is DESCENDING.
3. This program uses an internal main function to test the RList class. The RList class can also be used
as part of another program. Enter, compile and run the following class called Tutorial2a.
// file: Tutorial2a.java
import java.lang.*;
public Tutorial2a ()
{
rlist1 = new RList (10);
rlist2 = new RList (rlist1);
System.out.println ("\nRandom List 1");
System.out.println (rlist1.toString());
rlist1.Sort (RList.ASCENDING);
System.out.println (rlist1.toString());
System.out.println ("\nRandom List 2");
System.out.println (rlist2.toString());
rlist2.Sort (RList.DESCENDING);
System.out.println (rlist2.toString());
}
Modify the constructor with integer input parameter S to generate a list populated with S randomly
selected integers in the range -100 ≤ list[i] ≤ 100.
Modify the default constructor to randomly select a value for size (5 ≤ size ≤ 50) and to generate a list
populated with size randomly selected integers in the range -100 ≤ list[i] ≤ 100.
Goals: Upon successful completion of this tutorial you should be able to:
1. Use Swing and Java graphics to illustrate the actions of your simple sorting algorithm
2. Use Swing and Java graphics to illustrate the actions of your advanced sorting algorithm
Create a new subdirectory called Tutorial3. Copy RList.java from Tutorial2 to Tutorial3. Add the
following Paint method to Rlist.
public void Paint (Graphics2D g2, int ulX, int ulY, int lrX, int lrY,
Color minColor, Color maxColor)
{
if (size < 1)
return;
int min = 0;
int max = 0;
for (int i = 1; i < size; i++)
{
if (list[i] < min) min = list[i];
if (list[i] > max) max = list[i];
}
int deltaX = (lrX - ulX) / (2 * size + 1);
int range = max - min + 1;
int deltaY = (lrY - ulY) / range;
int deltaR = (maxColor.getRed() - minColor.getRed()) / range;
int deltaG = (maxColor.getGreen() - minColor.getGreen()) / range;
int deltaB = (maxColor.getBlue() - minColor.getBlue()) / range;
for (int i = 0; i < size; i++)
{
int left = ulX + (2*i+1) * deltaX;
int right = left + deltaX;
int top = lrY - list[i] * deltaY;
int bottom = lrY;
Color c = new Color (list[i] * deltaG, list[i] * deltaR,
list[i] * deltaB);
g2.setPaint (c);
g2.fillRect (left, top, right-left, bottom-top);
}
}
This function is designed to draw vertical bars representing the values in the list using the rectangle and
colors passed to it. The parameters to this function are, in order, the x and y coordinates of the upper left
corner of the rectangle in which the vertical bars will be drawn; the x and y coordinates of the lower right
corner of the rectangle; the minimum and maximum colors. The width of the rectangular area is divided
so that ‘size’ bars can be drawn along a horizontal axis. The height of the rectangular area is divided
into units representing the range of the magnitudes of the values in the list. The color range is divided into
units representing the range of the magnitudes of the values in the list.
Currently this function does not handle negative integer values in the list.
// file:Tutorial3a.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
Test the simple sort function in your RList class. After the Sort List button is pressed, the list should be
displayed in fully sorted order. Your display might look like:
4. Modify the program so that each press of the “Sort List” button causes 1 pass of your simple sort to
be executed. Multiple presses of the button will illustrate the modifications made to the list that
eventually result in a fully sorted list. You will most likely need to add a “SortPass” method to your
RList class.
Your completed RList.java and Tutorial3a.java files should be placed in ~tiawatts/cs460drop. The files
should be called your-last-name.java and your-last-nameT3.java respectively.