Swing and threads: scroll text : 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 and threads: scroll text
Swing and threads: scroll text
 

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

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

public class ScrollText extends JComponent {
  private BufferedImage image;

  private Dimension imageSize;

  private volatile int currOffset;

  private Thread internalThread;

  private volatile boolean noStopRequested;

  public ScrollText(String text) {
    currOffset = 0;
    buildImage(text);

    setMinimumSize(imageSize);
    setPreferredSize(imageSize);
    setMaximumSize(imageSize);
    setSize(imageSize);

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

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

  private void buildImage(String text) {
    RenderingHints renderHints = new RenderingHints(
        RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);

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

    BufferedImage scratchImage = new BufferedImage(11,
        BufferedImage.TYPE_INT_RGB);

    Graphics2D scratchG2 = scratchImage.createGraphics();
    scratchG2.setRenderingHints(renderHints);

    Font font = new Font("Serif", Font.BOLD | Font.ITALIC, 24);

    FontRenderContext frc = scratchG2.getFontRenderContext();
    TextLayout tl = new TextLayout(text, font, frc);
    Rectangle2D textBounds = tl.getBounds();
    int textWidth = (intMath.ceil(textBounds.getWidth());
    int textHeight = (intMath.ceil(textBounds.getHeight());

    int horizontalPad = 10;
    int verticalPad = 6;

    imageSize = new Dimension(textWidth + horizontalPad, textHeight
        + verticalPad);

    image = new BufferedImage(imageSize.width, imageSize.height,
        BufferedImage.TYPE_INT_RGB);

    Graphics2D g2 = image.createGraphics();
    g2.setRenderingHints(renderHints);

    int baselineOffset = (verticalPad / 2((inttextBounds.getY());

    g2.setColor(Color.white);
    g2.fillRect(00, imageSize.width, imageSize.height);

    g2.setColor(Color.blue);
    tl.draw(g2, 0, baselineOffset);

    // Free-up resources right away, but keep "image" for
    // animation.
    scratchG2.dispose();
    scratchImage.flush();
    g2.dispose();
  }

  public void paint(Graphics g) {
    // Make sure to clip the edges, regardless of curr size
    g.setClip(00, imageSize.width, imageSize.height);

    int localOffset = currOffset; // in case it changes
    g.drawImage(image, -localOffset, 0this);
    g.drawImage(image, imageSize.width - localOffset, 0this);

    // draw outline
    g.setColor(Color.black);
    g.drawRect(00, imageSize.width - 1, imageSize.height - 1);
  }

  private void runWork() {
    while (noStopRequested) {
      try {
        Thread.sleep(100)// 10 frames per second

        // adjust the scroll position
        currOffset = (currOffset + 1% imageSize.width;

        // signal the event thread to call paint()
        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) {
    ScrollText st = new ScrollText("Java can do animation!");

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

    JFrame f = new JFrame("ScrollText Demo");
    f.setContentPane(p);
    f.setSize(400100);
    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: slideSwing and threads: slide
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. Swing Thread Test
19. Thread and Swing 1Thread and Swing 1
20. Thread and Swing 2Thread and Swing 2
21. Thread and Swing 3Thread and Swing 3
22. Swing Type Tester 10Swing Type Tester 10
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.