public class ActiveCanvas extends Canvas
Canvas<TT>java.awt.Canvas</TT> with a channel interface.
All channels are optional. The toGraphics/fromGraphics channels are set by calling the ActiveCanvas.setGraphicsChannels<TT>setGraphicsChannels</TT> method. Event channels can be added to notify the occurrence of any type of Event the component generates by calling the appropriate addXXXEventChannel method. All channel connections must be made before the process is run.
Messages can be sent down the toGraphics channel at any time to configure or interrogate the component. A reply or acknowledgment is returned down the fromGraphics channel and must be read. See the table below and GraphicsProtocol for details.
Graphics operations on an ActiveCanvas are most conveniently managed by attaching, and then setting GraphicsCommands on, a DisplayList (or any user-defined object implementing the Paintable interface). This can be done either statically via the setPaintable method, or dynamically via the setGraphicsChannels<TT</TT>/<TT>> channels (see GraphicsProtocol.SetPaintable).
All channels are managed by independent internal handler processes. It is, therefore, safe for a serial application process both to service an event channel and configure the component -- no deadlock can occur.
IMPORTANT: it is essential that event channels from this process are always serviced -- otherwise the Java Event Thread will be blocked and the GUI will stop responding. A simple way to guarantee this is to use channels configured with overwriting buffers. For example:
final One2OneChannel myMouseEvent = Channel.createOne2One (new OverWriteOldestBuffer (n)); final ActiveCanvas myCanvas = new ActiveCanvas (); myCanvas.addMouseEventChannel (myMouseEvent.out ());This will ensure that the Java Event Thread will never be blocked. Slow or inattentive readers may miss rapidly generated events, but the n most recent events will always be available.
Input Channels | ||
---|---|---|
displayList | GraphicsCommand[] | See DisplayList and GraphicsCommand. |
toGraphics | GraphicsProtocol | See GraphicsProtocol. |
Output Channels | ||
fromGraphics | Object | Response to the fromGraphics message. See the documentation on GraphicsProtocol. |
componentEvent | ComponentEvent | See the addComponentEventChannel<TT>addComponentEventChannel</TT> method. |
focusEvent | FocusEvent | See the addFocusEventChannel<TT>addFocusEventChannel</TT> method. |
keyEvent | KeyEvent | See the addKeyEventChannel<TT>addKeyEventChannel</TT> method. |
mouseEvent | MouseEvent | See the addMouseEventChannel<TT>addMouseEventChannel</TT> method. |
mouseMotionEvent | MouseEvent | See the addMouseMotionEventChannel<TT>addMouseMotionEventChannel</TT> method. |
import java.awt.*; import java.awt.event.*; import org.jcsp.lang.*; import org.jcsp.util.*; import org.jcsp.awt.*; public class ActiveCanvasExample { public static void main (String argv[]) { final ActiveClosingFrame activeClosingFrame = new ActiveClosingFrame ("ActiveCanvas Example"); final Frame frame = activeClosingFrame.getActiveFrame (); final One2OneChannel mouseEvent = Channel.createOne2One (new OverWriteOldestBuffer (10)); final DisplayList displayList = new DisplayList (); final ActiveCanvas canvas = new ActiveCanvas (); canvas.addMouseEventChannel (mouseEvent.out ()); canvas.setPaintable (displayList); canvas.setSize (600, 400); frame.add (canvas); frame.pack (); frame.setVisible (true); new Parallel ( new CSProcess[] { activeClosingFrame, canvas, new CSProcess () { public void run () { final String clickMessage = "C L I C K T H E M O U S E T O E X I T"; final String clickPlea = "P L E A S E M O V E T H E M O U S E B A C K"; final GraphicsCommand[] mouseEntered = {new GraphicsCommand.SetColor (Color.cyan), new GraphicsCommand.FillRect (0, 0, 600, 400), new GraphicsCommand.SetColor (Color.black), new GraphicsCommand.DrawString (clickMessage, 145, 200)}; final GraphicsCommand[] mouseExited = {new GraphicsCommand.SetColor (Color.pink), new GraphicsCommand.FillRect (0, 0, 600, 400), new GraphicsCommand.SetColor (Color.black), new GraphicsCommand.DrawString (clickPlea, 160, 200)}; final GraphicsCommand mousePressed = new GraphicsCommand.DrawString (clickMessage, 165, 220); final GraphicsCommand mouseReleased = new GraphicsCommand.DrawString (clickMessage, 145, 200); displayList.set (mouseExited); boolean running = true; while (running) { final MouseEvent event = (MouseEvent) mouseEvent.in ().read (); switch (event.getID ()) { case MouseEvent.MOUSE_ENTERED: System.out.println ("MOUSE_ENTERED"); displayList.set (mouseEntered); break; case MouseEvent.MOUSE_EXITED: System.out.println ("MOUSE_EXITED"); displayList.set (mouseExited); break; case MouseEvent.MOUSE_PRESSED: System.out.println ("MOUSE_PRESSED"); displayList.change (mousePressed, 3); break; case MouseEvent.MOUSE_RELEASED: System.out.println ("MOUSE_RELEASED"); displayList.change (mouseReleased, 3); break; case MouseEvent.MOUSE_CLICKED: if (event.getClickCount() > 1) { System.out.println ("MOUSE_DOUBLE_CLICKED ... goodbye!"); running = false; } break; } } frame.setVisible (false); System.exit (0); } } } ).run (); } }
Fields inherited from class | Fields |
---|---|
class Canvas |
TOP_ALIGNMENT, CENTER_ALIGNMENT, BOTTOM_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, WIDTH, HEIGHT, PROPERTIES, SOMEBITS, FRAMEBITS, ALLBITS, ERROR, ABORT |
class Component |
TOP_ALIGNMENT, CENTER_ALIGNMENT, BOTTOM_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, WIDTH, HEIGHT, PROPERTIES, SOMEBITS, FRAMEBITS, ALLBITS, ERROR, ABORT |
Type | Name and description |
---|---|
void |
addComponentEventChannel(ChannelOutput componentEvent) Add a new channel to this component that will be used to notify that a ComponentEvent has occurred. |
void |
addFocusEventChannel(ChannelOutput focusEvent) Add a new channel to this component that will be used to notify that a FocusEvent has occurred. |
void |
addKeyEventChannel(ChannelOutput keyEvent) Add a new channel to this component that will be used to notify that a KeyEvent has occurred. |
void |
addMouseEventChannel(ChannelOutput mouseEvent) Add a new channel to this component that will be used to notify that a MouseEvent has occurred. |
void |
addMouseMotionEventChannel(ChannelOutput mouseMotionEvent) Add a new channel to this component that will be used to notify that a MouseMotionEvent has occurred. |
Dimension |
getMinimumSize() This method is used by system classes -- it is not really for public consumption! |
Dimension |
getPreferredSize() This method is used by system classes -- it is not really for public consumption! |
void |
paint(Graphics g) This method is used by the JVM event thread -- it is not really for public consumption. |
void |
run() The main body of this process. |
void |
setGraphicsChannels(ChannelInput toGraphics, ChannelOutput fromGraphics) Set the toGraphics/fromGraphics channels for configuring and/or examining this component. |
void |
setPaintable(Paintable paintable) Set the Paintable<TT>Paintable</TT> object that will be used by the paint and update methods of this canvas. |
void |
setSize(int requestedWidth, int requestedHeight) Request that the canvas takes the size given by the parameters. |
void |
update(Graphics g) This method is used by the JVM event thread -- it is not really for public consumption. |
Add a new channel to this component that will be used to notify that a ComponentEvent has occurred. This should be used instead of registering a ComponentListener with the component. It is possible to add more than one Channel by calling this method multiple times If the channel passed is null, no action will be taken.
NOTE: This method must be called before this process is run.
componentEvent
- the channel down which to send ComponentEvents.Add a new channel to this component that will be used to notify that a FocusEvent has occurred. This should be used instead of registering a FocusListener with the component. It is possible to add more than one Channel by calling this method multiple times If the channel passed is null, no action will be taken.
NOTE: This method must be called before this process is run.
focusEvent
- the channel down which to send FocusEvents.Add a new channel to this component that will be used to notify that a KeyEvent has occurred. This should be used instead of registering a KeyListener with the component. It is possible to add more than one Channel by calling this method multiple times If the channel passed is null, no action will be taken.
NOTE: This method must be called before this process is run.
keyEvent
- the channel down which to send KeyEvents.Add a new channel to this component that will be used to notify that a MouseEvent has occurred. This should be used instead of registering a MouseListener with the component. It is possible to add more than one Channel by calling this method multiple times If the channel passed is null, no action will be taken.
NOTE: This method must be called before this process is run.
mouseEvent
- the channel down which to send MouseEvents.Add a new channel to this component that will be used to notify that a MouseMotionEvent has occurred. This should be used instead of registering a MouseMotionListener with the component. It is possible to add more than one Channel by calling this method multiple times If the channel passed is null, no action will be taken.
NOTE: This method must be called before this process is run.
mouseMotionEvent
- the channel down which to send MouseMotionEvents.This method is used by system classes -- it is not really for public consumption!
This method is used by system classes -- it is not really for public consumption!
This method is used by the JVM event thread -- it is not really for public consumption. If setPaintable has been invoked on this canvas or a GraphicsProtocol.SetPaintable object has been sent down the toGraphics channel, this method uses the supplied Paintable<TT>Paintable</TT> object to do the painting.
The main body of this process.
Set the toGraphics/fromGraphics channels for configuring and/or examining this component. A GraphicsProtocol<TT>org.jcsp.awt.GraphicsProtocol</TT> object sent down the toGraphics channel generates the appropriate configuration or enquiry action. A reply object is always returned down the fromGraphics channel.
NOTE: This method must be called before this process is run.
toGraphics
- the channel down which GraphicsProtocol objects are sent.fromGraphics
- the reply/acknowledgement channel responding to the GraphicsProtocol object.Set the Paintable<TT>Paintable</TT> object that will be used by the paint and update methods of this canvas. If paintable is null, the paint/update methods will not be overriden.
JCSP provides a DisplayList as an example Paintable class. This can be thought of as a special form of channel. A user process at the other end writes to it by setting up and/or editing an ordered list of GraphicsCommands. This method may be used to connect the DisplayList to this ActiveCanvas. For example:
final ActiveCanvas myActiveCanvas = new ActiveCanvas (); final DisplayList draw = new DisplayList (); myActiveCanvas.setPaintable (draw); ... connect other channels (toGraphics, fromGraphics, mouseEvent, etc.) // myActiveCanvas is now ready to run
NOTE: If setPaintable is going to be invoked, this must happen before this process is run.
Alternatively, the Paintable<TT>Paintable</TT> object may be set dynamically by sending an appropriate GraphicsProtocol.SetPaintable object down the toGraphics channel.
paintable
- the object to be used for painting/updating the canvas.Request that the canvas takes the size given by the parameters. The methods getPreferredSize<TT>getPreferredSize</TT> and getMinimumSize<TT>getMinimumSize</TT> are overridden to return these dimensions.
NOTE: This method must be called before this process is run.
requestedWidth
- the requested width for the canvas.requestedHeight
- the requested height for the canvas.This method is used by the JVM event thread -- it is not really for public consumption. If setPaintable has been invoked on this canvas or a GraphicsProtocol.SetPaintable object has been sent down the toGraphics channel, this method uses the supplied Paintable<TT>Paintable</TT> object to do the updating.