scala/ScalaBook/chapter-06/JumpingBox.scala.orig

import scala.util.Random
import scala.swing._
import event._

class JumpingBox extends Applet {
  object ui extends UI with Reactor {
    private var mx = 0 
    private var my = 0
    private var oldSize : java.awt.Dimension = null
    private var onaroll = 0
    private var rnd = new Random()

    def init() = {
      onaroll = 0 
      val canvas = new Panel {
        opaque = false
        preferredSize = (500, 500)  
        override def paintComponent(g: java.awt.Graphics) {
          var newSize: java.awt.Dimension = getSize()
          if (oldSize == newSize) {
            // Erase old box
            g.setColor(getBackground())
            g.drawRect(mx, my, (oldSize.width / 10) - 1, 
                        (oldSize.height / 10) - 1)
          } 
          else {
            oldSize = newSize
            g.clearRect(0, 0, oldSize.width, oldSize.height)
          }
          // Calculate new position
          mx = rnd.nextInt(999)  % 
                 (oldSize.width - (oldSize.width / 10))
          my = rnd.nextInt(999) % 
                 (oldSize.height - (oldSize.height / 10))
          g.setColor(java.awt.Color.black)
          g.drawRect(0, 0, oldSize.width - 1, oldSize.height - 1)
          g.drawRect(mx, my, (oldSize.width / 10) - 1, 
                      (oldSize.height / 10) - 1)
        } //paintComponent
        listenTo(Mouse.clicks)
        reactions += {
          case MousePressed(_, p, _, _, _) => {
            var x = p.x
            var y = p.y 
            //e.consume();
            requestFocus
            if (mx < x && x < mx + getSize().width / 10 - 1 && 
                my < y && y < my + getSize().height / 10 - 1) {  //determine if hit
              if (onaroll > 0) { //not first hit
                ( onaroll % 4 ) match {   //play a sound
                  case 0 => play(getCodeBase(), "sounds/tiptoe.thru.the.tulips.au")
                  case 1 => play(getCodeBase(), "sounds/danger.au")
                  case 2 => play(getCodeBase(), "sounds/adapt-or-die.au")
                  case 3 => play(getCodeBase(), "sounds/cannot.be.completed.au")
                }
                onaroll += 1
                if (onaroll > 5) 
                  getAppletContext().showStatus("You're on your way to THE HALL OF FAME:"
                                    + onaroll + "Hits!")
                else 
                 getAppletContext().showStatus("YOU'RE ON A ROLL:" 
                                                  + onaroll + "Hits!")
              } // end of "not first hit" 
              else {  //first hit
                getAppletContext().showStatus("HIT IT AGAIN! AGAIN!")
                play(getCodeBase(), "sounds/that.hurts.au")
                onaroll = 1
              } // end of "first hit"
            } // end of "determine if hit|
            else { //miss
              getAppletContext().showStatus("You hit nothing at (" + x + ", " 
                                          + y + "), exactly");
              play(getCodeBase(), "sounds/thin.bell.au");
              onaroll = 0
            }
            repaint
          }       
        }
        listenTo(Mouse.moves)
        reactions += {
          case MouseEntered(_,_,_) =>  repaint
          case MouseExited(_,_,_)  => repaint
          case MouseMoved(_, p, _) => { 
            if ( (p.x % 3 == 0) && (p.y % 3 == 0) ) 
              repaint
          }
        }
      } 
      contents = canvas
    }  // end of init()
  } // ui
} // JumpingBox