Threads - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Threads

Description:

... a car moving across the screen. in paint() draw the car at ... in run() update the values of x,y and call repaint() in init() set the initial values of x and y ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 10
Provided by: robertb61
Category:
Tags: threads

less

Transcript and Presenter's Notes

Title: Threads


1
Threads
  • java supports multiple threads of execution
  • it appears as if the threads are executing
    concurrently
  • a class must extend Thread or implement Runnable
  • Runnable declares the method run()

2
Threads
  • a thread is controlled by the run() method
  • a thread starts when its start() method is called
  • a thread stops when its stop() method is called
    or when run() returns
  • separate threads are useful in animation

3
animation
  • the basic idea is to draw an image, wait a while
    and then draw the image again, modifying it
    slightly
  • the main thread of an applet can draw the image
    in its paint() method
  • the other thread can control when the image is
    changed and what it is changed to.

4
simple animation
  • write an applet that draws a car moving across
    the screen
  • in paint() draw the car at position x,y
  • in run() update the values of x,y and call
    repaint()
  • in init() set the initial values of x and y
  • in start() start the thread
  • in stop() stop the thread

5
reduce flickering
  • override update()
  • clip
  • double buffering

6
update
  • repaint() calls update()
  • update() clears the screen and calls paint()
  • if you are repainting the whole screen, override
    update such that it does not clear the screen
    before calling paint

7
clip
  • clipping restricts the area to be changed
  • clipRect(x,y,width,height) makes the clip area
    the intersection of the old clip area and this
    one
  • setClip(x,y,width,height) set the new clip area

8
double buffering
  • draw the image to an off-screen buffer
  • when done move it to the screen area

9
Image myImage Graphics myGraphics public void
init()myImage createImage(getSize)().width,
getSize().height) myGraphicsmyImage.getGraphi
cs() public void update(Graphics
g) paint(myGraphics) g.drawImage(myImage,0,0,t
his)
Write a Comment
User Comments (0)
About PowerShow.com