Implement threads and a GUI interface using advanced Java Swing classes.
Required data structure - the advanced data structure uses implementation of a multi-tree with
the following levels: Cave - level 0 Party - Level 1 Creature - Level 2 Artifacts - Level 3
Treasures - Level 3 Jobs
Use the Swing class JTree effectively to display the contents of the data file. Implement a JTable
to also show the contents of the data file.
Threads: Implement a thread for each job representing a task that creature will perform. Use the
synchronize directive to avoid race conditions and insure that a creature is performing only one
job at a time. The thread for each job should be started as the job is read in from the data file.
Only one job should be progressing for each creature at any moment. Use delays to show the
creature doing the task.
Use a JProgressBar for each creature to show the creature performing the task. Use JButton\'s on
the Job panel to allow the task to be suspended and cancelled. The GUI elements should be
distinct from the other classes in the program.
Solution
Please find the code below for the above problem :
import java.util.ArrayList;
import java.util.Scanner;
import java.util.HashMap;
import java.util.Random;
import java.io.File;
import java.io.FileNotFoundException;
import javax.swing.JProgressBar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Cave2 {
ArrayList parties = new ArrayList (); // the parties
ArrayList loners = new ArrayList (); // creatures not in parties
ArrayList drops = new ArrayList (); // Treasures not carried
ArrayList magics = new ArrayList (); // Artifacts not carried
ArrayList jobs = new ArrayList (); // Jobs to be performed
JFrame jf = new JFrame ();
JPanel jrun = new JPanel ();
JTextArea jta = new JTextArea (10, 10);
// hashmap of everything by index
HashMap hmElements = new HashMap ();
public Cave2 () {
Scanner sf = null;
String fileName = \"dataC.txt\";
try {
// sf = new Scanner (new File (fileName));
// getResourceAsStream is the way to access a data file in a jar package file
// this works in general, so leave it here as is.
sf = new Scanner (getClass().getResourceAsStream (fileName));
}
catch (Exception e) {
System.out.println (e + \"\ File bummer: \" + fileName);
} // end open file try/catch block
if (sf == null)
return;
readFile (sf);
jta.setText (toString ());
jf.setTitle (\"Cave2\");
jf.add (jrun, BorderLayout.PAGE_END);
jrun.setLayout (new GridLayout (0, 5, 2, 5));
jf.add (new JScrollPane (jta), BorderLayout.CENTER);
jta.setFont (new Font (\"Monospaced\", 0, 12));
jf.pack ();
jf.setVisible (true);
jf.setLocationRelativeTo (null);
} // end no-parameter constru.
Presiding Officer Training module 2024 lok sabha elections
Implement threads and a GUI interface using advanced Java Swing clas.pdf
1. Implement threads and a GUI interface using advanced Java Swing classes.
Required data structure - the advanced data structure uses implementation of a multi-tree with
the following levels: Cave - level 0 Party - Level 1 Creature - Level 2 Artifacts - Level 3
Treasures - Level 3 Jobs
Use the Swing class JTree effectively to display the contents of the data file. Implement a JTable
to also show the contents of the data file.
Threads: Implement a thread for each job representing a task that creature will perform. Use the
synchronize directive to avoid race conditions and insure that a creature is performing only one
job at a time. The thread for each job should be started as the job is read in from the data file.
Only one job should be progressing for each creature at any moment. Use delays to show the
creature doing the task.
Use a JProgressBar for each creature to show the creature performing the task. Use JButton's on
the Job panel to allow the task to be suspended and cancelled. The GUI elements should be
distinct from the other classes in the program.
Solution
Please find the code below for the above problem :
import java.util.ArrayList;
import java.util.Scanner;
import java.util.HashMap;
import java.util.Random;
import java.io.File;
import java.io.FileNotFoundException;
import javax.swing.JProgressBar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Color;
2. import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Cave2 {
ArrayList parties = new ArrayList (); // the parties
ArrayList loners = new ArrayList (); // creatures not in parties
ArrayList drops = new ArrayList (); // Treasures not carried
ArrayList magics = new ArrayList (); // Artifacts not carried
ArrayList jobs = new ArrayList (); // Jobs to be performed
JFrame jf = new JFrame ();
JPanel jrun = new JPanel ();
JTextArea jta = new JTextArea (10, 10);
// hashmap of everything by index
HashMap hmElements = new HashMap ();
public Cave2 () {
Scanner sf = null;
String fileName = "dataC.txt";
try {
// sf = new Scanner (new File (fileName));
// getResourceAsStream is the way to access a data file in a jar package file
// this works in general, so leave it here as is.
sf = new Scanner (getClass().getResourceAsStream (fileName));
}
catch (Exception e) {
System.out.println (e + " File bummer: " + fileName);
} // end open file try/catch block
if (sf == null)
return;
readFile (sf);
jta.setText (toString ());
3. jf.setTitle ("Cave2");
jf.add (jrun, BorderLayout.PAGE_END);
jrun.setLayout (new GridLayout (0, 5, 2, 5));
jf.add (new JScrollPane (jta), BorderLayout.CENTER);
jta.setFont (new Font ("Monospaced", 0, 12));
jf.pack ();
jf.setVisible (true);
jf.setLocationRelativeTo (null);
} // end no-parameter constructor
public static void main (String args []) {
Cave2 cv = new Cave2 ();
cv.jf.add (new JLabel (" Close this window to end program "),
BorderLayout.PAGE_START);
cv.jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
// System.out.println (cv);
} // end main
public String toString () {
String sb = "";
sb += toString (parties, "Party List");
sb += toString (loners , "Unaligned Creatures");
sb += toString (drops , "Unheld Treasures");
sb += toString (magics , "Unheld Artifacts");
sb += toString (jobs , "Jobs");
// sb += toString (hmElements.values (), "HashMap");
return sb;
} // end method dump
String toString (Iterable it, String label) {
String sb = " ----- DUMP " + label + " -------";
for (CaveElement c: it)
sb += " " + c.toString ();
return sb;
} // end method dump Iterable String
4. void readFile (Scanner sf) {
String inline;
Scanner line;
while (sf.hasNext()) {
inline = sf.nextLine().trim();
if (inline.length() == 0) continue;
line = new Scanner (inline).useDelimiter ("s*:s*"); // compress white space also, else
nextInt fails
switch (inline.charAt (0)) {
case 'p': addParty (line); break;
case 'c': addCreature (line); break;
case 't': addTreasure (line); break;
case 'a': addArtifact (line); break;
case 'j': addJob (line); break;
} // end switch
} // end while reading data file
jf.pack ();
} // end readFile
public void addJob (Scanner sc) {
Job job = new Job (hmElements, jrun, sc);
// (new Thread (job)).start();
// jobs.add (job);
} // end method addJob
public void addParty (Scanner sc){
Party pt = new Party (sc);
parties.add (pt);
hmElements.put (pt.index, pt);
} // end method addParty
public void addCreature (Scanner sc) {
Creature c = new Creature ();
int target = c.makeCreature (sc);
Party p = (Party)(hmElements.get (target));
if (p == null)
5. loners.add (c);
else {
p.addCreature (c);
c.party = p;
}
hmElements.put (c.index, c);
} // end method addToParty
public void addTreasure (Scanner sc) {
Treasure t = new Treasure ();
int target = t.makeTreasure (sc);
Creature c = (Creature)(hmElements.get (target));
if (c == null)
drops.add (t);
else {
c.addTreasure (t);
t.holder = c;
}
hmElements.put (t.index, t);
} // end method addTreasure
public void addArtifact (Scanner sc) {
Artifact a = new Artifact ();
int target = a.makeArtifact (sc);
Creature c = (Creature)(hmElements.get (target));
if (a == null)
magics.add (a);
else {
c.addArtifact (a);
a.holder = c;
}
hmElements.put (a.index, a);
} // end method addArtifact
} // end class Cave
6. class CaveElement {}
// j::::[::]*
class Job extends CaveElement implements Runnable {
static Random rn = new Random ();
JPanel parent;
Creature worker = null;
int jobIndex;
long jobTime;
String jobName = "";
JProgressBar pm = new JProgressBar ();
boolean goFlag = true, noKillFlag = true;
JButton jbGo = new JButton ("Stop");
JButton jbKill = new JButton ("Cancel");
Status status = Status.SUSPENDED;
enum Status {RUNNING, SUSPENDED, WAITING, DONE};
public Job (HashMap hmElements, JPanel cv, Scanner sc) {
parent = cv;
sc.next (); // dump first field, j
jobIndex = sc.nextInt ();
jobName = sc.next ();
int target = sc.nextInt ();
worker = (Creature) (hmElements.get (target));
jobTime = sc.nextInt ();
pm = new JProgressBar ();
pm.setStringPainted (true);
parent.add (pm);
parent.add (new JLabel (worker.name, SwingConstants.CENTER));
parent.add (new JLabel (jobName , SwingConstants.CENTER));
parent.add (jbGo);
parent.add (jbKill);
jbGo.addActionListener (new ActionListener () {
7. public void actionPerformed (ActionEvent e) {
toggleGoFlag ();
}
});
jbKill.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent e) {
setKillFlag ();
}
});
new Thread (this).start();
} // end constructor
// JLabel jln = new JLabel (worker.name);
// following shows how to align text relative to icon
// jln.setHorizontalTextPosition (SwingConstants.CENTER);
// jln.setHorizontalAlignment (SwingConstants.CENTER);
// parent.jrun.add (jln);
public void toggleGoFlag () {
goFlag = !goFlag;
} // end method toggleRunFlag
public void setKillFlag () {
noKillFlag = false;
jbKill.setBackground (Color.red);
} // end setKillFlag
void showStatus (Status st) {
status = st;
switch (status) {
case RUNNING:
jbGo.setBackground (Color.green);
jbGo.setText ("Running");
break;
8. case SUSPENDED:
jbGo.setBackground (Color.yellow);
jbGo.setText ("Suspended");
break;
case WAITING:
jbGo.setBackground (Color.orange);
jbGo.setText ("Waiting turn");
break;
case DONE:
jbGo.setBackground (Color.red);
jbGo.setText ("Done");
break;
} // end switch on status
} // end showStatus
public void run () {
long time = System.currentTimeMillis();
long startTime = time;
long stopTime = time + 1000 * jobTime;
double duration = stopTime - time;
synchronized (worker.party) { // party since looking forward to P4 requirements
while (worker.busyFlag) {
showStatus (Status.WAITING);
try {
worker.party.wait();
}
catch (InterruptedException e) {
} // end try/catch block
} // end while waiting for worker to be free
worker.busyFlag = true;
} // end sychronized on worker
while (time < stopTime && noKillFlag) {
try {
Thread.sleep (100);
9. } catch (InterruptedException e) {}
if (goFlag) {
showStatus (Status.RUNNING);
time += 100;
pm.setValue ((int)(((time - startTime) / duration) * 100));
} else {
showStatus (Status.SUSPENDED);
} // end if stepping
} // end runninig
pm.setValue (100);
showStatus (Status.DONE);
synchronized (worker.party) {
worker.busyFlag = false;
worker.party.notifyAll ();
}
} // end method run - implements runnable
public String toString () {
String sr = String.format ("j:%7d:%15s:%7d:%5d", jobIndex, jobName, worker.index,
jobTime);
return sr;
} //end method toString
} // end class Job
// p::
class Party extends CaveElement {
ArrayList members = new ArrayList ();
int index;
String name;
public Party (Scanner s) {
s.next (); // dump first field, p
index = s.nextInt ();
10. name = s.next();
} // String constructor
public void addCreature (Creature c) {
members.add (c);
} // end method addCreature
public String toString () {
String sr = String.format ("p:%6d:%s", index, name);
for (Creature c: members)
sr += " " + c;
return sr;
} // end toString method
} // end class Party
// c:::::::
class Creature extends CaveElement
// implements Runnable
{
ArrayList treasures = new ArrayList ();
ArrayList artifacts = new ArrayList ();
int index;
String type, name;
Party party = null;
double empathy, fear, capacity;
boolean busyFlag = false;
public int makeCreature (Scanner s) {
int partyInd;
s.next (); // dump first field, c
index = s.nextInt ();
type = s.next ();
name = s.next ();
partyInd = s.nextInt ();
11. empathy = s.nextDouble ();
fear = s.nextDouble ();
capacity = s.nextDouble ();
return partyInd;
} // Scanner Creature factory method
public void addTreasure (Treasure t) {treasures.add (t);}
public void addArtifact (Artifact a) {artifacts.add (a);}
// public void addJob (long t, Scanner sc) {
// jobTime = t;
// } // end addJob
public String toString () {
String sb = "";
if (party == null)
sb += String.format ("c:%6d: %s : %s : %6d : %4.1f : %4.1f : %4.1f", index, type,
name, 0, empathy, fear, capacity);
else sb += String.format ("c:%6d: %s : %s : %6d : %4.1f : %4.1f : %4.1f", index, type,
name, party.index, empathy, fear, capacity);
for (Treasure t: treasures) sb += " " + t;
for (Artifact a: artifacts) sb += " " + a;
return sb;
} // end toString method
} // end class CaveElement
// t:::::
class Treasure extends CaveElement {
int index;
String type;
double weight, value;
Creature holder;
public int makeTreasure (Scanner s) {
int partyInd;
12. s.next (); // dump first field, c
index = s.nextInt ();
type = s.next ();
partyInd = s.nextInt ();
weight = s.nextDouble ();
value = s.nextDouble ();
return partyInd;
} // Scanner Treasure factory method
public String toString () {
if (holder == null)
return String.format ("t:%6d: %s : %6d : %4.1f : %4.1f", index, type, 0, weight,
value);
return String.format ("t:%6d: %s : %6d : %4.1f : %4.1f", index, type, holder.index, weight,
value);
} // end toString method
} // end class Treasure
// a:::[:]
class Artifact extends CaveElement {
int index;
String type, name = "";
Creature holder;
public int makeArtifact (Scanner s) {
int partyInd;
s.next (); // dump first field, c
index = s.nextInt ();
type = s.next ();
partyInd = s.nextInt ();
if (s.hasNext()) name = s.next ();
return partyInd;
} // Scanner Artifact factory method
public String toString () {
if (holder == null)