Differences between revisions 5 and 7 (spanning 2 versions)
Revision 5 as of 2005-04-08 12:13:32
Size: 4538
Editor: dyn-176115
Comment:
Revision 7 as of 2008-10-03 20:18:43
Size: 423
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 22: Line 22:

}}}

==== BallFrame ====
----
{{{#!java

package nl.tue.id.dps.swing;

import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.util.Vector;
import java.util.Iterator;

/**
 * <p>Title: ITML</p>
 *
 * <p>Description: Distributed Media Extension to X-Smiles</p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: ID, TU/e</p>
 *
 * @author HU, Jun
 * @version 1.0
 */
public class BallFrame
    extends JFrame {
  TimeCounter counter = new TimeCounter();
  BorderLayout borderLayout1 = new BorderLayout();
  JPanel ballPanel = new JPanel();
  Vector listeners = new Vector();

  public void addBallListener(BallListener listener){
    listeners.add(listener);
  }

  public void removeBallListener(BallListener listener){
    listeners.remove(listener);
  }

  public static void main(String arg[]){
    BallFrame ball = new BallFrame();
    ball.pack();
    ball.show();
    ball.cold();
  }

  public BallFrame() {
    try {
      jbInit();
    }
    catch (Exception exception) {
      exception.printStackTrace();
    }
    counter.start();
  }

  private void jbInit() throws Exception {
    getContentPane().setLayout(borderLayout1);
    ballPanel.addMouseListener(new BallFrame_ballPanel_mouseAdapter(this));
    this.getContentPane().add(ballPanel, java.awt.BorderLayout.CENTER);
    ballPanel.setMinimumSize(new Dimension(640, 480));
    ballPanel.setPreferredSize(new Dimension(640, 480));
  }

  public void setBallColor(Color c){
    ballPanel.setBackground(c);
  }

  public void ballPanel_mousePressed(MouseEvent e) {
    counter.startCounting();

  }

  public void ballPanel_mouseReleased(MouseEvent e) {
    counter.stopCounting();
  }

  public void cold(){
    setBallColor(Color.blue);
    Iterator i = listeners.iterator();
    while(i.hasNext()){
      BallListener ballListener = (BallListener)i.next();
      ballListener.becomeCold();
    }
  }

  public void warm(){
    setBallColor(Color.yellow);
    Iterator i = listeners.iterator();
    while(i.hasNext()){
      BallListener ballListener = (BallListener)i.next();
      ballListener.becomeWarm();
    }
  }

  public void warmer(){
    setBallColor(Color.orange);
    Iterator i = listeners.iterator();
    while(i.hasNext()){
      BallListener ballListener = (BallListener)i.next();
      ballListener.becomeWarmer();
    }
  }

  public void warmest(){
    setBallColor(Color.red);
    Iterator i = listeners.iterator();
    while(i.hasNext()){
      BallListener ballListener = (BallListener)i.next();
      ballListener.becomeWarmest();
    }
  }

  class TimeCounter extends Thread {
  boolean counting = false;
  int n = 0;
  public void run(){
    while(true){
      if(counting){
        try {
          sleep(2000);
        }
        catch (InterruptedException ex1) {
        }
        n = n+1;
        if (n == 1){
          warm();
        }
        else if (n== 2){
          warmer();
        }
        else if (n==3){
          warmest();
        }
        else{
          n = 4;
        }

      }
      else{
        try {
          sleep(2000);
        }
        catch (InterruptedException ex1) {
        }
        n = n-1;
        if (n == 3){
          warmest();
        }
        else if (n== 2){
          warmer();
        }
        else if (n==1){
          warm();
        }
        else{
          n = 0;
          cold();
        }

      }
    }

  }
  public void startCounting(){
    counting = true;
  }
  public void stopCounting(){
    counting = false;
  }
}


}


class BallFrame_ballPanel_mouseAdapter
    extends MouseAdapter {
  private BallFrame adaptee;
  BallFrame_ballPanel_mouseAdapter(BallFrame adaptee) {
    this.adaptee = adaptee;
  }

  public void mousePressed(MouseEvent e) {
    adaptee.ballPanel_mousePressed(e);
  }

  public void mouseReleased(MouseEvent e){
    adaptee.ballPanel_mouseReleased(e);
  }
}
   1 package nl.tue.id.dps.swing;
   2 
   3 /**
   4  * <p>Title: ITML</p>
   5  *
   6  * <p>Description: Distributed Media Extension to X-Smiles</p>
   7  *
   8  * <p>Copyright: Copyright (c) 2004</p>
   9  *
  10  * <p>Company: ID, TU/e</p>
  11  *
  12  * @author HU, Jun
  13  * @version 1.0
  14  */
  15 public interface BallListener {
  16   public void becomeWarm();
  17   public void becomeCold();
  18   public void becomeWarmer();
  19   public void becomeWarmest();
  20 }

JunHu: Dc222Home/WorkShops/BallListener (last edited 2008-10-03 20:18:43 by localhost)