Java Assignment
Bacon/.DS_Store
__MACOSX/Bacon/._.DS_Store
Bacon/assign06.pdf
Software Design III (CS 340)
Note: For this assignment only, I am relaxing the late-policy rules. Assignments can be handed in without penalty until 9:00 PM, Friday, 16 May 2014 (the end of exam period). Assignments will not be accepted after that point in time.
This assignment will have you solve a well-known problem concerning the “proximity” of actors, measured in terms of whether they have co-starred together, or are connected more indirectly. Made famous as the “Kevin Bacon Number,” the measure works this way: Kevin Bacon has a proximity of 0 to himself. Kevin Bacon has a proximity of 1 to Donald Sutherland, since they co-starred together in Animal House. Kevin Bacon has a proximity of 2 to Daniel Radcliffe, since they never actually co-starred together, but Kevin Bacon co-starred with Helena Bonham Carter in Novocaine, and Helena Bonham Carter co-starred with Daniel Radcliffe in Harry Potter and the Deathly Hallows. More information can be found at The Oracle of Bacon:
http://oracleofbacon.org/
Your code will provide this sort of functionality, allowing a user to read in a database file of actors and their roles, then find a set of distinct shortest paths between those actors (if any such exist). Up to 10 different, disjoint paths can be found at a time (where “disjoint” here means that they have no actors in common, either than the start and end actors in the path; we do not worry about whether or not the paths have some movies in common).
Another note: To help answer questions about what the code should be doing, I have posted some short video demos of it running, with some more description.
http://screencast.com/t/GSMgo6iq62mz http://screencast.com/t/sY37l0MU49a
(a) (10 points) Your first job is to implement the Grapher() constructor. When the user chooses an input text-file, this constructor is called, taking the name of the file as an input, along with a reference to a JTextArea for output as in some prior assignments. The constructor should:
• Read in the text-file chosen by the user. Each line will consist of an actor’s name, followed by the ‘|’ character, followed by a movie (with date, so remakes and similar things are not confused). The set of downloads includes one small file for testing, and two larger files, consisting of all the data for films taken from IMDB (excluding actors/films that have irregular characters in their names), and the same data, restricted to the period starting in 2000, available for download and testing online:
1
http://cs.uwlax.edu/~mallen/courses/cs340/baconInputs.zip
• You need to store the data into whatever structures you need; for the remainder of the assignment, you will want to be able to look up all the movies that any actor starred in, as well as all the actors that starred in any movie. You may want to use, for instance, a pair of hash-maps here, but that is up to you.
• When the data is finished parsing, you should display the number of actors and number of movies stored in the input file, putting that information into the output text-area. (See the video and supplied sample runs of the program to see the format required.)
Note: For the large input files, you may find it necessary to increase the heap space given to the JVM. This is most easily done by adding the necessary arguments when running the compiled program from the command line. The first video linked above covers the basic command needed. For more information, see:
http://preview.tinyurl.com/mwe4e39
(b) (20 points) You also need to fill in the method that finds the shortest path from one actor to another in the data. The best way to implement this is to use something like the shortest-path search seen in the text and in class for graphs. Your search should start with the initial actor, and then see if the second actor appears in any films with them; if so, the path length is 1. If not, the search will need to take all of the actors who appear in the films with the first actor, and see if the second one appears in any films with any of them, and so forth. When a path is found, you should report back to the user the length of that path, along with the movies and other actors involved. (Again, see the videos and sample runs for format.)
Note 01: Your code will need to deal with cases where there are no paths between actors (it can sometimes happen for very obscure individuals), as well as dealing with cases where the person entered is not in the database (due to a misspelling or other issue).
Note 02: In class, we looked at graph search in an explicit form. That is, the data being searched was placed into a complete graph of nodes, and then a search was performed. For this assignment, you can use the same basic idea of the breadth-first search, but you probably do not want to build the entire graph. Particularly on large data-sets, this can be extremely slow, since it will involve building a graph out of every individual in the database, when the search will really only need to involve some small subset of individuals in order to find a shortest path from one to another. You are strongly advised to think about how to implement the search you need without explicitly building the entire graph of individuals first.
(c) (10 points) To finish the project, you should add code so that, if the user enters some value n greater than 1 in the box next to the path-search button, your program returns n distinct paths, shortest to longest, between the two actors. Each path should be disjoint in the sense that no actor (other than the two we are searching for) appears in more than one of those paths, so we have n different ways to get from the first actor to the second. Your code should limit the number searched for to a maximum of 10; if a user enters a value greater than 10, you should politely ignore them, and return 10 paths instead. You will also need to deal with cases where there are less than n paths possible, even under the cap. (Again, the videos and the sample runs provided show the sort of behavior expected of your program.)
2
(d) When you are done, submit your work on the class Moodle site. You should submit an archived folder containing just your source code.
https://moodle.cs.uwlax.edu/course/view.php?id=14
Coding conventions: Each step of the program has a points total, given above. For full points, you should complete all those components, and observe the basic coding conventions as follows:
• Comments should appear at the start of any code-file you write or change, with your name, the date of your work, and a short explanation of what the code does.
• Each method should be preceded by a blank line, followed by at least one line of comments explaining what the method does.
• Methods should be private if possible, and public only if necessary. • Class variables should be local to methods if possible, and should only appear as global
variables if they appear in more than one method of the class. • Unless absolutely necessary, global instance variables should be private. • Code can be broken up by blank lines, but keep this to a low level. There should be no more
than a single blank line separating pieces of code. Within a line of code, white space should not be too wide, for clarity.
• Code should be properly indented and separated into lines. • Basic naming conventions should be followed: variables should begin with a lower-case letter,
while classes begin in upper case; method names (other than constructors) should also start in lower-case.
3
__MACOSX/Bacon/._assign06.pdf
Bacon/sampleRuns/.DS_Store
__MACOSX/Bacon/sampleRuns/._.DS_Store
Bacon/sampleRuns/imdb_notv_testRun.txt
Actors in database: 1767749 Movies in database: 488340 ------------------------------------------------------- Path 1 between KEVIN BACON and ZZ TOP has length 2: KEVIN BACON appreared in TURN IT UP! (2013) with JOHN 5. JOHN 5 appreared in ROCK PROPHECIES (2009) with ZZ TOP. Path 2 between KEVIN BACON and ZZ TOP has length 2: KEVIN BACON appreared in APOLLO 13 (1995) with JIM MESKIMEN. JIM MESKIMEN appreared in BEING W (2008) with ZZ TOP. Path 3 between KEVIN BACON and ZZ TOP has length 2: KEVIN BACON appreared in TURN IT UP! (2013) with STEVE LUKATHER. STEVE LUKATHER appreared in ROCK PROPHECIES (2009) with ZZ TOP. Path 4 between KEVIN BACON and ZZ TOP has length 2: KEVIN BACON appreared in SKUM ROCKS! (2013) with ORIANTHI. ORIANTHI appreared in ROCK PROPHECIES (2009) with ZZ TOP. Path 5 between KEVIN BACON and ZZ TOP has length 2: KEVIN BACON appreared in ANIMAL HOUSE (1978) with ROBERT CRAY. ROBERT CRAY appreared in ROCK PROPHECIES (2009) with ZZ TOP. Path 6 between KEVIN BACON and ZZ TOP has length 2: KEVIN BACON appreared in TURN IT UP! (2013) with SLASH. SLASH appreared in ROCK PROPHECIES (2009) with ZZ TOP. Path 7 between KEVIN BACON and ZZ TOP has length 2: KEVIN BACON appreared in SKUM ROCKS! (2013) with RICK NIELSEN. RICK NIELSEN appreared in ROCK PROPHECIES (2009) with ZZ TOP. Path 8 between KEVIN BACON and ZZ TOP has length 3: KEVIN BACON appreared in SUNDANCE SKIPPY (2010) with ROBERT REDFORD. ROBERT REDFORD appreared in THE UNFORESEEN (2007) with GEORGE W. BUSH. GEORGE W. BUSH appreared in BEING W (2008) with ZZ TOP. Path 9 between KEVIN BACON and ZZ TOP has length 3: KEVIN BACON appreared in JFK (1991) with LYNDON JOHNSON. LYNDON JOHNSON appreared in REAGAN (2011) with ARNOLD SCHWARZENEGGER. ARNOLD SCHWARZENEGGER appreared in BEING W (2008) with ZZ TOP. Path 10 between KEVIN BACON and ZZ TOP has length 3: KEVIN BACON appreared in QUICKSILVER (1986) with JAMI GERTZ. JAMI GERTZ appreared in CROSSROADS (1986) with STEVE VAI. STEVE VAI appreared in ROCK PROPHECIES (2009) with ZZ TOP. ------------------------------------------------------- Path 1 between KEVIN BACON and WINONA RYDER has length 2: KEVIN BACON appreared in PICTURE PERFECT (1997) with BARRY DEL SHERMAN. BARRY DEL SHERMAN appreared in REALITY BITES (1994) with WINONA RYDER. Path 2 between KEVIN BACON and WINONA RYDER has length 2: KEVIN BACON appreared in PLANES, TRAINS & AUTOMOBILES (1987) with LAILA ROBINS. LAILA ROBINS appreared in WELCOME HOME, ROXY CARMICHAEL (1990) with WINONA RYDER. Path 3 between KEVIN BACON and WINONA RYDER has length 2: KEVIN BACON appreared in QUEENS LOGIC (1991) with TOM WAITS. TOM WAITS appreared in DRACULA (1992) with WINONA RYDER. Path 4 between KEVIN BACON and WINONA RYDER has length 2: KEVIN BACON appreared in PICTURE PERFECT (1997) with JOEY SONTZ. JOEY SONTZ appreared in THE TEN (2007) with WINONA RYDER. Path 5 between KEVIN BACON and WINONA RYDER has length 2: KEVIN BACON appreared in SHE'S HAVING A BABY (1988) with HOLLAND TAYLOR. HOLLAND TAYLOR appreared in HOW TO MAKE AN AMERICAN QUILT (1995) with WINONA RYDER. Path 6 between KEVIN BACON and WINONA RYDER has length 2: KEVIN BACON appreared in CRAZY, STUPID, LOVE. (2011) with REGGIE LEE. REGGIE LEE appreared in STAR TREK (2009) with WINONA RYDER. Path 7 between KEVIN BACON and WINONA RYDER has length 2: KEVIN BACON appreared in MY DOG SKIP (2000) with EMILY D. HALEY. EMILY D. HALEY appreared in HOMEFRONT (2013) with WINONA RYDER. Path 8 between KEVIN BACON and WINONA RYDER has length 2: KEVIN BACON appreared in SHE'S HAVING A BABY (1988) with JOANNA KERNS. JOANNA KERNS appreared in GIRL, INTERRUPTED (1999) with WINONA RYDER. Path 9 between KEVIN BACON and WINONA RYDER has length 2: KEVIN BACON appreared in IN THE CUT (2003) with THEO KOGAN. THEO KOGAN appreared in ZOOLANDER (2001) with WINONA RYDER. Path 10 between KEVIN BACON and WINONA RYDER has length 2: KEVIN BACON appreared in STARTING OVER (1979) with MARY CATHERINE WRIGHT. MARY CATHERINE WRIGHT appreared in CELEBRITY (1998) with WINONA RYDER. ------------------------------------------------------- Path 1 between KEVIN BACON and YMA SUMAC has length 3: KEVIN BACON appreared in JFK (1991) with GEORGE R. ROBERTSON. GEORGE R. ROBERTSON appreared in AIRPORT (1970) with MARION ROSS. MARION ROSS appreared in SECRET OF THE INCAS (1954) with YMA SUMAC. Path 2 between KEVIN BACON and YMA SUMAC has length 3: KEVIN BACON appreared in JFK (1991) with LYNDON JOHNSON. LYNDON JOHNSON appreared in ELVIS FOUND ALIVE (2012) with DEBRA PAGET. DEBRA PAGET appreared in OMAR KHAYYAM (1957) with YMA SUMAC. Path 3 between KEVIN BACON and YMA SUMAC has length 3: KEVIN BACON appreared in GOING TO PIECES: THE RISE AND FALL OF THE SLASHER FILM (2006) with ANDREW BRYNIARSKI. ANDREW BRYNIARSKI appreared in ANY GIVEN SUNDAY (1999) with CHARLTON HESTON. CHARLTON HESTON appreared in SECRET OF THE INCAS (1954) with YMA SUMAC. Path 4 between KEVIN BACON and YMA SUMAC has length 3: KEVIN BACON appreared in APOLLO 13 (1995) with ROGER CORMAN. ROGER CORMAN appreared in CORMAN'S WORLD: EXPLOITS OF A HOLLYWOOD REBEL (2011) with JOAN TAYLOR. JOAN TAYLOR appreared in OMAR KHAYYAM (1957) with YMA SUMAC. Path 5 between KEVIN BACON and YMA SUMAC has length 3: KEVIN BACON appreared in THE BIG PICTURE (1989) with JUNE LOCKHART. JUNE LOCKHART appreared in SERGEANT YORK (1941) with ROGER CREED. ROGER CREED appreared in OMAR KHAYYAM (1957) with YMA SUMAC. Path 6 between KEVIN BACON and YMA SUMAC has length 3: KEVIN BACON appreared in QUEENS LOGIC (1991) with TOM WAITS. TOM WAITS appreared in THE TWO JAKES (1990) with PERRY LOPEZ. PERRY LOPEZ appreared in OMAR KHAYYAM (1957) with YMA SUMAC. Path 7 between KEVIN BACON and YMA SUMAC has length 3: KEVIN BACON appreared in HERO AT LARGE (1980) with GERRY BLACK. GERRY BLACK appreared in VACATION (1983) with FRITZ FORD. FRITZ FORD appreared in SECRET OF THE INCAS (1954) with YMA SUMAC. Path 8 between KEVIN BACON and YMA SUMAC has length 3: KEVIN BACON appreared in GOING TO PIECES: THE RISE AND FALL OF THE SLASHER FILM (2006) with DOMINICK BRASCIA. DOMINICK BRASCIA appreared in THEY'RE PLAYING WITH FIRE (1984) with ALVY MOORE. ALVY MOORE appreared in SECRET OF THE INCAS (1954) with YMA SUMAC. Path 9 between KEVIN BACON and YMA SUMAC has length 3: KEVIN BACON appreared in GOING TO PIECES: THE RISE AND FALL OF THE SLASHER FILM (2006) with CHARLES CYPHERS. CHARLES CYPHERS appreared in ASSAULT ON PRECINCT 13 (1976) with HENRY BRANDON. HENRY BRANDON appreared in OMAR KHAYYAM (1957) with YMA SUMAC. Path 10 between KEVIN BACON and YMA SUMAC has length 3: KEVIN BACON appreared in GOING TO PIECES: THE RISE AND FALL OF THE SLASHER FILM (2006) with ALFRED HITCHCOCK. ALFRED HITCHCOCK appreared in THE MAN WHO KNEW TOO MUCH (1956) with MAHIN S. SHAHRIVAR. MAHIN S. SHAHRIVAR appreared in OMAR KHAYYAM (1957) with YMA SUMAC. ------------------------------------------------------- Path 1 between KEVIN BACON and HAKIM ZZIWA has length 3: KEVIN BACON appreared in THE AIR I BREATHE (2007) with FOREST WHITAKER. FOREST WHITAKER appreared in THE LAST KING OF SCOTLAND (2006) with JOANITTA BEWULIRA-WANDERA. JOANITTA BEWULIRA-WANDERA appreared in JAMAA (2011) with HAKIM ZZIWA. Path 2 between KEVIN BACON and HAKIM ZZIWA has length 3: KEVIN BACON appreared in X-MEN: FIRST CLASS (2011) with JAMES MCAVOY. JAMES MCAVOY appreared in THE LAST KING OF SCOTLAND (2006) with SAM OKELO. SAM OKELO appreared in JAMAA (2011) with HAKIM ZZIWA. Path 3 between KEVIN BACON and HAKIM ZZIWA has length 4: KEVIN BACON appreared in JFK (1991) with LYNDON JOHNSON. LYNDON JOHNSON appreared in TALK TO ME (2007) with CHIWETEL EJIOFOR. CHIWETEL EJIOFOR appreared in THE JOHN AKII BUA STORY: AN AFRICAN TRAGEDY (2008) with SAMUEL IBANDA. SAMUEL IBANDA appreared in JAMAA (2011) with HAKIM ZZIWA. Path 4 between KEVIN BACON and HAKIM ZZIWA has length 4: KEVIN BACON appreared in JAYNE MANSFIELD'S CAR (2012) with JOHN HURT. JOHN HURT appreared in SHOOTING DOGS (2005) with KIZITO SSENTAMU KAYIIRA. KIZITO SSENTAMU KAYIIRA appreared in IMBABAZI (2013) with EDWIN MUKALAZI. EDWIN MUKALAZI appreared in JAMAA (2011) with HAKIM ZZIWA. Path 5 between KEVIN BACON and HAKIM ZZIWA has length 5: KEVIN BACON appreared in JFK (1991) with COLUMBIA DUBOSE. COLUMBIA DUBOSE appreared in MALCOLM X (1992) with DENZEL WASHINGTON. DENZEL WASHINGTON appreared in MISSISSIPPI MASALA (1991) with MICHAEL WAWUYO. MICHAEL WAWUYO appreared in IMBABAZI (2013) with BENJAMIN ABEMIGISHA. BENJAMIN ABEMIGISHA appreared in JAMAA (2011) with HAKIM ZZIWA. Path 6 between KEVIN BACON and HAKIM ZZIWA has length 6: KEVIN BACON appreared in THE AIR UP THERE (1994) with JOHN MATSHIKIZA. JOHN MATSHIKIZA appreared in SHAKE HANDS WITH THE DEVIL (2007) with PETER ODEKE. PETER ODEKE appreared in THE JOHN AKII BUA STORY: AN AFRICAN TRAGEDY (2008) with KELOY KEMIGISHA. KELOY KEMIGISHA appreared in IMBABAZI (2013) with WILSON EGESSA. WILSON EGESSA appreared in HANG OUT (2013) with ISAAC KUDDZU. ISAAC KUDDZU appreared in JAMAA (2011) with HAKIM ZZIWA. There are no more paths between KEVIN BACON and HAKIM ZZIWA. -------------------------------------------------------
__MACOSX/Bacon/sampleRuns/._imdb_notv_testRun.txt
Bacon/sampleRuns/imdb_small_testRun.txt
Actors in database: 161 Movies in database: 1586 ------------------------------------------------------- Path 1 between KEVIN BACON and CHRISTIAN BALE has length 2: KEVIN BACON appreared in QUEENS LOGIC (1991) with JOHN MALKOVICH. JOHN MALKOVICH appreared in EMPIRE OF THE SUN (1987) with CHRISTIAN BALE. Path 2 between KEVIN BACON and CHRISTIAN BALE has length 3: KEVIN BACON appreared in MURDER IN THE FIRST (1995) with CHRISTIAN SLATER. CHRISTIAN SLATER appreared in HARD RAIN (1998) with MORGAN FREEMAN. MORGAN FREEMAN appreared in BATMAN BEGINS (2005) with CHRISTIAN BALE. Path 3 between KEVIN BACON and CHRISTIAN BALE has length 4: KEVIN BACON appreared in SHE'S HAVING A BABY (1988) with BILL MURRAY. BILL MURRAY appreared in COFFEE AND CIGARETTES (2003) with CATE BLANCHETT. CATE BLANCHETT appreared in LORD OF THE RINGS: THE FELLOWSHIP OF THE RING, THE (2001) with CHRISTOPHER LEE. CHRISTOPHER LEE appreared in MIO MIN MIO (1987) with CHRISTIAN BALE. There are no more paths between KEVIN BACON and CHRISTIAN BALE. ------------------------------------------------------- Path 1 between KEVIN BACON and CLARK GABLE has length 4: KEVIN BACON appreared in MURDER IN THE FIRST (1995) with CHRISTIAN SLATER. CHRISTIAN SLATER appreared in LAST PARTY, THE (1993) with ARNOLD SCHWARZENEGGER. ARNOLD SCHWARZENEGGER appreared in CENTURY OF CINEMA, A (1994) with JAMES STEWART. JAMES STEWART appreared in BROTHER, CAN YOU SPARE A DIME? (1975) with CLARK GABLE. There are no more paths between KEVIN BACON and CLARK GABLE. ------------------------------------------------------- Path 1 between KEVIN BACON and DANA CARVEY has length 3: KEVIN BACON appreared in SHE'S HAVING A BABY (1988) with BILL MURRAY. BILL MURRAY appreared in SATURDAY NIGHT LIVE CHRISTMAS (1999) with ADAM SANDLER. ADAM SANDLER appreared in ADAM SANDLER GOES TO HELL (2001) with DANA CARVEY. There are no more paths between KEVIN BACON and DANA CARVEY. ------------------------------------------------------- Path 1 between KEVIN BACON and JOHN MALKOVICH has length 1: KEVIN BACON appreared in QUEENS LOGIC (1991) with JOHN MALKOVICH. There are no more paths between KEVIN BACON and JOHN MALKOVICH. ------------------------------------------------------- Path 1 between KEVIN BACON and JAMES EARL JONES has length 3: KEVIN BACON appreared in QUEENS LOGIC (1991) with JOHN MALKOVICH. JOHN MALKOVICH appreared in PLACES IN THE HEART (1984) with DANNY GLOVER. DANNY GLOVER appreared in ENNIS' GIFT (2000) with JAMES EARL JONES. Path 2 between KEVIN BACON and JAMES EARL JONES has length 3: KEVIN BACON appreared in MURDER IN THE FIRST (1995) with CHRISTIAN SLATER. CHRISTIAN SLATER appreared in LAST PARTY, THE (1993) with ARNOLD SCHWARZENEGGER. ARNOLD SCHWARZENEGGER appreared in CONAN THE BARBARIAN (1982) with JAMES EARL JONES. Path 3 between KEVIN BACON and JAMES EARL JONES has length 3: KEVIN BACON appreared in SHE'S HAVING A BABY (1988) with BILL MURRAY. BILL MURRAY appreared in MR. MIKE'S MONDO VIDEO (1979) with CARRIE FISHER. CARRIE FISHER appreared in STAR WARS (1977) with JAMES EARL JONES. There are no more paths between KEVIN BACON and JAMES EARL JONES. -------------------------------------------------------
__MACOSX/Bacon/sampleRuns/._imdb_small_testRun.txt
__MACOSX/Bacon/._sampleRuns
Bacon/src/.DS_Store
__MACOSX/Bacon/src/._.DS_Store
Bacon/src/Baconator.java
Bacon/src/Baconator.java
/**
* Simple starting class for the IMDB graph search application.
*
*
@author
M. Allen
*/
public
class
Baconator
{
/**
* Simple initiating main.
*
*
@param
args Not used.
*/
public
static
void
main
(
String
[]
args
)
{
InterfaceWindow
window
=
new
InterfaceWindow
();
window
.
makeWindow
();
}
}
__MACOSX/Bacon/src/._Baconator.java
Bacon/src/Grapher.java
Bacon/src/Grapher.java
/**
* This class should create the graph of movie stars, and allow
* queries of pairs of actors. It should find a set of shortest paths between
* the actors, print them out, and also indicate if one or more of the actors is
* not found in the data-base if necessary.
*
*
@author
YOUR NAME HERE
*/
import
java
.
io
.
File
;
import
javax
.
swing
.
JTextArea
;
public
class
Grapher
{
/**
* Takes in name of database text-file as input, along with GUI text-area
* for output.
*
* This is called when the file is chosen by user; it should
* load the data in the file into some structures, and use the text-area to
* report the number of actors and movies contained in the file.
*
* DELETE THESE COMMENTS WHEN YOU ARE DONE AND REPLACE WITH MEANINGFUL ONES.
*/
public
Grapher
(
File
inFile
,
JTextArea
output
)
{
// ADD YOUR CODE HERE AND DELETE THE FOLLOWING
output
.
append
(
"I do nothing."
);
}
/**
* Takes in two names of actors, along with a number N, and returns
* the shortest N paths that link them (if N such paths exist; if not, then
* it returns as many as it can).
*
* Each path should be disjoint in the sense that no actor (other than the
* two being searched for) should appear in more than one path.
*
* The paths should be displayed in the output text-area in the
* format described in the homework PDF.
*
* If you need other helper methods, you should write them.
*
* DELETE THESE COMMENTS WHEN YOU ARE DONE AND REPLACE WITH MEANINGFUL ONES.
*/
public
void
getLinks
(
String
actor1
,
String
actor2
,
int
numPaths
)
{
// ADD YOUR CODE HERE AND DELETE THIS COMMENT
}
}
__MACOSX/Bacon/src/._Grapher.java
Bacon/src/InterfaceWindow.java
Bacon/src/InterfaceWindow.java
/**
* This class extends the JFrame class to create a window
* for the Kevin Bacon project to use.
*
* YOU WILL WANT TO ADD SOME CODE IN THE actionPerformed() METHOD, BELOW, BUT
* OTHERWISE SHOULD NOT NEED TO MODIFY THIS FILE.
*
*
@author
: M. Allen
*
@author
YOUR NAME HERE
*/
import
java
.
awt
.
*
;
import
java
.
awt
.
event
.
*
;
import
javax
.
swing
.
*
;
import
javax
.
swing
.
filechooser
.
*
;
import
java
.
io
.
*
;
@
SuppressWarnings
(
"serial"
)
public
class
InterfaceWindow
extends
JFrame
implements
ActionListener
{
private
TextField
actor1
,
actor2
,
numPaths
;
private
Label
dataFileNameArea
;
private
JButton
chooseData
,
link
;
private
JFileChooser
chooser
;
private
JTextArea
outputArea
;
private
File
inFile
;
private
Grapher
graph
;
private
final
int
windowWidth
=
750
;
private
final
int
windowHeight
=
750
;
/**
* post: creates window with text fields for file-name and text,
* and action buttons to handle the text input
*/
public
InterfaceWindow
()
{
// set up the basic window
setTitle
(
"Don't Stand So Close To Me"
);
setBounds
(
10
,
10
,
windowWidth
,
windowHeight
);
setVisible
(
true
);
setLayout
(
null
);
setResizable
(
false
);
getContentPane
().
setBackground
(
new
Color
(
175
,
238
,
238
)
);
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
}
public
void
makeWindow
()
{
// create and add buttons
chooseData
=
new
JButton
(
"Input..."
);
chooseData
.
setBounds
(
350
,
15
,
100
,
30
);
chooseData
.
addActionListener
(
this
);
add
(
chooseData
,
0
);
link
=
new
JButton
(
"Find Links"
);
link
.
setBounds
(
350
,
85
,
100
,
30
);
link
.
addActionListener
(
this
);
add
(
link
,
0
);
// add labels and fields for input & output
Label
fLabel
=
new
Label
(
" Database: "
);
fLabel
.
setBackground
(
getContentPane
().
getBackground
()
);
fLabel
.
setBounds
(
10
,
15
,
100
,
20
);
add
(
fLabel
,
0
);
dataFileNameArea
=
new
Label
();
dataFileNameArea
.
setBounds
(
100
,
15
,
200
,
20
);
dataFileNameArea
.
setBackground
(
getContentPane
().
getBackground
()
);
dataFileNameArea
.
setText
(
"<NONE CHOSEN>"
);
add
(
dataFileNameArea
,
0
);
Label
a1Label
=
new
Label
(
" Actor 1: "
);
a1Label
.
setBackground
(
getContentPane
().
getBackground
()
);
a1Label
.
setBounds
(
10
,
55
,
100
,
20
);
add
(
a1Label
,
0
);
actor1
=
new
TextField
();
actor1
.
setBounds
(
100
,
55
,
200
,
20
);
actor1
.
addActionListener
(
this
);
add
(
actor1
,
0
);
Label
a2Label
=
new
Label
(
" Actor 2: "
);
a2Label
.
setBackground
(
getContentPane
().
getBackground
()
);
a2Label
.
setBounds
(
10
,
90
,
100
,
20
);
add
(
a2Label
,
0
);
actor2
=
new
TextField
();
actor2
.
setBounds
(
100
,
90
,
200
,
20
);
actor2
.
addActionListener
(
this
);
add
(
actor2
,
0
);
numPaths
=
new
TextField
();
numPaths
.
setBounds
(
link
.
getX
()
+
link
.
getWidth
()
+
10
,
link
.
getY
()
+
5
,
50
,
20
);
numPaths
.
addActionListener
(
this
);
add
(
numPaths
,
0
);
// chooser for files
chooser
=
new
JFileChooser
();
chooser
.
setCurrentDirectory
(
new
java
.
io
.
File
(
"."
)
);
FileNameExtensionFilter
filter
=
new
FileNameExtensionFilter
(
"Text Files"
,
"txt"
);
chooser
.
setFileFilter
(
filter
);
// add text output display area
outputArea
=
new
JTextArea
();
outputArea
.
setEditable
(
false
);
outputArea
.
setLineWrap
(
true
);
outputArea
.
setWrapStyleWord
(
true
);
outputArea
.
setFont
(
new
Font
(
"Monospaced"
,
Font
.
PLAIN
,
12
)
);
int
offset
=
this
.
getInsets
().
top
+
actor2
.
getY
()
+
actor2
.
getHeight
();
outputArea
.
setBounds
(
0
,
0
,
windowWidth
-
10
,
windowHeight
-
(
offset
+
30
)
);
JScrollPane
scroll
=
new
JScrollPane
(
outputArea
);
scroll
.
setBounds
(
5
,
offset
,
outputArea
.
getWidth
(),
outputArea
.
getHeight
()
);
add
(
scroll
,
0
);
repaint
();
}
// post: inFile == file chosen by user (if any)
private
void
openDataFile
()
{
int
returnVal
=
chooser
.
showOpenDialog
(
this
);
if
(
returnVal
==
JFileChooser
.
APPROVE_OPTION
)
{
inFile
=
chooser
.
getSelectedFile
();
dataFileNameArea
.
setText
(
inFile
.
getName
()
);
graph
=
new
Grapher
(
inFile
,
outputArea
);
}
}
// post: responds to action events from buttons
public
void
actionPerformed
(
ActionEvent
e
)
{
// choosing a file of data
if
(
e
.
getSource
()
==
chooseData
)
openDataFile
();
// getting link between two actors
if
(
e
.
getSource
()
==
link
||
e
.
getSource
()
==
actor1
||
e
.
getSource
()
==
actor2
||
e
.
getSource
()
==
numPaths
)
{
if
(
graph
!=
null
)
{
// ADD CALLS TO YOUR getLinks() METHOD HERE
}
}
}
}