0% found this document useful (0 votes)
55 views6 pages

Class 35: Active Learning: Using Threads To Build An Animation

This document discusses using threads to create an animated scrolling text ticker application in Java. It provides code for the initial Ticker0 application that scrolls the text on mouse clicks. It then outlines steps to modify this into a Ticker1 application that continuously scrolls the text using a thread, and further into a Ticker2 application that stops and starts the scrolling animation when the user presses and releases the mouse. Examples from the SimpleClock application are provided as a reference for implementing the threading.

Uploaded by

yekych
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views6 pages

Class 35: Active Learning: Using Threads To Build An Animation

This document discusses using threads to create an animated scrolling text ticker application in Java. It provides code for the initial Ticker0 application that scrolls the text on mouse clicks. It then outlines steps to modify this into a Ticker1 application that continuously scrolls the text using a thread, and further into a Ticker2 application that stops and starts the scrolling animation when the user presses and releases the mouse. Examples from the SimpleClock application are provided as a reference for implementing the threading.

Uploaded by

yekych
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Introduction to Computation and Problem

Solving

Class 35:
Active Learning: Using Threads
to Build an Animation

Prof. Steven R. Lerman


and
Dr. V. Judson Harward

Ticker Application

Our goal is to use a separate thread to drive an


animation that scrolls a string across the screen.
In the full version, you can stop the animation by
pressing the mouse down on the animation
screen, and resume the animation by releasing
the mouse.
As a starting point, we give you Ticker0.java.
Download it from the class web site and let's look
at it together. It is not animated. Mouse clicks
drive the string across the screen.

Ticker0, Data Members

public class Ticker0 extends JPanel


{
private String animationString;
private Font animationFont;
private int animationWidth;
private int xoffset;
private int tickerWidth;

public static final Dimension DEFAULT_SIZE =


new Dimension( 400, 200 );

Ticker0, Geometry

xoffset=15

Click to advance me.


animationWidth

xoffset=-20

Click to advance me.

tickerWidth

Ticker0, Constructor

public Ticker0(String s) {
animationString = s;
setOpaque( true );
setBackground( Color.black );
setForeground( Color.white );
animationFont= new Font("SansSerif",Font.BOLD,16);
animationWidth = getFontMetrics(animationFont).
stringWidth( animationString );
setFont( animationFont );
xoffset = 15;
tickerWidth = DEFAULT_SIZE.width;

Ticker0, MouseListener

addMouseListener( new MouseAdapter() {


public void mouseClicked( MouseEvent e )
{
// move left 5 pixels each mouse click

xoffset -= 5;

// if string off screen now, bring it back on right

if (xoffset <= -animationWidth)

xoffset = tickerWidth;
repaint();
}
} );
} // end constructor

Ticker0, paintComponent()

public void paintComponent(Graphics g)


{
super.paintComponent( g );
int yoffset = getHeight() / 2;
tickerWidth = getWidth(); // allows resize
g.drawString( animationString, xoffset, yoffset );
}

Ticker, Step 1

Copy Ticker0 and rename it Ticker1.


Substitute your favorite movie quote as the
Ticker1 constructor argument in the main()
method.
Comment out the MouseListener code for the
moment.
Now modify Ticker1 so that it scrolls
continuously, a pixel every 1/100th of a second.
You may find the model of the SimpleClock
program useful.

SimpleClock, Constructor

public class SimpleClock


extends javax.swing.JPanel implements Runnable {
private Font clockFont =
new Font("SansSerif",Font.BOLD,24);
private Thread clockThread = null;
private int xPos = 40;
private int yPos = 50;

public SimpleClock() {
. . .
clockThread = new Thread( this, "Clock" );
clockThread.start();
}

SimpleClock, run()

public void run()


{
while ( true )
{
repaint();

try

Thread.sleep( 1000 );
}
catch (InterruptedException e )
{}
}
}

10

Ticker, Step 2

Now, as a second step, copy Ticker1 and


rename it Ticker2. Modify Ticker2 so that the
animation stops when you press the mouse and
resumes when you release it.
What events do you have to listen for?
How will you get the animation to stop and start?

11

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy