Commit e03783ff authored by h2b's avatar h2b

AWT event driver mounted on AWT screen driver.

parent efb0693a
......@@ -17,7 +17,8 @@
*/
package de.h2b.scala.lib.simgraf.driver
import java.awt.event.{ KeyEvent AwtKeyEvent, KeyListener, MouseEvent AwtMouseEvent, MouseListener }
import java.awt.event.{ KeyEvent AwtKeyEvent, KeyListener AwtKeyListener,
MouseEvent AwtMouseEvent, MouseListener AwtMouseListener }
import de.h2b.scala.lib.simgraf.event._
import de.h2b.scala.lib.simgraf.Pixel
......@@ -27,45 +28,50 @@ import javax.swing.SwingUtilities
* @since 1.2.0
* @author h2b
*/
trait AwtEventDriver extends KeyListener with MouseListener {
trait AwtEventDriver extends AwtScreenDriver {
import AwtEventDriver._
override protected def keyListenerOption: Option[AwtKeyListener] = Some(KeyListener)
override protected def mouseListenerOption: Option[AwtMouseListener] = Some(MouseListener)
def keyPressed (e: AwtKeyEvent): Unit = {
//unused in this implementation
}
object KeyListener extends AwtKeyListener {
def keyReleased (e: AwtKeyEvent): Unit = {
//unused in this implementation
}
def keyPressed (e: AwtKeyEvent): Unit = {
//unused in this implementation
}
def keyTyped (e: AwtKeyEvent): Unit = {
publisher ! keyEvent(e)
}
def keyReleased (e: AwtKeyEvent): Unit = {
//unused in this implementation
}
def mouseClicked (e: AwtMouseEvent): Unit = {
publisher ! mouseEvent(e)
}
def keyTyped (e: AwtKeyEvent): Unit = {
publisher ! keyEvent(e)
}
def mouseEntered (e: AwtMouseEvent): Unit = {
//unused in this implementation
}
def mouseExited (e: AwtMouseEvent): Unit = {
//unused in this implementation
}
object MouseListener extends AwtMouseListener {
def mousePressed (e: AwtMouseEvent): Unit = {
//unused in this implementation
}
def mouseClicked (e: AwtMouseEvent): Unit = {
publisher ! mouseEvent(e)
}
def mouseReleased (e: AwtMouseEvent): Unit = {
//unused in this implementation
}
def mouseEntered (e: AwtMouseEvent): Unit = {
//unused in this implementation
}
}
def mouseExited (e: AwtMouseEvent): Unit = {
//unused in this implementation
}
object AwtEventDriver {
def mousePressed (e: AwtMouseEvent): Unit = {
//unused in this implementation
}
def mouseReleased (e: AwtMouseEvent): Unit = {
//unused in this implementation
}
}
private def keyEvent (e: AwtKeyEvent) =
KeyEvent(e.getKeyChar)
......@@ -92,7 +98,6 @@ object AwtEventDriver {
result
}
//TODO: flip y
private def position (e: AwtMouseEvent) = Pixel(e.getX, e.getY)
private def position (e: AwtMouseEvent) = Pixel(e.getX, flipV(e.getY))
}
......@@ -17,15 +17,16 @@
*/
package de.h2b.scala.lib.simgraf.driver
import java.awt.{ Color => AwtColor, Dimension, FileDialog, Frame, Graphics, GraphicsEnvironment, Rectangle, RenderingHints }
import java.awt.event.{ ActionEvent, ActionListener }
import java.awt.{ Color => AwtColor, Dimension, FileDialog, Frame, Graphics,
GraphicsEnvironment, Rectangle, RenderingHints }
import java.awt.event.{ ActionEvent, ActionListener, KeyListener, MouseListener }
import java.awt.image.BufferedImage
import java.io.{ File, IOException }
import scala.collection.JavaConverters.mapAsJavaMapConverter
import de.h2b.scala.lib.simgraf.{ Color, Pixel }
import de.h2b.scala.lib.util.Logger
import de.h2b.scala.lib.util.{ Level, Logger }
import de.h2b.scala.lib.util.config.BundleConfig
import javax.imageio.ImageIO
import javax.swing.{ JFrame, JMenu, JMenuBar, JMenuItem, JPanel, WindowConstants }
......@@ -40,6 +41,8 @@ trait AwtScreenDriver extends ScreenDriver {
import AwtScreenDriver._
import AwtScreenDriver.Localizer._
private val log = Logger(getClass) at Level.Debug
private val conf = env.getDefaultScreenDevice.getDefaultConfiguration
private val image = conf.createCompatibleImage(width, height)
private val graphics = image.createGraphics()
......@@ -57,14 +60,25 @@ trait AwtScreenDriver extends ScreenDriver {
graphics.setColor(toAwtColor(_activeColor))
}
private val panel = new JPanel {
protected def keyListenerOption: Option[KeyListener] = None
protected def mouseListenerOption: Option[MouseListener] = None
private lazy val panel = new JPanel {
override protected def paintComponent(g: Graphics): Unit = {
super.paintComponent(g)
g.drawImage(image, 0, 0, null)
}
for (l keyListenerOption) {
addKeyListener(l)
log.config("key listener installed")
}
for (l mouseListenerOption) {
addMouseListener(l)
log.config("mouse listener installed")
}
}
private val frame = new JFrame {
private lazy val frame = new JFrame {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
setLocation(p0.x, p0.y)
setTitle(title)
......@@ -207,7 +221,7 @@ trait AwtScreenDriver extends ScreenDriver {
object AwtScreenDriver {
private val log = Logger(classOf[AwtScreenDriver].getName)
private val log = Logger(getClass)
private val env = GraphicsEnvironment.getLocalGraphicsEnvironment
private val bounds = if (env.isHeadlessInstance()) new Rectangle(0,0,0,0) else env.getMaximumWindowBounds
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment