2) Broad.java (part 2)

public void drawStone(Graphics g,int i)
{
int centerX,centerY;

if (Stone[i].status == 1 ) //only draw when stone is alive
{
centerX = startPoint.x + (Stone[i].X - 1) * weight 
  - (int)(weight * 0.5);
centerY = startPoint.y + (Stone[i].Y - 1) * weight
  - (int)(weight * 0.5);

if (Stone[i].type == 1)
{offscreenG.setColor(Color.black);
 g.fillOval(centerX,centerY,weight+1,weight+1);
}
else
{offscreenG.setColor(Color.white);
  g.fillOval(centerX,centerY,weight,weight);}

if (Stone[i].type == 0) //if white draw black border
{ g.setColor(Color.black);
g.drawOval(centerX,centerY,weight,weight);} if (i == sequence) { //To indicate the last one
offscreenG.setColor(Color.blue);
g.drawOval(centerX,centerY,weight,weight);
} }

} // APPLET INFO SUPPORT:
// The getAppletInfo() method returns a string describing the applet's
// author, copyright date, or miscellaneous information.
    //--------------------------------------------------------------------------
public String getAppletInfo()
{
return "Name: Broad\r\n" +
       "Author: tmss\r\n" +
       "Created with Microsoft Visual J++ Version 1.1";
}
public void init()
{
        // If you use a ResourceWizard-generated "control creator" class to
        // arrange controls in your applet, you may want to call its
        // CreateControls() method from within this method. Remove the following
        // call to resize() before adding the call to CreateControls();
        // CreateControls() does its own resizing.
        //----------------------------------------------------------------------
     resize(500, 700);
setLayout(new FlowLayout(FlowLayout.LEFT,10,5));
setBackground(Color.white);
offscreenImg = createImage(this.size().width, this.size().height);
offscreenG = offscreenImg.getGraphics();
Stone[0] = new Stone(0,0,0,0,3); //Set Indicate add(new Label("Hello , Go World",Label.CENTER));

fiveStarChoice = new Choice();
sizeChoice = new Choice(); fiveStarChoice.addItem("Go");
fiveStarChoice.addItem("Five Stars");

sizeChoice.addItem("9");
sizeChoice.addItem("11");
sizeChoice.addItem("13");
sizeChoice.addItem("15");
sizeChoice.addItem("17");
sizeChoice.addItem("19");

sizeChoice.select("19");
add(fiveStarChoice);
add(sizeChoice); String s;
s = getParameter("broadsize");
if (s != null) interval = Integer.parseInt(s);

backButton = new Button("Go Back");
add(backButton);

qf = new Checkbox("www.qf.com",false) ;
add(qf); cbg = new CheckboxGroup();
black = new Checkbox("Black",true,cbg);
white = new Checkbox("White",false,cbg); add(black);
add(white);

tf = new TextField("",20);
add(tf);

          sb = new Scrollbar(Scrollbar.HORIZONTAL,0,0,0,9);
sb.addAdjustmentListener(this);
add(sb);

handicap = new Label("Handicap 0", Label.CENTER);
add(handicap); printButton =  new Button("Print");
add(printButton);

saveasButton = new Button("Saveas");
this.add(saveasButton);
}
// Place additional applet clean up code here.  destroy() is called when
// when you applet is terminating and being unloaded.
//-------------------------------------------------------------------------
public void destroy()
{
// TODO: Place applet cleanup code here
   offscreenG.dispose();
   } public void update(Graphics g) {
paint(g);
}   
// Broad Paint Handler
//--------------------------------------------------------------------------
public void paint(Graphics g)
{
drawBroad(offscreenG);
for (int i = 1; i <= sequence; i++)
{
drawStone(offscreenG,i);

}

offscreenG.setColor(Color.white);
offscreenG.fillRect(0,5,200,90);
offscreenG.setColor(Color.red);
offscreenG.fillOval(xpos,5,90,90); g.drawImage(offscreenImg,0,0,this);

tf.setText("Current Stone: "+sequence); if (stoneColor == 1){ cbg.setCurrent(black);}
  else {cbg.setCurrent(white);}

sb.setValue(numHandicap);

handicap.setText("Handicap" + numHandicap);
} public boolean checkExist(int x, int y)
{
if (pointStatus[x][y] == 1) 
return true;
else
pointStatus[x][y] = 1; /*set status as occupied*/
return false;
}
// MOUSE SUPPORT:
// The mouseDown() method is called if the mouse button is pressed
// while the mouse cursor is over the applet's portion of the screen.
//--------------------------------------------------------------------------

解决方案 »

  1.   

    2) Broad.java (part3) public boolean mouseDown(Event evt, int x, int y)
    {
    // TODO: Place applet mouseDown code here
    int X,Y;
    boolean kill = false;
    if (evt.clickCount == 2) {
    X = 1 + (x - startPoint.x + weight/2)/(weight);
    if (X < 1 || X > interval ) 
    return false;
    Y = 1 + (y - startPoint.y + weight/2)/(weight);
    if (Y < 1 || Y > interval ) 
    return false; if (checkExist(X,Y)) return false; /*The point is occupied*/ sequence ++ ;

    Stone[sequence] = new Stone(X,Y,sequence,1,stoneColor);

    if (fiveStar == 0){
    //Check killing situation
    for (int i = 1; i <= interval ; i++)
    for (int j = 1; j <= interval ; j++)
    {
    if (i ==X && j==Y) continue;
    if ((pointStatus[i][j] == 1) && (checkStone(i,j).type == 1-stoneColor))//Starting checking
    {
    pointScanned = new int[interval+1][interval+1];
    if (checkKilling(i,j,1-stoneColor) == true)
    {
    pointStatus[i][j] = 2;
    checkStone(i,j).status = 2 ; //Kill
    kill = true;
    }
    }

    }

    //Actually killing process
    for (int i = 1; i <= interval ; i++)
    for (int j = 1; j <= interval ; j++)
    if (pointStatus[i][j] == 2) //Killing
    pointStatus[i][j] = 0;

    for (int j = 1; j <= sequence ; j++)
    if (Stone[j].status == 2)
    Stone[j].status = sequence; //Killing

    if (kill == false) {
    //Check itself

    for (int i = 1; i <= interval ; i++)
    for (int j = 1; j <= interval ; j++)
    {
    if (pointStatus[i][j] == 1 && checkStone(i,j).type == stoneColor)//Starting checking
    {
    pointScanned = new int[interval+1][interval+1];

    if (checkKilling(i,j,stoneColor) == true)
    {
    pointStatus[i][j] = 2;
    checkStone(i,j).status = 2 ; //Kill
    }
    }

    }

    //Actually killing process
    for (int ii = 1; ii <= interval ; ii++)
    for (int jj = 1; jj <= interval ; jj++)
    if (pointStatus[ii][jj] == 2) //Killing
    pointStatus[ii][jj] = 0;

    for (int jjj = 1; jjj <= sequence ; jjj++)
    if (Stone[jjj].status == 2)
    Stone[jjj].status = sequence; //Killing
    }
    } repaint();
    stoneColor = 1 - stoneColor;
    }
    return true;
    }
        
    public boolean checkKilling(int X,int Y,int type)
    {
    int x;
    boolean Result = true;
    int l_type;

    pointScanned[X][Y] = 1; 
    if (X-1 >= 1 && pointScanned[X-1][Y] != 1) 
    {l_type = checkStone(X-1,Y).type;
     if (l_type == 3) return false ; 
     if (l_type == type) 
    {Result = checkKilling(X-1,Y,type);
    if (Result == false) return false;}
    }

    if (X+1 <= interval && pointScanned[X+1][Y] != 1) 
    {l_type = checkStone(X+1,Y).type;
     if (l_type == 3) return false ; 
     if (l_type == type) 
     {Result = checkKilling(X+1,Y,type);
     if (Result == false) return false;}
    } if (Y-1 >= 1 && pointScanned[X][Y-1] != 1) 
    {l_type = checkStone(X,Y-1).type;
     if (l_type == 3) return false ; 
     if (l_type == type) 
     {Result =checkKilling(X,Y-1,type);
     if (Result == false) return false;}
    } if (Y+1 <= interval && pointScanned[X][Y+1] != 1) 
    {l_type = checkStone(X,Y+1).type;
     if (l_type == 3) return false ; 
     if (l_type == type) 
     { Result =checkKilling(X,Y+1,type);
     if (Result == false) return false;}
    }
    return Result; 
    }
         
    public Stone checkStone(int X, int Y)
    {
    for (int i = 1; i <= sequence ; i++)
    {
    if ((Stone[i].status == 1||Stone[i].status == 2)&&(Stone[i].X ==X)
    &&(Stone[i].Y ==Y) )
    return Stone[i];
    }
    return Stone[0]; //Dummy
    }

    public boolean action(Event evt, Object arg) {
         // if (evt.target instanceof Button) {
    if (evt.target == backButton) {
           goBack();
           return true;

    else if (evt.target == saveasButton)
    {

    String qipu = "WEIQI RECORD";
    try
    {

    FileOutputStream f = new FileOutputStream("Weiqi.txt");
    ObjectOutputStream s = new ObjectOutputStream (f);
    s.writeChars(qipu);
    /*
    for (int i = sequence; i >= 1 ; i--)
    {
    s.writeChars
    }*/
    s.close();
    }
    catch(IOException e)
    {
    e.printStackTrace();
    }
    return true;
    }
    else if (evt.target == printButton)
    {
    // PrintJob job = getToolkit().getPrintJob((Frame)parent,"Print Test",(Properties)null);
    // if (job != null){
    // Graphics pg = job.getGraphics();
    Graphics pg = offscreenImg.getGraphics();
    if (pg != null) {
    getAppletContext().showStatus("Test Printing");
    printAll(pg);
    pg.dispose();
    }
    //job.end();
    //}
    return true;
    }
    else if (evt.target == sizeChoice )
    {
    offscreenImg = createImage(this.size().width, this.size().height);
    offscreenG = offscreenImg.getGraphics(); interval = Integer.parseInt(sizeChoice.getSelectedItem());
    switch (interval) {
    case 19:
    weight = 14;
    break;
    case 17:
    weight = 16;
    break;
    case 15:
    weight = 18;
    break;
    case 13:
    weight = 20;
    break;
    case 11:
    weight = 22;
    break;
    case 9:
    weight = 24;
    break;
    }
    sequence = 0;
    stoneColor = 1;
    pointStatus = new int[interval+1][interval+1];
    numHandicap = 0;
    repaint();
    return true;
    }
    else if (evt.target == fiveStarChoice )
    {
    sequence = 0;
    stoneColor = 1;
    pointStatus = new int[interval+1][interval+1];
    if (fiveStarChoice.getSelectedItem() == "Go" ){
    fiveStar = 0;}
    else{
    fiveStar = 1;}

    numHandicap = 0;
    repaint();
    return true;
    }
    else if (evt.target == qf)
    {
    if (qf.getState() ) {
    URL theURL = null;
    try {theURL = new URL("http://www.qf.com.cn");}
    catch (MalformedURLException e)
    {
    getAppletContext().showStatus("Error");
    }
    getAppletContext().showDocument(theURL);
    }

    return true;
    }
    else return false;
       } public void goBack()
    {
    if (sequence >= 1)
    {pointStatus[Stone[sequence].X][Stone[sequence].Y] = 0;
     sequence --;
     stoneColor = 1 - stoneColor; //Change Color
     for (int i = sequence; i >= 1 ; i--){
    if (Stone[i].status > sequence){
    Stone[i].status = 1; //Make it live
          pointStatus[Stone[i].X][Stone[i].Y] = 1;
    }
     }
     repaint();} }/* public boolean handleEvent(Event evt) {
         if (evt.target instanceof Scrollbar) {
           int v = ((Scrollbar)evt.target).getValue();
           handicap.setText("Handicap" + String.valueOf(v));
    numHandicap = Integer.parseInt(String.valueOf(v));
    //    repaint();
       return true;
         }
    else 
    {
    action(evt,null);
    return true;
    };
       }
    */
    public void adjustmentValueChanged(AdjustmentEvent e){
    if (interval==19) {
    int v = ((Scrollbar)e.getSource()).getValue();
          numHandicap = Integer.parseInt(String.valueOf(v)); pointStatus = new int[interval+1][interval+1];

    if (numHandicap >= 1 ){
    Stone[1] = new Stone(4,4,1,1,1);
    checkExist(4,4);
    if (numHandicap >= 2) {Stone[2] = new Stone(16,16,3,1,1);checkExist(16,16);}
    if (numHandicap >= 3) {Stone[3] = new Stone(4,16,3,1,1);checkExist(4,16);}
    if (numHandicap >= 4) {Stone[4] = new Stone(16,4,4,1,1);checkExist(16,4);}
    if (numHandicap >= 5) {Stone[5] = new Stone(10,4,5,1,1);checkExist(10,4);}
    if (numHandicap >= 6) {Stone[6] = new Stone(10,16,6,1,1);checkExist(10,16);}
    if (numHandicap >= 7) {Stone[7] = new Stone(4,10,7,1,1);checkExist(4,10);}
    if (numHandicap >= 8) {Stone[8] = new Stone(16,10,8,1,1);checkExist(16,10);}
    if (numHandicap >= 9) {Stone[9] = new Stone(10,10,9,1,1);checkExist(10,10);}
    stoneColor = 0;
    sequence = numHandicap ;
    repaint();}
    else{
    stoneColor = 1;
    sequence = numHandicap ;
    repaint(); }}
    }
      

  2.   

    2) Broad.java (part4)
    public void run() {
    while (true) {
    for (xpos = 5; xpos <= 105; xpos += 4){
    repaint();
    try{Thread.sleep(100);}
    catch(InterruptedException e) {}
    } for (xpos = 105; xpos >= 5; xpos -= 4){
    repaint();
    try{Thread.sleep(100);}
    catch(InterruptedException e) {}
    }
    }
    } public void start() {
    if (runner == null) {
    runner = new Thread(this);
    runner.start();
    }
    } public void stop() {
    if (runner != null) {
    runner.stop();
    runner = null;
    } }}class Stone 
    {
    int sequence ;
    int status ; //1 : Live ; Number : Dead
    int X;
    int Y;
    public int type; //1 : Black ; 0: White
    public Stone()
    {
    // Default Constructor
    }
    public Stone(int x, int y, int l_sequence, int l_status, int l_type)
    {
    X = x;
    Y = y;
    sequence = l_sequence;
    status = l_status;
    type = l_type;
    }}stone.java may be ignore :-) Let's play weiqi and develop in Java
      

  3.   

    the code too long
    let see detail...........