Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala applets - SimpleApplet demo

The ScalaDoc for the applet class is pretty thin on details on how you actually override the ui piece and add components. It says "Clients should implement the ui field. See the SimpleApplet demo for an example."

  1. Where is this SimpleApplet demo?
  2. Barring that, does anyone have some simple source code of using the Scala Applet class, rather than the JApplet class directly?

Thanks

like image 482
I82Much Avatar asked Feb 27 '23 13:02

I82Much


2 Answers

The more recent ScalaDoc may be slightly more helpful (in particular, the new version of ScalaDoc allows you to show/hide concrete members so you can focus on what you must implement).

It should be noted that you don't have to define an object named ui that extends UI. What the ScalaDoc says is both more accurate and more flexible -- "implement the ui field". Because of the Uniform Access Principle, you're free to implement the ui field as a val or an object (similarly, you can use a val or var to implement a def). The only constraints (as reflected in the ScalaDoc as val ui : UI) are that

  1. the ui has to be a UI, and
  2. the reference to the ui has to be immutable

For example:

class MainApplet extends Applet {
  val ui = new MainUI(Color.WHITE)

  class MainUI(backgroundColor: Color) extends UI {
     val mainPanel = new BoxPanel(Orientation.Vertical) {
        // different sort of swing components
        contents.append(new Button("HI"))
     }
     mainPanel.background = backgroundColor // no need for ugly _=
     contents = mainPanel

     def init(): Unit = {}
   }
}
like image 187
Aaron Novstrup Avatar answered Mar 05 '23 14:03

Aaron Novstrup


Finally found some source that shows what you need to do:

http://scala-forum.org/read.php?4,701,701

import swing._
import java.awt.Color

class MainApplet extends Applet {

  object ui extends UI {
     val mainPanel = new BoxPanel(Orientation.Vertical) {
     // different sort of swing components
     contents.append(new Button("HI"))
     }
     mainPanel.background = Color.WHITE
     contents = mainPanel

     def init():Unit = {}
  }
}

In other words you define an object named ui that extends UI. I never would have thought of that. That ScalaDoc needs some serious work.

like image 34
I82Much Avatar answered Mar 05 '23 14:03

I82Much