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."
Where is this SimpleApplet demo?
Barring that, does anyone have some simple source code of using the Scala Applet class, rather than the JApplet class directly?
Thanks
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
the ui has to be a UI, and
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 = {}
}
}
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.
Related
I'm dabbling with game design and trying to create some characters for the game. Right now I've just created a single MovieClip that contains a rectangle. The MovieClip symbol extends a class that I've created in Flash Builder that implements the logic of a monster. I can then drag an instance of this monster symbol from the library to the stage and the code works when I run the simulation. So far, so good.
Now I want to create several monsters, all slightly different:
public class Monster extends MovieClip
{
public var isFriendly:Boolean = true;
public var strength:int = 10;
public var catchPhrase:String = "Booyah!";
public function Monster()
{
}
}
One way to do this is to write a new class for each monster that extends Monster and sets the properties I want in the constructor (I'd also have to create a unique symbol in the library for each of these variations too). However, this seems to be overkill if my monsters only differ by their property values.
Looking at the Flash Professional use interface, I see that at the very bottom of the Properties panel is a section that looks like a small table headed by 'Properties/Value'. Can I use this to somehow set the properties of my classes from within the Flash Professional UI? I can't find any info on how this is used.
Okay, I figured it out. The key is converting my symbol into a flash Component.
First I edited my ActionScript class to export the properties I wanted to set (including the Inspectable tag):
public class Monster
{
private var _catchPhrase:String;
public function Monster()
{
}
public function get catchPhrase():String
{
return _catchPhrase;
}
[Inspectable(name = "catchPhrase", type = String, defaultValue = "Booyah!")]
public function set catchPhrase(value:String):void
{
_catchPhrase = value;
}
}
Then I right clicked on the Monster symbol in my library and selected 'Component Definition...'. This brought up the Component Definition dialog. I then entered the name of my ActionScript class in the Class field and clicked the checkmark to validate it. Flash then automatically generated the properties I needed.
I also found this tutorial helpful:
http://redbjarne.wordpress.com/actionscript-3-0-custom-components-from-hell/
I have defined a renderer with some Labels, TextInputs and Buttons and call "setStyle" to set their colors and font when i create it.
If i add the renderer in some normal panels, the style works fine.
If i put it in a TileList object, the style will be lost
code like this:
public class MyRenderer extends UIComponent implements ICellRenderer
{
public function MyRenderer()
{
private var m_label = new Label();
m_label.setStyle("textFormat", someTextFormat);
addChild(m_label);
}
}
var m_tileList:TileList = new TileList();
m_tileList.setStyle("cellRenderer", MyRenderer)
m_tileList.addItem({});//the renderer showed but the "someTextFormat" lose
It's definitely more difficult finding information on how to use the fl.controls components since Adobe came out with CS 6. The information on how to use stules with them is here.
Try something like
StyleManager.setComponentStyle(MyRenderer, "textFormat", someTextFormat);
I am trying to add a ScrollBar. The ScrollBar will iterate through the documents displayed. However, I am having trouble receiving an event when the scroll bar changes. I'm not sure what I need to listen to, and I'm not sure what event I should be responding to. I tried the following and I get some events, but I don't think these are the ideal events to handle.
listenTo(scrollBar)
listenTo(scrollBar.keys)
listenTo(scrollBar.mouse.moves)
listenTo(scrollBar.mouse.wheel)
listenTo(scrollBar.mouse.clicks)
For example, I only get MouseClicked, MousePressed, and MouseReleased when I click inside the scrollbar--not when I click on the arrows to actually change the value.
I found this discussion about scroll bars not receiving events properly, but it's two years old. As far as I can tell, the author did not follow up an file a ticket. Maybe he found a workaround.
Any ideas?
Good question. Clicking on arrows isn't handled by ScrollBar, it's handled by ScrollBarUI. I believe that default implementation(or at least base class for majority of ScrollBarUI implementations) is BasicScrollBarUI.
If you'll take a look into source of javax.swing.plaf.basic.BasicScrollBarUI it has incrButton and decrButton buttons and they are components you want listen to.
P.S. I had similar need for having custom (key) listener for my Slider and having custom ui which exposes needed components/model(as you could see almost all components are protected so you easy could access them in subclasses and expose via public getters) worked great for me. I did that in plain java though, maybe in scala you can listen to buttons just by specyfying property name.
One more piece of Scala Swing being broken. The Adjustable trait seems to be completely hollow, nothing is wired.
The following works:
class ScrollBarAlive extends swing.ScrollBar {
me =>
peer.addAdjustmentListener(new java.awt.event.AdjustmentListener {
def adjustmentValueChanged(e: java.awt.event.AdjustmentEvent) {
publish(new swing.event.ValueChanged(me))
}
})
}
Test:
import swing._
object ScrollBarTest extends SimpleSwingApplication {
lazy val top = new Frame {
val label = new Label { text = "0" }
val scroll = new ScrollBarAlive {
orientation = Orientation.Horizontal
listenTo(this)
reactions += {
case event.ValueChanged(_) =>
label.text = value.toString + (if (valueIsAjusting) " A" else "")
}
}
contents = new BorderPanel {
add(label, BorderPanel.Position.North)
add(scroll, BorderPanel.Position.South)
}
pack().centerOnScreen()
open()
}
}
A proper implementation would also introduce a subtype of AdjustingEvent.
This is my first experiment using Swing with Scala, and have a few questions about my code below. All it does is to produce a window with a coloured rectangle that changes colour. Please feel free to answer one or any of the questions.
1) I used a Java ActionListener below because I couldn't work out how to get javax.swing.Timer to work as a Publisher. Is there a way to use the Scala model, i.e. listenTo() - or is this the way to do it?
2) My overridden preferredSize value in the Panel doesn't seem to work: the window comes up minimized. In my Java version I override the getPreferredSize method, but there is no such method in Panel, so I assumed this is the way to do it, but why doesn't it work?
3) paintComponent isn't documented at all in the Scala API documentation. I assume this is because it is protected access in Java, but it seems like an oversight. Am I correct to override paintComponent or is it hidden because I'm supposed to use the documented paint method instead?
4) Scala doesn't seem to have getWidth() and getHeight() methods on components - is it standard to use size.width and size.height?
import swing._
import java.awt.{Graphics, Color}
import java.awt.event.{ActionEvent, ActionListener}
import javax.swing.Timer
object ColorPanel extends SimpleSwingApplication {
private var c: Color = new Color(0)
def top = new MainFrame {
title = "Flash!"
contents = p
}
val p = new Panel with ActionListener {
override val preferredSize = new Dimension(200, 200)
override def paintComponent(g: Graphics2D) {
g.setColor(c)
g.fillRect(0, 0, size.width, size.height)
}
def actionPerformed(e: ActionEvent) {
c = new Color((c.getRGB() + 1000) % 16777216)
repaint
}
}
val timer = new Timer(100, p)
timer.start()
}
No immediate answer. But your approach is certainly ok. I don't see though why your observer should be the panel. I would create an anonymous ActionListener directly with the timer, and instead add a specific method to that panel, like def animateColor() { ... }
You can use preferredSize = new Dimension(200, 200)
According to this quasi official document, yes : http://www.scala-lang.org/sid/8 (section 6 Custom Painting)
A bit stupid indeed to instantiate a new Dimension all the time. But if you look exactly at the example the SID, it does the same, uses size.height. In super high performance code, you may want to call directly into the underlying peer (peer.getWidth)
This is a continuation to my last question. I used the MVC pattern with swing components and code goes like this
import scala.swing
import scala.swing.event._
case object MyBusinessEvent extends Event
class MyController extends Publisher {
val form = new MyForm
listenTo(form)
reactions += {
case MyBusinessEvent => //handle event code here
}
}
class MyForm extends Publisher {
val ui = new GridBagPanel {
val c = new Constraints
.... more code here
}
val button1 = new Button("Button 1")
//add button to panel
listenTo(button1)
reactions += {
case ButtonClicked(_) => publish(MyBusinessEvent)
}
}
However with multiple buttons the program hangs up and seems to stop publishing events. Is there any way to fix this? Thanks
Although from the truncated example it's not clear what the problem is, my guess is you are creating a cycle which unfortunately can happen quite easy with swing and MVC. that is, one model gets updated as part of listening to another model, and that again triggers an update in the other model. In java swing you have two choices,
a) temporarily remove the listener that is updating the model (e.g. do a removeActionListener before calling setSelectedItem on a JComboBox, and then afterwards re-register with addActionListener)
b) check for the event source (getSource on an java.util.EventObject) and ignore events in the model that originated from that very same model.
Now scala swing is more simplified, so you don't have event object and event sources. You can add though a source in your custom event case class. And it might be feasible to remove the reaction and re-add it after setting a model's state.
To avoid the cycles mentioned in 0__'s answer I have a trait like this:
trait Editable extends Publisher {
private var _editing = false
def editing = _editing
def editing_=(b: Boolean) = _editing = b
override def publish(e: Event) {
if(!editing) super.publish(e)
}
}
which I mix-in in the components I want to have more control about the events fired. So, when I want to change something without firing any event, I simply put the code between editing = true and editing = false.