Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gui
- import logic.Board
- import javax.swing.JPanel
- import java.awt.Graphics
- internal class Panel : JPanel() {
- var board: Board = Board(20, 0, "plik.png")
- var board2: Board = Board(400, 0, null)
- // Game Mechanics in mouseClicked
- internal fun mouseClicked(mouseX: Int, mouseY: Int) {
- onBoardClicked(mouseX, mouseY)
- }
- // true - if click handled, false - not
- internal fun onBoardClicked(mouseX: Int, mouseY: Int): Boolean {
- val next = board.checkCoordinates(mouseX, mouseY) ?: return false
- board.selectField(next.x, next.y)
- return true
- }
- override fun paintComponent(g: Graphics) {
- g.clearRect(0, 0, this.width, this.height)
- board.draw(g)
- board2.draw(g)
- }
- }
- package logic
- import java.awt.Color
- import java.awt.Graphics
- import java.awt.Point
- import java.awt.Rectangle
- import java.awt.image.BufferedImage
- import javax.imageio.ImageIO
- import java.io.File
- class Board constructor(var x: Int, var y: Int, var path: String?){
- var blue = Color(0, 0, 255)
- var black = Color(0, 0, 0)
- var fields = Array(50) { arrayOfNulls<Rectangle>(size = 50) } // tablica pól planszy
- var selected = HashSet<Point>()
- var image : BufferedImage?
- init {
- image = if (path == null) null else ImageIO.read(File(path))
- createFields()
- }
- fun createFields() {
- for (i in 0 until size) {
- for (j in 0 until size) {
- val rectX = x + (i + 1) * width
- val rectY = y + (j + 1) * heigth
- fields[i][j] = Rectangle(rectX, rectY, width, heigth)
- if (image != null) {
- if (((image!!.getRGB(i, j) shr 16) and 0x0FF) <= 125) {
- selectField(i, j)
- }
- }
- }
- }
- }
- fun checkCoordinates(x: Int, y: Int): Point? {
- for (i in 0 until size) {
- for (j in 0 until size) {
- val rect = fields[i][j]
- if (rect!!.contains(x, y)) {
- return Point(i, j)
- }
- }
- }
- return null
- }
- fun selectField(x: Int, y: Int) {
- val point = Point(x, y)
- if (selected.contains(point)) {
- selected.remove(point)
- } else {
- selected.add(point)
- }
- }
- fun toArray() : DoubleArray{
- var result = DoubleArray(size*size) { 0.0 }
- for (i in 0 until size) {
- for (j in 0 until size) {
- val point = Point(i,j)
- if(selected.contains(point)) {
- result[j*size+i] = 1.0
- }
- }
- }
- return result;
- }
- fun BitmapRedtoArray() : DoubleArray{
- var result = DoubleArray(size*size) { 0.0 }
- for (i in 0 until size) {
- for (j in 0 until size) {
- result[j*size+i] = ((image!!.getRGB(i, j) shr 16) and 0x0FF).toDouble()
- }
- }
- return result;
- }
- fun clearSelecion() {
- selected.clear()
- }
- private fun markField(g: Graphics, rect: Rectangle, color: Color) {
- g.color = color
- g.fillRect(rect.x + 1, rect.y + 1, width - 1, heigth - 1)
- g.color = black
- }
- internal fun draw(g: Graphics) {
- // głowny szablon
- for (i in 0 until size) {
- for (j in 0 until size) {
- val rect = fields[i][j]
- g.drawRect(rect!!.x, rect.y, rect.height, rect.width)
- if (selected.contains(Point(i, j))) {
- markField(g, rect!!, blue)
- }
- }
- }
- }
- companion object {
- internal var width = 5
- internal var heigth = 5
- val size = 50
- fun mirror(point: Point): Point {
- return Point(Math.abs(point.x - 8), Math.abs(point.y - 8))
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment