// // very simple program illustrating use of graphics class // displays very simple proof-of-concept image // prints information about (some) mouse events // prints information about (some) keyboard events // changes color in display if user types 'b' or 'g' // (apparently user must click once in main window to get keyboard focus first) // import scala.swing._ import scala.swing.event._ import java.awt.Color import java.awt.Graphics2D // panel for drawing val drawingPanel = new Panel { var color = Color.green // method used to "paint" panel on screen override def paint(g:Graphics2D) { println("paint called") g.setPaint(color) // g.fill(new Rectangle(10,20,100,200)) // get width and height val w = size.width val h = size.height // rectangle half the size of the whole area, centered g.fill(new Rectangle(w/4, h/4, w/2, h/2)) } // set up to receive mouse and keyboard input requestFocus() // request keyboard focus listenTo(mouse.clicks,mouse.moves,keys) // code to be called when mouse/keyboard input received -- // mostly just prints things, but does change something in // display if user presses 'b' (short for "blue") or 'g' ("green") reactions += { case e:MousePressed => { println("mouse pressed "+e) requestFocus() } case e:MouseDragged => { println("mouse dragged "+e) requestFocus() } case e:KeyPressed => { println("key pressed "+e+" "+e.key) if (e.key == Key.Up) { println("up arrow") } } case e:KeyTyped => { println("key typed "+e.char) e.char match { case 'b' => { color = Color.blue repaint() } case 'g' => { color = Color.green repaint() } case _ => { } } } } } // main frame/window val frame = new MainFrame { title = "Hello!" contents = drawingPanel size = new Dimension(400,400) } frame.visible = true