001: // $Id: DrawApplet.java,v 1.5 2005/05/30 16:14:36 belaban Exp $
002:
003: package org.jgroups.demos.applets;
004:
005: import java.applet.Applet;
006: import java.awt.*;
007: import java.awt.event.ActionEvent;
008: import java.awt.event.ActionListener;
009: import java.awt.event.MouseEvent;
010: import java.awt.event.MouseMotionListener;
011: import java.io.ByteArrayInputStream;
012: import java.io.ByteArrayOutputStream;
013: import java.io.DataInputStream;
014: import java.io.DataOutputStream;
015: import java.util.Random;
016: import java.util.Vector;
017: import org.jgroups.*;
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020:
021: public class DrawApplet extends Applet implements Runnable,
022: MouseMotionListener, ActionListener {
023: private Graphics graphics = null;
024: private Panel panel = null, sub_panel = null;
025: private final ByteArrayOutputStream out = new ByteArrayOutputStream();
026: private DataOutputStream outstream;
027: private DataInputStream instream;
028: private final Random random = new Random(System.currentTimeMillis());
029: private Button clear_button, leave_button;
030: private Label mbr_label;
031: private final Font default_font = new Font("Helvetica", Font.PLAIN,
032: 12);
033: private final String groupname = "DrawGroup";
034: private Channel channel = null;
035: private Thread receiver = null;
036: private int member_size = 1;
037: private int red = 0, green = 0, blue = 0;
038: private Color default_color = null;
039:
040: private final ChannelFactory factory = new JChannelFactory();
041: private String props = "TUNNEL(router_host=janet;router_port=12002):"
042: + "PING(gossip_host=janet;gossip_port=12002):"
043: + "FD:STABLE:NAKACK:UNICAST:FRAG:FLUSH:GMS:VIEW_ENFORCER:QUEUE";
044:
045: private final Vector members = new Vector();
046: private boolean fl = true;
047: Log log = LogFactory.getLog(getClass());
048:
049: public void init() {
050: System.out.println("INIT");
051: setLayout(new BorderLayout());
052:
053: String tmp_props = getParameter("properties");
054: if (tmp_props != null) {
055: System.out.println("Setting parameters " + tmp_props);
056: props = tmp_props;
057: }
058:
059: try {
060: channel = factory.createChannel(props);
061: showStatus("Connecting to group " + groupname);
062: channel.connect(groupname);
063: } catch (Exception e) {
064: log.error(e);
065: }
066: receiver = new Thread(this , "DrawThread");
067: receiver.start();
068: go();
069: }
070:
071: public void start() {
072: System.out.println("------- START");
073: }
074:
075: public void destroy() {
076: System.out.println("------- DESTROY");
077:
078: if (receiver != null && receiver.isAlive()) {
079: fl = false;
080: receiver.interrupt();
081: try {
082: receiver.join(1000);
083: } catch (Exception ex) {
084: }
085: }
086: receiver = null;
087: showStatus("Disconnecting from " + groupname);
088: channel.disconnect();
089: showStatus("Disconnected");
090: }
091:
092: public void paint(Graphics g) {
093: Rectangle bounds = panel.getBounds();
094: Color old = graphics.getColor();
095:
096: if (bounds == null || graphics == null)
097: return;
098:
099: graphics.setColor(Color.black);
100: graphics.drawRect(0, 0, bounds.width - 1, bounds.height - 1);
101: graphics.setColor(old);
102: }
103:
104: private void selectColor() {
105: red = (Math.abs(random.nextInt()) % 255);
106: green = (Math.abs(random.nextInt()) % 255);
107: blue = (Math.abs(random.nextInt()) % 255);
108: default_color = new Color(red, green, blue);
109: }
110:
111: public void go() {
112: try {
113: panel = new Panel();
114: sub_panel = new Panel();
115: resize(200, 200);
116: add("Center", panel);
117: clear_button = new Button("Clear");
118: clear_button.setFont(default_font);
119: clear_button.addActionListener(this );
120: leave_button = new Button("Exit");
121: leave_button.setFont(default_font);
122: leave_button.addActionListener(this );
123: mbr_label = new Label("0 mbr(s)");
124: mbr_label.setFont(default_font);
125: sub_panel.add("South", clear_button);
126: sub_panel.add("South", leave_button);
127: sub_panel.add("South", mbr_label);
128: add("South", sub_panel);
129: panel.addMouseMotionListener(this );
130: setVisible(true);
131: mbr_label.setText(member_size + " mbrs");
132: graphics = panel.getGraphics();
133: selectColor();
134: graphics.setColor(default_color);
135: panel.setBackground(Color.white);
136: clear_button.setForeground(Color.blue);
137: leave_button.setForeground(Color.blue);
138: } catch (Exception e) {
139: log.error(e);
140: return;
141: }
142: }
143:
144: public void run() {
145: Object tmp;
146: Message msg = null;
147: int my_x = 10, my_y = 10, r = 0, g = 0, b = 0;
148:
149: while (fl) {
150: my_x = 10;
151: my_y = 10;
152: try {
153: tmp = channel.receive(0);
154: if (tmp instanceof View) {
155: viewAccepted((View) tmp);
156: continue;
157: }
158: if (!(tmp instanceof Message))
159: continue;
160: msg = (Message) tmp;
161:
162: if (msg == null || msg.getLength() == 0) {
163: log
164: .error("DrawApplet.run(): msg or msg.buffer is null !");
165: continue;
166: }
167:
168: instream = new DataInputStream(
169: new ByteArrayInputStream(msg.getRawBuffer(),
170: msg.getOffset(), msg.getLength()));
171: r = instream.readInt(); // red
172: if (r == -13) {
173: clearPanel();
174: continue;
175: }
176: g = instream.readInt(); // green
177: b = instream.readInt(); // blue
178: my_x = instream.readInt();
179: my_y = instream.readInt();
180: } catch (ChannelNotConnectedException conn) {
181: break;
182: } catch (Exception e) {
183: log.error(e);
184: }
185: if (graphics != null) {
186: graphics.setColor(new Color(r, g, b));
187: graphics.fillOval(my_x, my_y, 10, 10);
188: graphics.setColor(default_color);
189: }
190: }
191: }
192:
193: /* --------------- Callbacks --------------- */
194:
195: public void mouseMoved(MouseEvent e) {
196: }
197:
198: public void mouseDragged(MouseEvent e) {
199: int tmp[] = new int[1], x, y;
200:
201: tmp[0] = 0;
202: x = e.getX();
203: y = e.getY();
204:
205: graphics.fillOval(x, y, 10, 10);
206:
207: try {
208: out.reset();
209: outstream = new DataOutputStream(out);
210: outstream.writeInt(red);
211: outstream.writeInt(green);
212: outstream.writeInt(blue);
213: outstream.writeInt(x);
214: outstream.writeInt(y);
215: channel.send(new Message(null, null, out.toByteArray()));
216: out.reset();
217: } catch (Exception ex) {
218: log.error(ex);
219: }
220: }
221:
222: public void clearPanel() {
223: Rectangle bounds = null;
224: if (panel == null || graphics == null)
225: return;
226:
227: bounds = panel.getBounds();
228: graphics.clearRect(1, 1, bounds.width - 2, bounds.height - 2);
229:
230: }
231:
232: public void sendClearPanelMsg() {
233: int tmp[] = new int[1];
234: tmp[0] = 0;
235:
236: clearPanel();
237:
238: try {
239: out.reset();
240: outstream = new DataOutputStream(out);
241: outstream.writeInt(-13);
242: channel.send(new Message(null, null, out.toByteArray()));
243: outstream.flush();
244: } catch (Exception ex) {
245: log.error(ex);
246: }
247: }
248:
249: public void actionPerformed(ActionEvent e) {
250: String command = e.getActionCommand();
251: if (command == "Clear") {
252: System.out.println("Members are " + members);
253: sendClearPanelMsg();
254: } else if (command == "Exit") {
255: try {
256: destroy();
257: setVisible(false);
258: } catch (Exception ex) {
259: log.error(ex);
260: }
261:
262: } else
263: System.out.println("Unknown action");
264: }
265:
266: public void viewAccepted(View v) {
267: Vector mbrs = v.getMembers();
268: if (v != null) {
269: System.out.println("View accepted: " + v);
270: member_size = v.size();
271:
272: if (mbr_label != null)
273: mbr_label.setText(member_size + " mbr(s)");
274:
275: members.removeAllElements();
276: for (int i = 0; i < mbrs.size(); i++)
277: members.addElement(mbrs.elementAt(i));
278: }
279: }
280:
281: }
|