第8章练习13 Java的艺术与科学 - 如何制作新的线程来更新每个塔的颜色

时间:2016-01-12 03:11:56

标签: java multithreading

请有人能帮助我。

我正在尝试从Java的艺术与科学的第8章完成练习13并且已经陷入困境。

程序显示一组塔(GRects),然后用户需要点击任何塔,让它发送信号以“点亮(改变颜色)”链中的下一个塔。在system.out的后台我可以看到发送的信号,但事件调度程序在更新颜色之前等待(我在另一个站点上读取)此过程完成。每个对象还有一个时间thread.sleep(300)。我的问题是:有人可以教我如何写一个新的线程或告诉我我做错了什么以及我应该如何解决它。我已经查看了线程上的Oracle文档,无法理解所有内容,我也是Java的新手,并且正在进行自学。虽然需要让练习工作,我真的想学习如何制作这样的程序,以便将来我不会再被卡住。任何来自在线社区的帮助将不胜感激。先感谢您。 代码:

/**
 * 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();        
        }

    }

}

0 个答案:

没有答案