From 05f44dd222bb71d88480435444dfeac7679336c7 Mon Sep 17 00:00:00 2001 From: Robinson Date: Mon, 18 Dec 2023 14:16:10 +0100 Subject: [PATCH] Added tests --- test/dorkbox/render/RenderTypeTest1.kt | 110 +++++++++++++++++++++++++ test/dorkbox/render/RenderTypeTest2.kt | 98 ++++++++++++++++++++++ 2 files changed, 208 insertions(+) create mode 100644 test/dorkbox/render/RenderTypeTest1.kt create mode 100644 test/dorkbox/render/RenderTypeTest2.kt diff --git a/test/dorkbox/render/RenderTypeTest1.kt b/test/dorkbox/render/RenderTypeTest1.kt new file mode 100644 index 0000000..f761820 --- /dev/null +++ b/test/dorkbox/render/RenderTypeTest1.kt @@ -0,0 +1,110 @@ +/* + * Copyright 2023 dorkbox, llc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dorkbox.render + +import java.awt.* +import java.awt.image.BufferStrategy +import javax.swing.JWindow +import javax.swing.SwingUtilities + + + +class RenderTypeTest1 : JWindow() { + private var squareX = 50 + private val squareY = 50 + private val squareSize = 50 + + init { + isVisible = true + setSize(WIDTH, HEIGHT) + setLocationRelativeTo(null) + ignoreRepaint = true + createBufferStrategy(2) + val bufferStrategy = bufferStrategy + val loop = Thread { + while (true) { + update() + render(bufferStrategy) + try { + Thread.sleep(10) // Adjust the sleep time based on your needs + } + catch (e: InterruptedException) { + e.printStackTrace() + } + } + } + loop.start() + } + + private fun update() { + // Update the game state (e.g., move the square) + squareX += 2 + if (squareX > WIDTH) { + squareX = -squareSize + } + } + + private fun render(bufferStrategy: BufferStrategy) { + // Get the graphics context from the buffer strategy + val g = bufferStrategy.drawGraphics + + // Draw the square directly on the JWindow + g.clearRect(0, 0, width, height) // Clear the screen + g.color = Color.BLUE + g.fillRect(squareX, squareY, squareSize, squareSize) + + // Dispose the graphics context and show the buffer + g.dispose() + bufferStrategy.show() + } + + companion object { + private const val WIDTH = 800 + private const val HEIGHT = 600 + + fun getMonitorAtLocation(pos: Point): GraphicsDevice? { + val ge = GraphicsEnvironment.getLocalGraphicsEnvironment() + val screenDevices = ge.screenDevices + + for (device1 in screenDevices) { + val gc = device1.defaultConfiguration + val screenBounds = gc.bounds + if (screenBounds.contains(pos)) { + return device1 + } + } + + return null + } + + fun showOnSameScreenAsMouse_Center(frame: Container) { + val mouseLocation = MouseInfo.getPointerInfo().location + val monitorAtMouse = getMonitorAtLocation(mouseLocation) + val bounds = monitorAtMouse!!.defaultConfiguration.bounds + frame.setLocation(bounds.x + bounds.width / 2 - frame.width / 2, bounds.y + bounds.height / 2 - frame.height / 2) + } + + @JvmStatic + fun main(args: Array) { + SwingUtilities.invokeLater { + val renderTypeTest1 = RenderTypeTest1() + showOnSameScreenAsMouse_Center(renderTypeTest1) + renderTypeTest1.isVisible = true + } + } + } +} diff --git a/test/dorkbox/render/RenderTypeTest2.kt b/test/dorkbox/render/RenderTypeTest2.kt new file mode 100644 index 0000000..c205ee8 --- /dev/null +++ b/test/dorkbox/render/RenderTypeTest2.kt @@ -0,0 +1,98 @@ +/* + * Copyright 2023 dorkbox, llc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dorkbox.render + +import dorkbox.swingActiveRender.SwingActiveRender +import java.awt.* +import javax.swing.JWindow +import javax.swing.SwingUtilities + +class RenderTypeTest2 : JWindow() { + private var squareX = 50 + private val squareY = 50 + private val squareSize = 50 + + init { + isVisible = true + setSize(WIDTH, HEIGHT) + setLocationRelativeTo(null) + ignoreRepaint = true + createBufferStrategy(2) + + SwingActiveRender.add(this) + } + + private fun update() { + // Update the game state (e.g., move the square) + squareX += 2 + if (squareX > WIDTH) { + squareX = -squareSize + } + } + + override fun paint(g11: Graphics) { + update() + + // Get the graphics context from the buffer strategy + val g = bufferStrategy.drawGraphics + + // Draw the square directly on the JWindow + g.clearRect(0, 0, width, height) // Clear the screen + g.color = Color.BLUE + g.fillRect(squareX, squareY, squareSize, squareSize) + + // Dispose the graphics context and show the buffer + g.dispose() + bufferStrategy.show() + } + + companion object { + private const val WIDTH = 800 + private const val HEIGHT = 600 + + fun getMonitorAtLocation(pos: Point): GraphicsDevice? { + val ge = GraphicsEnvironment.getLocalGraphicsEnvironment() + val screenDevices = ge.screenDevices + + for (device1 in screenDevices) { + val gc = device1.defaultConfiguration + val screenBounds = gc.bounds + if (screenBounds.contains(pos)) { + return device1 + } + } + + return null + } + + fun showOnSameScreenAsMouse_Center(frame: Container) { + val mouseLocation = MouseInfo.getPointerInfo().location + val monitorAtMouse = getMonitorAtLocation(mouseLocation) + val bounds = monitorAtMouse!!.defaultConfiguration.bounds + frame.setLocation(bounds.x + bounds.width / 2 - frame.width / 2, bounds.y + bounds.height / 2 - frame.height / 2) + } + + @JvmStatic + fun main(args: Array) { + SwingUtilities.invokeLater { + val renderTypeTest2 = RenderTypeTest2() + showOnSameScreenAsMouse_Center(renderTypeTest2) + renderTypeTest2.isVisible = true + } + } + } +}