bartoshr

Panel

Nov 20th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.00 KB | None | 0 0
  1. package gui
  2.  
  3. import logic.Board
  4. import javax.swing.JPanel
  5. import java.awt.Graphics
  6.  
  7. internal class Panel : JPanel() {
  8.     var board: Board = Board(20, 0, "plik.png")
  9.     var board2: Board = Board(400, 0, null)
  10.  
  11.     // Game Mechanics in mouseClicked
  12.     internal fun mouseClicked(mouseX: Int, mouseY: Int) {
  13.         onBoardClicked(mouseX, mouseY)
  14.     }
  15.  
  16.     // true - if click handled, false - not
  17.     internal fun onBoardClicked(mouseX: Int, mouseY: Int): Boolean {
  18.         val next = board.checkCoordinates(mouseX, mouseY) ?: return false
  19.         board.selectField(next.x, next.y)
  20.         return true
  21.     }
  22.  
  23.     override fun paintComponent(g: Graphics) {
  24.         g.clearRect(0, 0, this.width, this.height)
  25.         board.draw(g)
  26.         board2.draw(g)
  27.     }
  28.  
  29.  
  30. }
  31.  
  32.  
  33. package logic
  34.  
  35. import java.awt.Color
  36. import java.awt.Graphics
  37. import java.awt.Point
  38. import java.awt.Rectangle
  39. import java.awt.image.BufferedImage
  40. import javax.imageio.ImageIO
  41. import java.io.File
  42.  
  43.  
  44. class Board constructor(var x: Int, var y: Int, var path: String?){
  45.  
  46.     var blue = Color(0, 0, 255)
  47.     var black = Color(0, 0, 0)
  48.     var fields = Array(50) { arrayOfNulls<Rectangle>(size = 50) } // tablica pól planszy
  49.     var selected = HashSet<Point>()
  50.     var image : BufferedImage?
  51.  
  52.     init {
  53.         image = if (path == null) null else ImageIO.read(File(path))
  54.         createFields()
  55.     }
  56.  
  57.     fun createFields() {
  58.         for (i in 0 until size) {
  59.             for (j in 0 until size) {
  60.                 val rectX = x + (i + 1) * width
  61.                 val rectY = y + (j + 1) * heigth
  62.                 fields[i][j] = Rectangle(rectX, rectY, width, heigth)
  63.  
  64.                 if (image != null) {
  65.                     if (((image!!.getRGB(i, j) shr 16) and 0x0FF) <= 125) {
  66.                         selectField(i, j)
  67.                     }
  68.  
  69.                 }
  70.             }
  71.         }
  72.     }
  73.  
  74.  
  75.     fun checkCoordinates(x: Int, y: Int): Point? {
  76.         for (i in 0 until size) {
  77.             for (j in 0 until size) {
  78.                 val rect = fields[i][j]
  79.                 if (rect!!.contains(x, y)) {
  80.                     return Point(i, j)
  81.                 }
  82.             }
  83.         }
  84.         return null
  85.     }
  86.  
  87.     fun selectField(x: Int, y: Int) {
  88.         val point = Point(x, y)
  89.         if (selected.contains(point)) {
  90.             selected.remove(point)
  91.         } else {
  92.             selected.add(point)
  93.         }
  94.     }
  95.  
  96.     fun toArray() : DoubleArray{
  97.         var result = DoubleArray(size*size) { 0.0 }
  98.         for (i in 0 until size) {
  99.             for (j in 0 until size) {
  100.                 val point =  Point(i,j)
  101.                 if(selected.contains(point)) {
  102.                     result[j*size+i] = 1.0
  103.                 }
  104.             }
  105.         }
  106.         return result;
  107.     }
  108.  
  109.  
  110.     fun BitmapRedtoArray() : DoubleArray{
  111.         var result = DoubleArray(size*size) { 0.0 }
  112.         for (i in 0 until size) {
  113.             for (j in 0 until size) {
  114.                 result[j*size+i] = ((image!!.getRGB(i, j) shr 16) and 0x0FF).toDouble()
  115.             }
  116.         }
  117.         return result;
  118.     }
  119.  
  120.  
  121.     fun clearSelecion() {
  122.         selected.clear()
  123.     }
  124.  
  125.     private fun markField(g: Graphics, rect: Rectangle, color: Color) {
  126.         g.color = color
  127.         g.fillRect(rect.x + 1, rect.y + 1, width - 1, heigth - 1)
  128.         g.color = black
  129.     }
  130.  
  131.     internal fun draw(g: Graphics) {
  132.         // głowny szablon
  133.         for (i in 0 until size) {
  134.             for (j in 0 until size) {
  135.                 val rect = fields[i][j]
  136.                 g.drawRect(rect!!.x, rect.y, rect.height, rect.width)
  137.                 if (selected.contains(Point(i, j))) {
  138.                     markField(g, rect!!, blue)
  139.                 }
  140.             }
  141.         }
  142.  
  143.     }
  144.  
  145.     companion object {
  146.         internal var width = 5
  147.         internal var heigth = 5
  148.         val size = 50
  149.  
  150.         fun mirror(point: Point): Point {
  151.             return Point(Math.abs(point.x - 8), Math.abs(point.y - 8))
  152.         }
  153.     }
  154.  
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment