2

If I run this example code

class ExampleGUIElement extends Panel
{
  preferredSize = new Dimension(100, 100)
  val rand : Random = Random
  override def paintComponent(g: Graphics2D) = g.drawLine(rand.nextInt(10),rand.nextInt(10),rand.nextInt(90) + 10 ,rand.nextInt(90) + 10)
}

object GUITest extends SimpleSwingApplication  
{
  def top = new MainFrame
  {
    contents = new ExampleGUIElement
  }
}

It obviously only shows the one line that has been draw initially. If I want a periodic repaint I normally would just make a Timer that calls repaint on the most outer JFrame. But SimpleSwingApplication does not have this method, at least I can not find it, and calling top.repaint instead just does nothing. So how do you periodically repaint the whole thing?

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228

2 Answers2

1

Consider calling peer on the top (see MainFrame), and then calling repaint on that:

top.peer.repaint()

Also, don't forget to call top.pack().

axel22
  • 32,045
  • 9
  • 125
  • 137
  • This does not work for me either. I've made a timer with override def run = GUITest.top.peer.repaint() and it still does not update. –  Apr 07 '13 at 14:33
1

I realize this is dated, but I extracted your Panel into a val and added a timer to repaint it:

def top = new MainFrame
{
    val example = new ExampleGUIElement 
    contents = example
    val timer=new javax.swing.Timer(250, Swing.ActionListener(e =>
    { 
        example.repaint()
    }))
    timer.start()
}
climmunk
  • 1,141
  • 1
  • 12
  • 29