Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 4.21 KB | None | 0 0
  1. package maze
  2.  
  3. import scala.swing._
  4. import java.awt.Color
  5.  
  6. class GUI(val originalMaze: Maze) extends SimpleSwingApplication {
  7.   val preferredWallThickness = 6
  8.   val preferredCellSize = 20
  9.   def rows = originalMaze.rows
  10.   def columns = originalMaze.columns
  11.   private val maze = new Maze(rows, columns)
  12.   private val filled = Array.fill(rows, columns)(false)
  13.   val mazePanel = new Panel {
  14.     background = Color.black
  15.     preferredSize = new Dimension(maze.columns * (preferredWallThickness + preferredCellSize) + preferredWallThickness,
  16.       maze.rows * (preferredWallThickness + preferredCellSize) + preferredWallThickness)
  17.  
  18.     override protected def paintComponent(g: Graphics2D) {
  19.       super.paintComponent(g)
  20.       val width = this.size.width
  21.       val height = this.size.height
  22.       val vWallThickness = width.toDouble / (maze.rows + maze.rows * preferredCellSize.toDouble / preferredWallThickness + 1)
  23.       val hWallThickness = height.toDouble / (maze.rows + maze.rows * preferredCellSize.toDouble / preferredWallThickness + 1)
  24.       val wallThickness = (hWallThickness min vWallThickness) max 2.0
  25.       val vCellSize = (height.toDouble - (maze.rows + 1) * wallThickness) / maze.rows
  26.       val hCellSize = (width.toDouble - (maze.columns + 1) * wallThickness) / maze.columns
  27.  
  28.       def xInLeft(c: Int): Int = {
  29.         (c * (wallThickness + hCellSize) + wallThickness).toInt
  30.       }
  31.       def xInRight(c: Int): Int = {
  32.         (c * (wallThickness + hCellSize) + wallThickness + hCellSize).toInt
  33.       }
  34.       def xOutLeft(c: Int): Int = if (c == 0) 0 else xInRight(c - 1) + 1
  35.       def xOutRight(c: Int): Int = if (c == columns - 1) width else xInLeft(c + 1) - 1
  36.       def yInBot(r: Int): Int = {
  37.         height - 1 - (r * (wallThickness + vCellSize) + wallThickness).toInt
  38.       }
  39.       def yInTop(r: Int): Int = {
  40.         height - 1 - (r * (wallThickness + vCellSize) + wallThickness + vCellSize).toInt
  41.       }
  42.       def yOutBot(r: Int): Int = if (r == 0) height else yInTop(r - 1) - 1
  43.       def yOutTop(r: Int): Int = if (r == rows - 1) 0 else yInBot(r + 1) + 1
  44.       g.setStroke(new java.awt.BasicStroke(0))
  45.       for (r <- 0 until rows) {
  46.         for (c <- 0 until columns) {
  47.           if (filled(r)(c)) g.setColor(Color.white)
  48.           else g.setColor(new Color(200, 200, 255))
  49.           g.fillRect(xInLeft(c), yInTop(r), xInRight(c) - xInLeft(c) + 1, yInBot(r) - yInTop(r) + 1)
  50.         }
  51.       }
  52.       g.setColor(Color.black)
  53.       g.setStroke(new java.awt.BasicStroke(wallThickness.toInt))
  54.       for (r <- 0 until maze.rows) {
  55.         for (c <- 0 to maze.columns) {
  56.           if (maze.hasVerticalWall(r, c)) {
  57.             g.setColor(Color.black)
  58.             g.fillRect(xOutLeft(c), yOutTop(r), xInLeft(c) - xOutLeft(c), yOutBot(r) - yOutTop(r))
  59.           } else {
  60.             g.setColor(Color.white)
  61.             g.fillRect(xOutLeft(c), yInTop(r), xInLeft(c) - xOutLeft(c), yInBot(r) - yInTop(r) + 1)
  62.           }
  63.         }
  64.       }
  65.       for (c <- 0 until maze.columns) {
  66.         for (r <- 0 to maze.rows) {
  67.           if (maze.hasHorizontalWall(r, c)) {
  68.             g.setColor(Color.black)
  69.             g.fillRect(xOutLeft(c), yInBot(r) + 1, xOutRight(c) - xOutLeft(c) + 1, yOutBot(r) - yInBot(r) + 1)
  70.           } else {
  71.             g.setColor(Color.white)
  72.             g.fillRect(xInLeft(c), yInBot(r) + 1, xInRight(c) - xInLeft(c) + 1, yOutBot(r) - yInBot(r) + 1)
  73.           }
  74.         }
  75.       }
  76.     }
  77.   }
  78.   def top = new MainFrame {
  79.     //contents = new GridPanel(2, 1) {
  80.     //val buttonPanel = new Button("Play again")
  81.     //contents ++= mazePanel :: buttonPanel :: Nil
  82.     contents = mazePanel
  83.     //}
  84.   }
  85.   def animate(delayInMS: Int = 100) = {
  86.     import Maze.Direction._
  87.     for ((r, c, d) <- originalMaze.getBreaks) {
  88.       Thread.sleep(delayInMS)
  89.       maze.breakWall(r, c, d)
  90.       filled(r)(c) = true
  91.       d match {
  92.         case Up => filled(r + 1)(c) = true
  93.         case Down => filled(r - 1)(c) = true
  94.         case Right => filled(r)(c + 1) = true
  95.         case Left => filled(r)(c - 1) = true
  96.       }
  97.       mazePanel.repaint()
  98.     }
  99.   }
  100.   def go() {
  101.     val t = top
  102.     if (t.size == new Dimension(0, 0)) t.pack()
  103.     t.centerOnScreen()
  104.     t.visible = true
  105.     animate()
  106.   }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement