~ubuntu-branches/ubuntu/precise/jcsp/precise

« back to all changes in this revision

Viewing changes to src/jcsp-demos/balls/balls0/BallsControl0.java

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2010-06-20 18:12:26 UTC
  • Revision ID: james.westby@ubuntu.com-20100620181226-8yg8d9rjjjiuy7oz
Tags: upstream-1.1-rc4
ImportĀ upstreamĀ versionĀ 1.1-rc4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
    //////////////////////////////////////////////////////////////////////
 
2
    //                                                                  //
 
3
    //  JCSP ("CSP for Java") Libraries                                 //
 
4
    //  Copyright (C) 1996-2008 Peter Welch and Paul Austin.            //
 
5
    //                2001-2004 Quickstone Technologies Limited.        //
 
6
    //                                                                  //
 
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       //
 
11
    //  version.                                                        //
 
12
    //                                                                  //
 
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     //
 
17
    //  details.                                                        //
 
18
    //                                                                  //
 
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.                                     //
 
23
    //                                                                  //
 
24
    //  Author contact: P.H.Welch@kent.ac.uk                             //
 
25
    //                                                                  //
 
26
    //////////////////////////////////////////////////////////////////////
 
27
 
 
28
 
 
29
import org.jcsp.lang.*;
 
30
import org.jcsp.awt.*;
 
31
import java.awt.*;
 
32
 
 
33
/**
 
34
 * @author P.H. Welch
 
35
 */
 
36
class BallsControl0 implements CSProcess {
 
37
 
 
38
  private static final int RED_SPEED = 7,
 
39
                           GREEN_SPEED = 13,
 
40
                           BLUE_SPEED = 19;
 
41
 
 
42
  private final ChannelOutput[] toBalls;
 
43
  private final DisplayList displayList;
 
44
  private final ChannelOutput toGraphics;
 
45
  private final ChannelInput fromGraphics;
 
46
 
 
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;
 
55
  }
 
56
 
 
57
  private final static class Graphic implements GraphicsCommand.Graphic {
 
58
    public Color colour;
 
59
    public void doGraphic (java.awt.Graphics g, java.awt.Component c) {
 
60
      Dimension dim = c.getSize();
 
61
      g.setColor (colour);
 
62
      g.fillRect (0, 0, dim.width, dim.height);
 
63
    }
 
64
  }
 
65
 
 
66
  Graphic oldGraphic = new Graphic ();
 
67
  Graphic newGraphic = new Graphic ();
 
68
 
 
69
  GraphicsCommand oldCommand = new GraphicsCommand.General (oldGraphic);
 
70
  GraphicsCommand newCommand = new GraphicsCommand.General (newGraphic);
 
71
 
 
72
  public void run() {
 
73
 
 
74
    toGraphics.write (GraphicsProtocol.GET_DIMENSION);
 
75
    final Dimension graphicsDim = (Dimension) fromGraphics.read ();
 
76
    System.out.println ("BallsControl: graphics dimension = " + graphicsDim);
 
77
 
 
78
    final CSTimer tim = new CSTimer ();
 
79
    final long seed = tim.read ();
 
80
 
 
81
    // initialise data for background colour ...
 
82
 
 
83
    int colRed = 0, colGreen = 0, colBlue = 0,
 
84
        cvRed = RED_SPEED,  cvGreen = GREEN_SPEED,  cvBlue = BLUE_SPEED;
 
85
 
 
86
    newGraphic.colour = Color.black;
 
87
 
 
88
    displayList.set (newCommand);
 
89
 
 
90
    for (int i = 0; i < toBalls.length; i++) {
 
91
      toBalls[i].write (graphicsDim);
 
92
      toBalls[i].write (new Long (seed));
 
93
    }
 
94
 
 
95
    //final Thread me = Thread.currentThread ();
 
96
    //me.setPriority (Thread.MAX_PRIORITY);
 
97
 
 
98
    final long second = 1000;  // JCSP Timer units are milliseconds
 
99
    long interval = second / 20;
 
100
 
 
101
    long timeout;                         // timeouts will drift ... but never mind ...
 
102
    // long timeout = tim.read ();        // timeouts won't drift ... not wanted here ...
 
103
 
 
104
    while (true) {
 
105
 
 
106
        timeout = tim.read () + interval;   // timeouts will drift ... but never mind ...
 
107
        // timeout += interval;             // timeouts won't drift ... not wanted here ...
 
108
 
 
109
        final Graphic tmpA = oldGraphic;
 
110
        oldGraphic = newGraphic;
 
111
        newGraphic = tmpA;
 
112
 
 
113
        final GraphicsCommand tmpB = oldCommand;
 
114
        oldCommand = newCommand;
 
115
        newCommand = tmpB;
 
116
 
 
117
        colRed += cvRed;
 
118
        if (colRed > 255 - RED_SPEED) cvRed = -RED_SPEED; else if (colRed < RED_SPEED) cvRed = RED_SPEED;
 
119
        colGreen += cvGreen;
 
120
        if (colGreen > 255 - GREEN_SPEED) cvGreen = -GREEN_SPEED; else if (colGreen < GREEN_SPEED) cvGreen = GREEN_SPEED;
 
121
        colBlue += cvBlue;
 
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);
 
124
 
 
125
        tim.after (timeout);
 
126
 
 
127
        displayList.change (newCommand, 0);
 
128
 
 
129
     }
 
130
 
 
131
  }
 
132
 
 
133
}