1

Would someone be able to help me please.

I am trying to complete exercise 13 from chapter 8 of the Art and Science of Java and have became stuck.

The program displays a set of towers (GRects), and then the user needs to click on any tower to have it send a signal to 'light (change the color)' the next tower in the chain. In the background with system.out I can see the signals being sent, however the event dispatcher waits (I read on another site) for this process to finish before updating the colors. Each object also has a time thread.sleep(300). My question is: Can someone please teach me how to write a new thread or show me what I am doing wrong and how I should go about fixing it. I have looked at Oracle docs on threads and can't understand what everything is, I am also new to Java and and doing self-study. While getting the exercise working is needed, I really want to learn how to make a program like this so that in the future I don't get stuck again. Any help from the online community would be greatly appreciated. Thank you in advance. The code:

/**
 * Class name: SignalTower
 * -----------------------
 * This class defines a signal tower object that passes a message to         the
 * next tower in a line.
 * 
 * Programmer: 
 * Date: 2015/12/25
 */

package com.chapter8;

import java.awt.Color;

import acm.graphics.GCompound;
import acm.graphics.GLabel;
import acm.graphics.GRect;



/* Class: SignalTower */
/**
 * This class defines a signal tower object that passes a message
 * to the next tower in a line.
 */
public class SignalTower extends GCompound {


    GRect tower = new GRect(TOWER_WIDTH,TOWER_HEIGHT);

    /* Constructor: SignalTower(name, link) */
    /**
     * Constructs a new signal tower with the following parameters:
     *
     * @param name The name of the tower
     * @param link A link to the next tower, or null if none exists
     */
    public SignalTower(String name, SignalTower link) {
        towerName = name;
        nextTower = link;
        buildTower();
    }

    /* Method: signal() */
    /**
     * This method represents sending a signal to this tower. The     effect
     * is to light the signal fire here and to send an additional signal
     * message to the next tower in the chain, if any.
     */
    public void signal() {
         lightCurrentTower();
        if (nextTower != null) nextTower.signal();
    }

    /* Method: lightCurrentTower() */


/**
     * This method lights the signal fire for this tower. This version
     * supplies a temporary implementation (typically called a "stub")
     * that simply prints the name of the tower to the standard output
     * channel. If you wanted to redesign this class to be part of a
     * graphical application, for example, you could override this
     * method to draw an indication of the signal fire on the display.
     */
public void lightCurrentTower() {
    try {
        this.setTowerColor();
        Thread.sleep(300);

    } catch (InterruptedException e) {
        // Handle here
    }
    System.out.println("Lighting " + towerName);

}
    public void buildTower(){
        tower.setFilled(false);
        add(tower);

        GLabel label = new GLabel(towerName);
        label.setLocation(getTowerWidth()/2  - label.getWidth()/2, this.getTowerLabelY());
        add(label);
    }

    public void setTowerColor(){
        tower.setColor(Color.RED);
        tower.setFillColor(Color.RED);
        tower.setFilled(true);
    }

    public int getTowerWidth(){
        return TOWER_WIDTH;
    }
    public int getTowerSpace(){
        return TOWER_SPACE;
    }
    public int getTowerLabelY(){
        return TOWER_LABEL_Y;
    }

    /* Private instance variables */
    private String towerName;         /* The name of this tower    */
    private SignalTower nextTower;    /* A link to the next tower  */
    private static final int TOWER_WIDTH = 30;
    private static final int TOWER_HEIGHT = 180;
    private static final int TOWER_SPACE = 100;
    private static final int TOWER_LABEL_Y = 200;

}

/**
 * File name: BeaconsOfGondor.java
 * -------------------------------
 * Message passing in linked structures: The beacons of Gondor:
 * 
 * For answer Gandalf cried aloud to his horse. “On, Shadowfax! We must hasten.
 * Time is short. See! The beacons of Gondor are alight, calling for aid. War 
 * is kindled. See, there is the fire on Amon Dîn, and flame on Eilenach; and 
 * there they go speeding west: Nardol, Erelas, Min-Rimmon, Calenhad, and the
 * Halifirien on the borders of Rohan.”
 * —J. R. R. Tolkien, The Return of the King, 1955
 * 
 * This program creates a graphical representation of linked structures using 
 * this example.
 * 
 * Programmer: Peter Lock
 * Date: 2016/1/5
 */
package com.chapter8;

import java.awt.Point;
import java.awt.event.MouseEvent;
import acm.program.GraphicsProgram;

public class BeaconsOfGondor extends GraphicsProgram implements Runnable{

    private Object gobj;
    public String flag = "";

    SignalTower[] towers = new SignalTower[10];

    SignalTower rohan = new SignalTower("Rohan", null);
    SignalTower halifirien = new SignalTower("Halifirien", rohan);
    SignalTower calenhad = new SignalTower("Calenhad", halifirien);
    SignalTower minRimmon = new SignalTower("Min-Rimmon", calenhad);
    SignalTower erelas = new SignalTower("Erelas", minRimmon);
    SignalTower nardol = new SignalTower("Nardol", erelas);
    SignalTower eilenach = new SignalTower("Eilenach", nardol);
    SignalTower amonDin = new SignalTower("Amon Din", eilenach);
    SignalTower minasTirith = new SignalTower("Minas Tirith", amonDin);


    public void run(){
        createTowers();
        addMouseListeners();
        //  minasTirith.signal();
    }

    private void createTowers() {
        add(minasTirith);
        minasTirith.setLocation(minasTirith.getTowerSpace(), 30);       
        add(amonDin);
        amonDin.setLocation(amonDin.getTowerSpace()*2, 30);     
        add(eilenach);
        eilenach.setLocation(eilenach.getTowerSpace()*3, 30);   
        add(nardol);
        nardol.setLocation(nardol.getTowerSpace()*4, 30);       
        add(erelas);
        erelas.setLocation(erelas.getTowerSpace()*5, 30); 
        add(minRimmon);
        minRimmon.setLocation(minRimmon.getTowerSpace()*6, 30);     
        add(calenhad);
        calenhad.setLocation(calenhad.getTowerSpace()*7, 30);       
        add(halifirien);
        halifirien.setLocation(halifirien.getTowerSpace()*8, 30);       
        add(rohan);
        rohan.setLocation(rohan.getTowerSpace()*9, 30);                 
    }


    public void mousePressed(MouseEvent e) {

        gobj = getElementAt(e.getX(), e.getY());       
        if(gobj != null){
            Point mp = e.getPoint();       

            if(minasTirith.contains(mp.getX(), mp.getY())) minasTirith.signal();    
            if(amonDin.contains(mp.getX(), mp.getY())) amonDin.signal();
            if(eilenach.contains(mp.getX(), mp.getY())) eilenach.signal(); 
            if(nardol.contains(mp.getX(), mp.getY())) nardol.signal();
            if(erelas.contains(mp.getX(), mp.getY())) erelas.signal();    
            if(minRimmon.contains(mp.getX(), mp.getY())) minRimmon.signal();
            if(calenhad.contains(mp.getX(), mp.getY())) calenhad.signal();
            if(halifirien.contains(mp.getX(), mp.getY())) halifirien.signal();
            if(rohan.contains(mp.getX(), mp.getY())) rohan.signal();        
        }

    }

}
Peter Lock
  • 11
  • 3
  • Unfortunately I don't have time to give a complete answer to your question at the moment, but you can try looking at my answer to another question where I wrote a sample java program with a couple threads (including one that runs after a timer) at http://stackoverflow.com/questions/33762588/how-do-i-use-one-java-program-to-monitor-another-java-programs-output/34324184#34324184 it isn't the same question, and my answer there is not a full tutorial on threading, but it may be supplemental because you can see a simple example of threads and how the program fits together. Hope that helps. – rp.beltran Jan 12 '16 at 04:10
  • Thank you. I will look at your sample program when I finish work. Thank you very much for the reply. – Peter Lock Jan 12 '16 at 04:14

0 Answers0