Swing and threads: slide : Swing Thread « Threads « Java






Swing and threads: slide

Swing and threads: slide
  

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SlideShow extends JComponent {
  private BufferedImage[] slide;

  private Dimension slideSize;

  private volatile int currSlide;

  private Thread internalThread;

  private volatile boolean noStopRequested;

  public SlideShow() {
    currSlide = 0;
    slideSize = new Dimension(50, 50);
    buildSlides();

    setMinimumSize(slideSize);
    setPreferredSize(slideSize);
    setMaximumSize(slideSize);
    setSize(slideSize);

    noStopRequested = true;
    Runnable r = new Runnable() {
      public void run() {
        try {
          runWork();
        } catch (Exception x) {
          x.printStackTrace();
        }
      }
    };

    internalThread = new Thread(r, "SlideShow");
    internalThread.start();
  }

  private void buildSlides() {
    RenderingHints renderHints = new RenderingHints(
        RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);

    renderHints.put(RenderingHints.KEY_RENDERING,
        RenderingHints.VALUE_RENDER_QUALITY);

    slide = new BufferedImage[20];

    Color rectColor = Color.BLUE;
    Color circleColor = Color.YELLOW;

    for (int i = 0; i < slide.length; i++) {
      slide[i] = new BufferedImage(slideSize.width, slideSize.height,
          BufferedImage.TYPE_INT_RGB);

      Graphics2D g2 = slide[i].createGraphics();
      g2.setRenderingHints(renderHints);

      g2.setColor(rectColor);
      g2.fillRect(0, 0, slideSize.width, slideSize.height);

      g2.setColor(circleColor);

      int diameter = 0;
      if (i < (slide.length / 2)) {
        diameter = 5 + (8 * i);
      } else {
        diameter = 5 + (8 * (slide.length - i));
      }

      int inset = (slideSize.width - diameter) / 2;
      g2.fillOval(inset, inset, diameter, diameter);

      g2.setColor(Color.black);
      g2.drawRect(0, 0, slideSize.width - 1, slideSize.height - 1);

      g2.dispose();
    }
  }

  public void paint(Graphics g) {
    g.drawImage(slide[currSlide], 0, 0, this);
  }

  private void runWork() {
    while (noStopRequested) {
      try {
        Thread.sleep(100); // 10 frames per second
        currSlide = (currSlide + 1) % slide.length;
        repaint();
      } catch (InterruptedException x) {
        Thread.currentThread().interrupt();
      }
    }
  }

  public void stopRequest() {
    noStopRequested = false;
    internalThread.interrupt();
  }

  public boolean isAlive() {
    return internalThread.isAlive();
  }

  public static void main(String[] args) {
    SlideShow ss = new SlideShow();

    JPanel p = new JPanel(new FlowLayout());
    p.add(ss);

    JFrame f = new JFrame("SlideShow");
    f.setContentPane(p);
    f.setSize(250, 150);
    f.setVisible(true);
  }
}

           
         
    
  








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: scroll textSwing and threads: scroll text
14.Animation: Swing and threadAnimation: Swing and thread
15.Swing and Thread for length operationSwing and Thread for length operation
16.Swing and Thread: cancel a lengthy operationSwing and Thread: cancel a lengthy operation
17.Swing and Thread: repaintSwing and Thread: repaint
18.Thread and Swing 1Thread and Swing 1
19.Thread and Swing 2Thread and Swing 2
20.Thread and Swing 3Thread and Swing 3
21.Swing Type Tester 10Swing Type Tester 10
22.Swing and Threading
23.SwingUtilities.invokeLater and swing thread
24.An abstract class to perform lengthy GUI-interacting tasks in a dedicated thread.
25.This program demonstrates that a thread that runs in parallel with the event dispatch thread can cause errors in Swing components