1
//////////////////////////////////////////////////////////////////////
3
// JCSP ("CSP for Java") Libraries //
4
// Copyright (C) 1996-2008 Peter Welch and Paul Austin. //
5
// 2001-2004 Quickstone Technologies Limited. //
7
// This library is free software; you can redistribute it and/or //
8
// modify it under the terms of the GNU Lesser General Public //
9
// License as published by the Free Software Foundation; either //
10
// version 2.1 of the License, or (at your option) any later //
13
// This library is distributed in the hope that it will be //
14
// useful, but WITHOUT ANY WARRANTY; without even the implied //
15
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
16
// PURPOSE. See the GNU Lesser General Public License for more //
19
// You should have received a copy of the GNU Lesser General //
20
// Public License along with this library; if not, write to the //
21
// Free Software Foundation, Inc., 59 Temple Place, Suite 330, //
22
// Boston, MA 02111-1307, USA. //
24
// Author contact: P.H.Welch@kent.ac.uk //
26
//////////////////////////////////////////////////////////////////////
29
import org.jcsp.lang.*;
30
import org.jcsp.awt.*;
36
class BallsControl0 implements CSProcess {
38
private static final int RED_SPEED = 7,
42
private final ChannelOutput[] toBalls;
43
private final DisplayList displayList;
44
private final ChannelOutput toGraphics;
45
private final ChannelInput fromGraphics;
47
public BallsControl0 (final ChannelOutput[] toBalls,
48
final DisplayList displayList,
49
final ChannelOutput toGraphics,
50
final ChannelInput fromGraphics) {
51
this.toBalls = toBalls;
52
this.displayList = displayList;
53
this.toGraphics = toGraphics;
54
this.fromGraphics = fromGraphics;
57
private final static class Graphic implements GraphicsCommand.Graphic {
59
public void doGraphic (java.awt.Graphics g, java.awt.Component c) {
60
Dimension dim = c.getSize();
62
g.fillRect (0, 0, dim.width, dim.height);
66
Graphic oldGraphic = new Graphic ();
67
Graphic newGraphic = new Graphic ();
69
GraphicsCommand oldCommand = new GraphicsCommand.General (oldGraphic);
70
GraphicsCommand newCommand = new GraphicsCommand.General (newGraphic);
74
toGraphics.write (GraphicsProtocol.GET_DIMENSION);
75
final Dimension graphicsDim = (Dimension) fromGraphics.read ();
76
System.out.println ("BallsControl: graphics dimension = " + graphicsDim);
78
final CSTimer tim = new CSTimer ();
79
final long seed = tim.read ();
81
// initialise data for background colour ...
83
int colRed = 0, colGreen = 0, colBlue = 0,
84
cvRed = RED_SPEED, cvGreen = GREEN_SPEED, cvBlue = BLUE_SPEED;
86
newGraphic.colour = Color.black;
88
displayList.set (newCommand);
90
for (int i = 0; i < toBalls.length; i++) {
91
toBalls[i].write (graphicsDim);
92
toBalls[i].write (new Long (seed));
95
//final Thread me = Thread.currentThread ();
96
//me.setPriority (Thread.MAX_PRIORITY);
98
final long second = 1000; // JCSP Timer units are milliseconds
99
long interval = second / 20;
101
long timeout; // timeouts will drift ... but never mind ...
102
// long timeout = tim.read (); // timeouts won't drift ... not wanted here ...
106
timeout = tim.read () + interval; // timeouts will drift ... but never mind ...
107
// timeout += interval; // timeouts won't drift ... not wanted here ...
109
final Graphic tmpA = oldGraphic;
110
oldGraphic = newGraphic;
113
final GraphicsCommand tmpB = oldCommand;
114
oldCommand = newCommand;
118
if (colRed > 255 - RED_SPEED) cvRed = -RED_SPEED; else if (colRed < RED_SPEED) cvRed = RED_SPEED;
120
if (colGreen > 255 - GREEN_SPEED) cvGreen = -GREEN_SPEED; else if (colGreen < GREEN_SPEED) cvGreen = GREEN_SPEED;
122
if (colBlue > 255 - BLUE_SPEED) cvBlue = -BLUE_SPEED; else if (colBlue < BLUE_SPEED) cvBlue = BLUE_SPEED;
123
newGraphic.colour = new Color (colRed, colGreen, colBlue);
127
displayList.change (newCommand, 0);