Swing Type Tester 10 : Swing Thread « Threads « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Threads » Swing ThreadScreenshots 
Swing Type Tester 10
Swing Type Tester 10
 
/*
Java Threads, 3rd Edition
By Scott Oaks, Henry Wong
3rd Edition September 2004 
ISBN: 0-596-00782-5

*/

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import java.util.Vector;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class SwingTypeTester10 extends JFrame implements CharacterSource {

  protected RandomCharacterGenerator producer;

  private AnimatedCharacterDisplayCanvas displayCanvas;

  private CharacterDisplayCanvas feedbackCanvas;

  private JButton quitButton;

  private JButton startButton;

  private JButton stopButton;

  private CharacterEventHandler handler;

  private ScoreLabel score;

  public SwingTypeTester10() {
    initComponents();
  }

  private void initComponents() {
    handler = new CharacterEventHandler();
    producer = new RandomCharacterGenerator();
    producer.setDone(true);
    producer.start();
    displayCanvas = new AnimatedCharacterDisplayCanvas(producer);
    feedbackCanvas = new CharacterDisplayCanvas(this);
    quitButton = new JButton();
    startButton = new JButton();
    stopButton = new JButton();
    score = new ScoreLabel(producer, this);

    Container pane = getContentPane();
    JPanel p1 = new JPanel();
    p1.setLayout(new BoxLayout(p1, BoxLayout.PAGE_AXIS));
    p1.add(displayCanvas);
    p1.add(feedbackCanvas);

    JPanel p2 = new JPanel();
    score.setText("      ");
    score.setFont(new Font("MONOSPACED", Font.BOLD, 30));
    p2.add(score);
    startButton.setText("Start");
    p2.add(startButton);
    stopButton.setText("Stop");
    stopButton.setEnabled(false);
    p2.add(stopButton);
    quitButton.setText("Quit");
    p2.add(quitButton);
    p1.add(p2);
    pane.add(p1, BorderLayout.NORTH);
    pack();

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent evt) {
        quit();
      }
    });
    feedbackCanvas.addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent ke) {
        char c = ke.getKeyChar();
        if (c != KeyEvent.CHAR_UNDEFINED)
          newCharacter((intc);
      }
    });
    startButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        displayCanvas.setDone(false);
        producer.setDone(false);
        score.resetScore();
        startButton.setEnabled(false);
        stopButton.setEnabled(true);
        feedbackCanvas.setEnabled(true);
        feedbackCanvas.requestFocus();
      }
    });
    stopButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        startButton.setEnabled(true);
        stopButton.setEnabled(false);
        producer.setDone(true);
        displayCanvas.setDone(true);
        feedbackCanvas.setEnabled(false);
      }
    });
    quitButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        quit();
      }
    });
  }

  private void quit() {
    System.exit(0);
  }

  public void addCharacterListener(CharacterListener cl) {
    handler.addCharacterListener(cl);
  }

  public void removeCharacterListener(CharacterListener cl) {
    handler.removeCharacterListener(cl);
  }

  public void newCharacter(int c) {
    handler.fireNewCharacter(this, c);
  }

  public void nextCharacter() {
    throw new IllegalStateException("We don't produce on demand");
  }

  public static void main(String args[]) {
    new SwingTypeTester10().show();
  }
}

class ScoreLabel extends JLabel implements CharacterListener {

  private volatile int score = 0;

  private int char2type = -1;

  private CharacterSource generator = null, typist = null;

  private Lock scoreLock = new ReentrantLock();

  public ScoreLabel(CharacterSource generator, CharacterSource typist) {
    this.generator = generator;
    this.typist = typist;
    if (generator != null)
      generator.addCharacterListener(this);
    if (typist != null)
      typist.addCharacterListener(this);
  }

  public ScoreLabel() {
    this(null, null);
  }

  public void resetGenerator(CharacterSource newGenerator) {
    try {
      scoreLock.lock();
      if (generator != null)
        generator.removeCharacterListener(this);
      generator = newGenerator;
      if (generator != null)
        generator.addCharacterListener(this);
    finally {
      scoreLock.unlock();
    }
  }

  public void resetTypist(CharacterSource newTypist) {
    try {
      scoreLock.lock();
      if (typist != null)
        typist.removeCharacterListener(this);
      typist = newTypist;
      if (typist != null)
        typist.addCharacterListener(this);
    finally {
      scoreLock.unlock();
    }
  }

  public void resetScore() {
    try {
      scoreLock.lock();
      score = 0;
      char2type = -1;
      setScore();
    finally {
      scoreLock.unlock();
    }
  }

  private void setScore() {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        setText(Integer.toString(score));
      }
    });
  }

  public void newCharacter(CharacterEvent ce) {
    scoreLock.lock();
    try {
      if (ce.source == generator) {
        if (char2type != -1) {
          score--;
          setScore();
        }
        char2type = ce.character;
      else {
        if (char2type != ce.character) {
          score--;
        else {
          score++;
          char2type = -1;
        }
      }
      setScore();
    finally {
      scoreLock.unlock();
    }
  }
}

class RandomCharacterGenerator extends Thread implements CharacterSource {
  private static char[] chars;

  private static String charArray = "abcdefghijklmnopqrstuvwxyz0123456789";
  static {
    chars = charArray.toCharArray();
  }

  private Random random;

  private CharacterEventHandler handler;

  private boolean done = true;

  private Lock lock = new ReentrantLock();

  private Condition cv = lock.newCondition();

  public RandomCharacterGenerator() {
    random = new Random();
    handler = new CharacterEventHandler();
  }

  public int getPauseTime(int minTime, int maxTime) {
    return (int) (minTime + ((maxTime - minTime* random.nextDouble()));
  }

  public int getPauseTime() {
    return getPauseTime(20005500);
  }

  public void addCharacterListener(CharacterListener cl) {
    handler.addCharacterListener(cl);
  }

  public void removeCharacterListener(CharacterListener cl) {
    handler.removeCharacterListener(cl);
  }

  public void nextCharacter() {
    handler.fireNewCharacter(this,
        (intchars[random.nextInt(chars.length)]);
  }

  public void run() {
    try {
      lock.lock();
      while (true) {
        try {
          if (done) {
            cv.await();
          else {
            nextCharacter();
            cv.await(getPauseTime(), TimeUnit.MILLISECONDS);
          }
        catch (InterruptedException ie) {
          return;
        }
      }
    finally {
      lock.unlock();
    }
  }

  public void setDone(boolean b) {
    try {
      lock.lock();
      done = b;

      if (!done)
        cv.signal();
    finally {
      lock.unlock();
    }
  }
}

interface CharacterListener {
  public void newCharacter(CharacterEvent ce);
}

interface CharacterSource {
  public void addCharacterListener(CharacterListener cl);

  public void removeCharacterListener(CharacterListener cl);

  public void nextCharacter();
}

class AnimatedCharacterDisplayCanvas extends CharacterDisplayCanvas implements
    CharacterListener, Runnable {

  private volatile boolean done = false;

  private int curX;

  private Lock lock = new ReentrantLock();

  private Condition cv = lock.newCondition();

  private Thread timer = null;

  public AnimatedCharacterDisplayCanvas(CharacterSource cs) {
    super(cs);
  }

  public synchronized void newCharacter(CharacterEvent ce) {
    curX = 0;
    tmpChar[0(charce.character;
    repaint();
  }

  public synchronized void paintComponent(Graphics gc) {
    if (tmpChar[0== 0)
      return;
    Dimension d = getSize();
    int charWidth = fm.charWidth(tmpChar[0]);
    gc.clearRect(00, d.width, d.height);
    gc.drawChars(tmpChar, 01, curX++, fontHeight);
    if (curX > d.width - charWidth)
      curX = 0;
  }

  public void run() {
    try {
      lock.lock();
      while (true) {
        try {
          if (done) {
            cv.await();
          else {
            repaint();
            cv.await(100, TimeUnit.MILLISECONDS);
          }
        catch (InterruptedException ie) {
          return;
        }
      }
    finally {
      lock.unlock();
    }
  }

  public void setDone(boolean b) {
    try {
      lock.lock();
      done = b;
      if (timer == null) {
        timer = new Thread(this);
        timer.start();
      }
      if (!done)
        cv.signal();
    finally {
      lock.unlock();
    }
  }
}

class CharacterDisplayCanvas extends JComponent implements CharacterListener {
  protected FontMetrics fm;

  protected char[] tmpChar = new char[1];

  protected int fontHeight;

  public CharacterDisplayCanvas(CharacterSource cs) {
    setFont(new Font("Monospaced", Font.BOLD, 18));
    fm = Toolkit.getDefaultToolkit().getFontMetrics(getFont());
    fontHeight = fm.getHeight();
    cs.addCharacterListener(this);
  }

  public void setCharacterListener(CharacterSource cs) {
    cs.addCharacterListener(this);
  }

  public Dimension preferredSize() {
    return new Dimension(fm.getMaxAscent() 10, fm.getMaxAdvance() 10);
  }

  public synchronized void newCharacter(CharacterEvent ce) {
    tmpChar[0(charce.character;
    repaint();
  }

  protected synchronized void paintComponent(Graphics gc) {
    Dimension d = getSize();
    gc.clearRect(00, d.width, d.height);
    if (tmpChar[0== 0)
      return;
    int charWidth = fm.charWidth((inttmpChar[0]);
    gc.drawChars(tmpChar, 01(d.width - charWidth2, fontHeight);
  }
}

class CharacterEvent {
  public CharacterSource source;

  public int character;

  public CharacterEvent(CharacterSource cs, int c) {
    source = cs;
    character = c;
  }
}

class CharacterEventHandler {
  private Vector listeners = new Vector();

  public void addCharacterListener(CharacterListener cl) {
    listeners.add(cl);
  }

  public void removeCharacterListener(CharacterListener cl) {
    listeners.remove(cl);
  }

  public void fireNewCharacter(CharacterSource source, int c) {
    CharacterEvent ce = new CharacterEvent(source, c);
    CharacterListener[] cl = (CharacterListener[]) listeners
        .toArray(new CharacterListener[0]);
    for (int i = 0; i < cl.length; i++)
      cl[i].newCharacter(ce);
  }
}


           
         
  
Related examples in the same category
1. Is Event Dispatcher ThreadIs Event Dispatcher Thread
2. GUI clock
3. Race Conditions using Swing ComponentsRace Conditions using Swing Components
4. Eliminating race Conditions using Swing ComponentsEliminating race Conditions using Swing Components
5. Using the Runnable interfaceUsing the Runnable interface
6. Write your Beans this way so they can run in a multithreaded environmentWrite your Beans this way so they can run in a multithreaded environment
7. User interface responsiveness
8. InvokeExample: Swing and threadInvokeExample: Swing and thread
9. Counter: Swing and threadCounter: Swing and thread
10. Thread accuracy: Swing and threadsThread accuracy: Swing and threads
11. Swing and thread: invoke and waitSwing and thread: invoke and wait
12. Swing and threads: invoke laterSwing and threads: invoke later
13. Swing and threads: slideSwing and threads: slide
14. Swing and threads: scroll textSwing and threads: scroll text
15. Animation: Swing and threadAnimation: Swing and thread
16. Swing and Thread for length operationSwing and Thread for length operation
17. Swing and Thread: cancel a lengthy operationSwing and Thread: cancel a lengthy operation
18. Swing and Thread: repaintSwing and Thread: repaint
19. Swing Thread Test
20. Thread and Swing 1Thread and Swing 1
21. Thread and Swing 2Thread and Swing 2
22. Thread and Swing 3Thread and Swing 3
23. Swing and Threading
24. SwingUtilities.invokeLater and swing thread
25. An abstract class to perform lengthy GUI-interacting tasks in a dedicated thread.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.