{"id":26860,"date":"2021-05-03T23:10:05","date_gmt":"2021-05-04T03:10:05","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/multithreaded-graphics-and-double-buffering-programming-code-examples-java-j2ee-j2me-java-threads\/"},"modified":"2021-05-03T23:10:05","modified_gmt":"2021-05-04T03:10:05","slug":"multithreaded-graphics-and-double-buffering-programming-code-examples-java-j2ee-j2me-java-threads","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=26860","title":{"rendered":"Multithreaded Graphics and Double Buffering #Programming Code Examples #Java\/J2EE\/J2ME #Java Threads"},"content":{"rendered":"<pre>\nShipSimulation.java  Illustrates the basic approach of multithreaded graphics whereas a thread adjusts parameters affecting the appearance of the graphics and then calls repaint to schedule an update of the display. \n\nimport java.applet.Applet;\nimport java.awt.*;\n\npublic class ShipSimulation extends Applet implements Runnable {\n  ...\n  \n  public void run() {\n    Ship s;\n    for(int i=0; i&lt;ships .length; i++) {\n      s = ships[i];\n      s.move(); \/\/ Update location.\n    }\n    repaint();\n  }\n\n  ...\n  \n  public void paint(Graphics g) {\n    Ship s;\n    for(int i=0; i&lt;ships.length; i++) {\n      s = ships[i];\n      g.draw(s); \/\/ Draw at current location.\n    }\n  }\n}\n*************************\nDrawCircles.java An applet that draws a circle where the user clicks the mouse.\nimport java.applet.Applet;\nimport java.awt.*;\nimport java.awt.event.*;\nimport java.util.Vector;\n\n\/** An applet that draws a small circle where you click.\n \npublic class DrawCircles extends Applet {\n\n  private Vector circles;\n\n  \/** When you click the mouse, create a SimpleCircle,\n   *  put it in the Vector, and tell the system\n   *  to repaint (which calls update, which clears\n   *  the screen and calls paint).\n   *\/\n\n  private class CircleDrawer extends MouseAdapter {\n    public void mousePressed(MouseEvent event) {\n      circles.addElement(\n         new SimpleCircle(event.getX(),event.getY(),25));\n      repaint();\n    }\n  }\n\n  public void init() {\n    circles = new Vector();\n    addMouseListener(new CircleDrawer());\n    setBackground(Color.white);\n  }\n\n  \/** This loops down the available SimpleCircle objects,\n   *  drawing each one.\n   *\/\n\n  public void paint(Graphics g) {\n    SimpleCircle circle;\n    for(int i=0; i&lt;circles.size(); i++) {\n      circle = (SimpleCircle)circles.elementAt(i);\n      circle.draw(g);\n    }\n  }\n}\n******************************\nSimpleCircle.java A class to store the x, y, and radius of a circle. Also, provides a draw method to paint the circle on the graphics object.\nimport java.awt.*;\n\n\/** A class to store an x, y, and radius, plus a draw method.\n \/\/\npublic class SimpleCircle {\n  private int x, y, radius;\n\n  public SimpleCircle(int x, int y, int radius) {\n    setX(x);\n    setY(y);\n    setRadius(radius);\n  }\n\n  \/** Given a Graphics, draw the SimpleCircle\n   *  centered around its current position.\n   *\/\n\n  public void draw(Graphics g) {\n    g.fillOval(x - radius, y - radius,\n               radius * 2, radius * 2);\n  }\n\n  public int getX() { return(x); }\n\n  public void setX(int x) { this.x = x; }\n\n  public int getY() { return(y); }\n\n  public void setY(int y) { this.y = y; }\n\n  public int getRadius() { return(radius); }\n\n  public void setRadius(int radius) {\n    this.radius = radius;\n  }\n}\n\/\/\nRubberband.java  Draws a rectangle where one corner is fixed (initial selection of the mouse) and the opposite corner is determined by the location of the dragged mouse. Illustrates drawing directly into the graphics object instead of performing the drawing in the paint method. The graphics object is obtained by calling getGraphics.\nimport java.applet.Applet;\nimport java.awt.*;\nimport java.awt.event.*;\n\n\/** Draw &quot;rubberband&quot; rectangles when the user drags\n *  the mouse.\n *\n  *\/\n\npublic class Rubberband extends Applet {\n  private int startX, startY, lastX, lastY;\n\n  public void init() {\n    addMouseListener(new RectRecorder());\n    addMouseMotionListener(new RectDrawer());\n    setBackground(Color.white);\n  }\n\n  \/** Draw the rectangle, adjusting the x, y, w, h\n   *  to correctly accommodate for the opposite corner of the\n   *  rubberband box relative to the start position.\n   *\/\n\n  private void drawRectangle(Graphics g, int startX, int startY,\n                             int stopX, int stopY ) {\n    int x, y, w, h;\n    x = Math.min(startX, stopX);\n    y = Math.min(startY, stopY);\n    w = Math.abs(startX - stopX);\n    h = Math.abs(startY - stopY);\n    g.drawRect(x, y, w, h);\n  }\n\n  private class RectRecorder extends MouseAdapter {\n\n    \/** When the user presses the mouse, record the\n     *  location of the top-left corner of rectangle.\n     *\/\n\n    public void mousePressed(MouseEvent event) {\n      startX = event.getX();\n      startY = event.getY();\n      lastX = startX;\n      lastY = startY;\n    }\n\n    \/** Erase the last rectangle when the user releases\n     *  the mouse.\n     *\/\n\n    public void mouseReleased(MouseEvent event) {\n      Graphics g = getGraphics();\n      g.setXORMode(Color.lightGray);\n      drawRectangle(g, startX, startY, lastX, lastY);\n    }\n  } \n\n  private class RectDrawer extends MouseMotionAdapter {\n\n    \/** This draws a rubberband rectangle, from the location\n     *  where the mouse was first clicked to the location\n     *  where the mouse is dragged.\n     *\/\n\n    public void mouseDragged(MouseEvent event) {\n      int x = event.getX();\n      int y = event.getY();\n\n      Graphics g = getGraphics();\n      g.setXORMode(Color.lightGray);\n      drawRectangle(g, startX, startY, lastX, lastY);\n      drawRectangle(g, startX, startY, x, y);\n\n      lastX = x;\n      lastY = y;\n    }\n  } \n}\n*\/\/\nBounce.java An applet that contains moving circles that bounce off the walls. Illustrates overriding update to reduce animation flickering and performing incremental updating in the paint method. Here, to achieve animation, a single thread continuously calls repaint while subsequently sleeping for a 100 milliseconds\nimport java.applet.Applet;\nimport java.awt.*;\nimport java.awt.event.*;\nimport java.util.Vector;\n\n\/** Bounce circles around on the screen. Doesn&#039;t use double\n *  buffering, so has problems with overlapping circles.\n *  Overrides update to avoid flicker problems.\n *\n *\/\n\npublic class Bounce extends Applet implements Runnable,\n                                              ActionListener {\n  private Vector circles;\n  private int width, height;\n  private Button startButton, stopButton;\n  private Thread animationThread = null;\n\n  public void init() {\n    setBackground(Color.white);\n    width = getSize().width;\n    height = getSize().height;\n    circles = new Vector();\n    startButton = new Button(&quot;Start a circle&quot;);\n    startButton.addActionListener(this);\n    add(startButton);\n    stopButton = new Button(&quot;Stop all circles&quot;);\n    stopButton.addActionListener(this);\n    add(stopButton);\n  }\n\n  \/** When the &quot;start&quot; button is pressed, start the animation\n   *  thread if it is not already started. Either way, add a\n   *  circle to the Vector of circles that are being bounced.\n   *  <p>\n   *  When the &quot;stop&quot; button is pressed, stop the thread and\n   *  clear the Vector of circles.\n   *\/\n\n  public void actionPerformed(ActionEvent event) {\n    if (event.getSource() == startButton) {\n      if (circles.size() == 0) {\n        \/\/ Erase any circles from previous run.\n        getGraphics().clearRect(0, 0, getSize().width,\n                                      getSize().height);\n        animationThread = new Thread(this);\n        animationThread.start();\n      }\n      int radius = 25;\n      int x = radius + randomInt(width - 2 * radius);\n      int y = radius + randomInt(height - 2 * radius);\n      int deltaX = 1 + randomInt(10);\n      int deltaY = 1 + randomInt(10);\n      circles.addElement(new MovingCircle(x, y, radius, deltaX,\n                                          deltaY));\n    } else if (event.getSource() == stopButton) {\n      if (animationThread != null) {\n        animationThread = null;\n        circles.removeAllElements();\n      }\n    }\n    repaint();\n  }\n\n  \/** Each time around the loop, call paint and then take a\n   *  short pause. The paint method will move the circles and\n   *  draw them.\n   *\/\n\n  public void run() {\n    Thread myThread = Thread.currentThread();\n    \/\/ Really while animationThread not null\n    while(animationThread==myThread) {\n      repaint();\n      pause(100);\n    }\n  }\n\n  \/** Skip the usual screen-clearing step of update so that\n   *  there is no flicker between each drawing step.\n   *\/\n\n  public void update(Graphics g) {\n    paint(g);\n  }\n\n  \/** Erase each circle's old position, move it, then draw it\n   *  in new location.\n   *\/\n\n  public void paint(Graphics g) {\n    MovingCircle circle;\n    for(int i=0; i&lt;circles .size(); i++) {\n      circle = (MovingCircle)circles.elementAt(i);\n      g.setColor(getBackground());\n      circle.draw(g);  \/\/ Old position.\n      circle.move(width, height);\n      g.setColor(getForeground());\n      circle.draw(g);  \/\/ New position.\n    }\n  }\n\n  \/\/ Returns an int from 0 to max (inclusive),\n  \/\/ yielding max + 1 possible values.\n\n  private int randomInt(int max) {\n    double x =\n      Math.floor((double)(max + 1) * Math.random());\n    return((int)(Math.round(x)));\n  }\n\n\n  \/\/ Sleep for the specified amount of time.\n\n  private void pause(int milliseconds) {\n    try {\n      Thread.sleep((long)milliseconds);\n    } catch(InterruptedException ie) {}\n  }\n}\n*\/\/\nMovingCircle.java An extension of SimpleCircle that can be moved about and bounces off walls. \n\/** An extension of SimpleCircle that can be moved around\n *  according to deltaX and deltaY values. Movement will\n *  continue in a given direction until the edge of the circle\n *  reaches a wall, when it will &quot;bounce&quot; and move in the other\n *  direction.\n *\n  *\/\n\npublic class MovingCircle extends SimpleCircle {\n  private int deltaX, deltaY;\n\n  public MovingCircle(int x, int y, int radius, int deltaX,\n                      int deltaY) {\n    super(x, y, radius);\n    this.deltaX = deltaX;\n    this.deltaY = deltaY;\n  }\n\n  public void move(int windowWidth, int windowHeight) {\n    setX(getX() + getDeltaX());\n    setY(getY() + getDeltaY());\n    bounce(windowWidth, windowHeight);\n  }\n\n  private void bounce(int windowWidth, int windowHeight) {\n    int x = getX(), y = getY(), radius = getRadius(),\n        deltaX = getDeltaX(), deltaY = getDeltaY();\n    if ((x - radius &lt; 0) &amp;&amp; (deltaX  windowWidth) &amp;&amp; (deltaX &gt; 0)) {\n      setDeltaX(-deltaX);\n    }\n    if ((y -radius &lt; 0) &amp;&amp; (deltaY  windowHeight) &amp;&amp; (deltaY &gt; 0)) {\n      setDeltaY(-deltaY);\n    }\n  }\n\n  public int getDeltaX() {\n    return(deltaX);\n  }\n\n  public void setDeltaX(int deltaX) {\n    this.deltaX = deltaX;\n  }\n\n  public int getDeltaY() {\n    return(deltaY);\n  }\n\n  public void setDeltaY(int deltaY) {\n    this.deltaY = deltaY;\n  }\n}\n*\/\/\nSimpleCircle.java A class to store the x, y, and radius of a circle. Also, provides a draw method to paint the circle on the graphics object.\nimport java.awt.*;\n\n\/** A class to store an x, y, and radius, plus a draw method.\n *\n  *\/\n\npublic class SimpleCircle {\n  private int x, y, radius;\n\n  public SimpleCircle(int x, int y, int radius) {\n    setX(x);\n    setY(y);\n    setRadius(radius);\n  }\n\n  \/** Given a Graphics, draw the SimpleCircle\n   *  centered around its current position.\n   *\/\n\n  public void draw(Graphics g) {\n    g.fillOval(x - radius, y - radius,\n               radius * 2, radius * 2);\n  }\n\n  public int getX() { return(x); }\n\n  public void setX(int x) { this.x = x; }\n\n  public int getY() { return(y); }\n\n  public void setY(int y) { this.y = y; }\n\n  public int getRadius() { return(radius); }\n\n  public void setRadius(int radius) {\n    this.radius = radius;\n  }\n}\n*\/\/\nDoubleBufferBounce.java  An enhancement to the previous applet containing bouncing circles. In this case, double buffering is used to improve the animation; all incremental updating is done in an off-screen image and then the image is drawn to the screen.\nimport java.applet.Applet;\nimport java.awt.*;\nimport java.awt.event.*;\nimport java.util.Vector;\n\n\/** Bounce circles around on the screen, using double buffering\n *  for speed and to avoid problems with overlapping circles.\n *  Overrides update to avoid flicker problems.\n *\n  *\/\n\npublic class DoubleBufferBounce extends Applet implements\n                                      Runnable, ActionListener {\n  private Vector circles;\n  private int width, height;\n  private Image offScreenImage;\n  private Graphics offScreenGraphics;\n  private Button startButton, stopButton;\n  private Thread animationThread = null;\n\n  public void init() {\n    setBackground(Color.white);\n    width = getSize().width;\n    height = getSize().height;\n    offScreenImage = createImage(width, height);\n    offScreenGraphics = offScreenImage.getGraphics();\n    \/\/ Automatic in some systems, not in others.\n    offScreenGraphics.setColor(Color.black);\n    circles = new Vector();\n    startButton = new Button(&quot;Start a circle&quot;);\n    startButton.addActionListener(this);\n    add(startButton);\n    stopButton = new Button(&quot;Stop all circles&quot;);\n    stopButton.addActionListener(this);\n    add(stopButton);\n  }\n\n  \/** When the &quot;start&quot; button is pressed, start the animation\n   *  thread if it is not already started. Either way, add a\n   *  circle to the Vector of circles that are being bounced.\n   *  <\/p><p>\n   *  When the &quot;stop&quot; button is pressed, stop the thread and\n   *  clear the Vector of circles.\n   *\/\n\n  public void actionPerformed(ActionEvent event) {\n    if (event.getSource() == startButton) {\n      if (circles.size() == 0) {\n        animationThread = new Thread(this);\n        animationThread.start();\n      }\n      int radius = 25;\n      int x = radius + randomInt(width - 2 * radius);\n      int y = radius + randomInt(height - 2 * radius);\n      int deltaX = 1 + randomInt(10);\n      int deltaY = 1 + randomInt(10);\n      circles.addElement(new MovingCircle(x, y, radius, deltaX,\n                                          deltaY));\n      repaint();\n    } else if (event.getSource() == stopButton) {\n      if (animationThread != null) {\n        animationThread = null;\n        circles.removeAllElements();\n      }\n    }\n  }\n\n  \/** Each time around the loop, move each circle based on its\n   *  current position and deltaX\/deltaY values. These values\n   *  reverse when the circles reach the edge of the window.\n   *\/\n\n  public void run() {\n    MovingCircle circle;\n    Thread myThread = Thread.currentThread();\n    \/\/ Really while animationThread not null.\n    while(animationThread==myThread) {\n      for(int j=0; j&lt;circles .size(); j++) {\n        circle = (MovingCircle)circles.elementAt(j);\n        circle.move(width, height);\n      }\n      repaint();\n      pause(100);\n    }\n  }\n\n  \/** Skip the usual screen-clearing step of update so that\n   *  there is no flicker between each drawing step.\n   *\/\n\n  public void update(Graphics g) {\n    paint(g);\n  }\n\n\n  \/** Clear the off-screen pixmap, draw each circle onto it, then\n   *  draw that pixmap onto the applet window.\n   *\/\n\n  public void paint(Graphics g) {\n    offScreenGraphics.clearRect(0, 0, width, height);\n    MovingCircle circle;\n    for(int i=0; i&lt;circles.size(); i++) {\n      circle = (MovingCircle)circles.elementAt(i);\n      circle.draw(offScreenGraphics);\n    }\n    g.drawImage(offScreenImage, 0, 0, this);\n  }\n\n  \/\/ Returns an int from 0 to max (inclusive), yielding max + 1\n  \/\/ possible values.\n\n  private int randomInt(int max) {\n    double x = Math.floor((double)(max + 1) * Math.random());\n    return((int)(Math.round(x)));\n  }\n\n  \/\/ Sleep for the specified amount of time.\n\n  private void pause(int milliseconds) {\n    try {\n      Thread.sleep((long)milliseconds);\n    } catch(InterruptedException ie) {}\n  }\n}\n**\/\/\nImageAnimation.java Illustrates animation of images by using a thread to cycle through an array of images. See ImageAnimation.html. Uses the following class and images:\n\nimport java.applet.Applet;\nimport java.awt.*;\n\npublic class ImageAnimation extends Applet {\n\n\/** Sequence through an array of 15 images to perform the\n *  animation. A separate Thread controls each tumbling Duke.\n *  The Applet&#039;s stop method calls a public service of the\n *  Duke class to terminate the thread. Override update to\n *  avoid flicker problems.\n *\n  *\/\n\n  private static final int NUMDUKES  = 2;\n  private Duke[] dukes;\n  private int i;\n\n  public void init() {\n    dukes = new Duke[NUMDUKES];\n    setBackground(Color.white);\n  }\n\n\n  \/** Start each thread, specifing a direction to sequence\n   *  through the array of images.\n   *\/\n\n  public void start() {\n    int tumbleDirection;\n    for (int i=0; i&lt;NUMDUKES ; i++) {\n      tumbleDirection = (i%2==0) ? 1 :-1;\n      dukes[i] = new Duke(tumbleDirection, this);\n      dukes[i].start();\n    }\n  }\n\n\n  \/** Skip the usual screen-clearing step of update so that\n   *  there is no flicker between each drawing step.\n   *\/\n\n  public void update(Graphics g) {\n    paint(g);\n  }\n\n  public void paint(Graphics g) {\n    for (i=0 ; i&lt;NUMDUKES ; i++) {\n      if (dukes[i] != null) {\n        g.drawImage(Duke.images[dukes[i].getIndex()],\n                    200*i, 0, this);\n      }\n    }\n  }\n\n\n  \/** When the Applet&#039;s stop method is called, use the public\n   *  service, setState, of the Duke class to set a flag and\n   *  terminate the run method of the thread.\n   *\/\n\n  public void stop() {\n    for (int i=0; i&lt;NUMDUKES ; i++) {\n      if (dukes[i] != null) {\n        dukes[i].setState(Duke.STOP);\n      }\n    }\n  }\n}\n*\/\/\nDuke.java A subclass of Thread holding the images for the animation. \nimport java.applet.Applet;\nimport java.awt.*;\n\n\/** Duke is a Thread that has knowledge of the parent applet\n *  (highly coupled) and thus can call the parent&#039;s repaint\n *  method. Duke is mainly responsible for changing an index\n *  value into an image array.\n *\n  *\/\n\npublic class Duke extends Thread {\n  public static final int STOP = 0;\n  public static final int RUN  = 1;\n  public static final int WAIT = 2;\n\n  public static Image[] images;\n  private static final int NUMIMAGES = 15;\n  private static Object lock = new Object();\n  private int state = RUN;\n  private int tumbleDirection;\n  private int index = 0;\n  private Applet parent;\n\n  public Duke(int tumbleDirection, Applet parent) {\n    this.tumbleDirection = tumbleDirection;\n    this.parent = parent;\n    synchronized(lock) {\n      if (images==null) {  \/\/ If not previously loaded.\n        images = new Image[ NUMIMAGES ];\n        for (int i=0; i&lt;NUMIMAGES; i++) {\n          images[i] = parent.getImage( parent.getCodeBase(),\n                                       &quot;images\/T&quot; + i + &quot;.gif&quot;);\n        }\n      }\n    }\n  }\n\n  \/** Return current index into image array.  *\/\n\n  public int getIndex() { return index; }\n\n  \/** Public method to permit setting a flag to stop or\n   *  suspend the thread. State is monitored through\n   *  corresponding checkState method.\n   *\/\n\n  public synchronized void setState(int state) {\n    this.state = state;\n    if (state==RUN) {\n      notify();\n    }\n  }\n\n  \/** Returns the desired state (RUN, STOP, WAIT) of the\n   *  thread. If the thread is to be suspended, then the\n   *  thread method wait is continuously called until the\n   *  state is changed through the public method setState.\n   *\/\n\n  private synchronized int checkState() {\n    while (state==WAIT) {\n      try {\n        wait();\n      } catch (InterruptedException e) {}\n    }\n    return state;\n  }\n\n  \/** The variable index (into image array) is incremented\n   *  once each time through the while loop, calls repaint,\n   *  and pauses for a moment. Each time through the loop the\n   *  state (flag) of the thread is checked.\n   *\/\n\n  public void run() {\n    while (checkState()!=STOP) {\n      index += tumbleDirection;\n      if (index = NUMIMAGES) {\n        index = 0;\n      }\n\n      parent.repaint();\n\n      try {\n        Thread.sleep(100);\n      } catch (InterruptedException e) {\n        break;   \/\/ Break while loop.\n      }\n    }\n  }\n}\n*\/\/\nTimedAnimation.java An applet that demonstrates animation of an image by using a Timer. Note that Timer is located in the javax.swing package.\nimport java.awt.*;\nimport javax.swing.*;\n\n\/** An example of performing animation through Swing timers.\n *  Two timed Dukes are created with different timer periods.\n *\n  *\/\n\npublic class TimedAnimation extends JApplet {\n  private static final int NUMDUKES = 2;\n  private TimedDuke[] dukes;\n  private int i, index;\n\n  public void init() {\n    dukes = new TimedDuke[NUMDUKES];\n    setBackground(Color.white);\n    dukes[0] = new TimedDuke( 1, 100, this);\n    dukes[1] = new TimedDuke(-1, 500, this);\n\n  }\n\n  \/\/  Start each Duke timer.\n\n  public void start() {\n    for (int i=0; i&lt;numdukes ; i++) {\n      dukes[i].startTimer();\n    }\n  }\n\n  public void paint(Graphics g) {\n    for (i=0 ; i&lt;NUMDUKES ; i++) {\n      if (dukes[i] != null) {\n        index = dukes[i].getIndex();\n        g.drawImage(TimedDuke.images[index], 200*i, 0, this);\n      }\n    }\n  }\n\n  \/\/  Stop each Duke timer.\n\n  public void stop() {\n    for (int i=0; i&lt;NUMDUKES ; i++) {\n      dukes[i].stopTimer();\n    }\n  }\n}\n**\/\/\nTimedDuke.java Facilitates animation of Duke images by creating an internal timer. \nimport java.applet.Applet;\nimport java.awt.*;\nimport java.awt.event.*;\nimport javax.swing.*;\n\n\/** Duke facilitates animation by creating an internal timer.\n *  When the timer fires, an actionPerformed event is\n *  triggered, which in turn calls repaint on the parent\n *  Applet.\n *\n  *\/\n\npublic class TimedDuke implements ActionListener {\n  private static final int NUMIMAGES = 15;\n  private static boolean loaded = false;\n  private static Object lock = new Object();\n  private int tumbleDirection;\n  private int msec;\n  private int index = 0;\n  private Applet parent;\n  private Timer timer;\n  public static Image[] images = new Image[NUMIMAGES];\n\n  public TimedDuke(int tumbleDirection, int msec,\n                                        Applet parent) {\n    this.tumbleDirection = tumbleDirection;\n    this.msec = msec;\n    this.parent = parent;\n\n    synchronized (lock) {\n      if (!loaded) {\n         MediaTracker tracker = new MediaTracker(parent);\n         for (int i=0; i&lt;NUMIMAGES; i++) {\n           images[i] = parent.getImage(parent.getCodeBase(),\n                                       &quot;images\/T&quot; + i + &quot;.gif&quot;);\n           tracker.addImage(images[i],0);\n         }\n         try {\n           tracker.waitForAll();\n         } catch (InterruptedException ie) {}\n         if (!tracker.isErrorAny()) {\n           loaded = true;\n         }\n      }\n    }\n\n    timer = new Timer(msec, this);\n  }\n\n  \/\/ Return current index into image array.\n\n  public int getIndex() { return index; }\n\n  \/\/ Receives timer firing event.  Increments the index into\n  \/\/ image array and forces repainting of the new image.\n\n  public void actionPerformed(ActionEvent event) {\n    index += tumbleDirection;\n    if (index = NUMIMAGES) {\n      index = 0;\n    }\n    parent.repaint();\n  }\n\n  \/\/ Public service to start the timer.\n  public void startTimer() {\n    timer.start();\n  }\n\n  \/\/ Public service to stop the timer.\n  public void stopTimer() {\n    timer.stop();\n  }\n}\n**\/\/\n<\/p><\/pre>\n<p>Note: Brought from our old site: http:\/\/www.salearningschool.com\/example_codes\/ on Jan 2nd, 2017 From: http:\/\/sitestree.com\/?p=10298<br \/> Categories:Programming Code Examples, Java\/J2EE\/J2ME, Java Threads<br \/>Tags:Java\/J2EE\/J2MEJava Threads<br \/> Post Data:2017-01-02 16:04:31<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>ShipSimulation.java Illustrates the basic approach of multithreaded graphics whereas a thread adjusts parameters affecting the appearance of the graphics and then calls repaint to schedule an update of the display. import java.applet.Applet; import java.awt.*; public class ShipSimulation extends Applet implements Runnable { &#8230; public void run() { Ship s; for(int i=0; i&lt;ships .length; i++) { &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=26860\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-26860","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":10212,"url":"http:\/\/bangla.sitestree.com\/?p=10212","url_meta":{"origin":26860,"position":0},"title":"Multithreaded Graphics and Double Buffering","author":"","date":"August 25, 2015","format":false,"excerpt":"ShipSimulation.java\u00a0 Illustrates the basic approach of multithreaded graphics whereas a thread adjusts parameters affecting the appearance of the graphics and then calls repaint to schedule an update of the display. \u00a0 import java.applet.Applet; import java.awt.*; public class ShipSimulation extends Applet implements Runnable { \u00a0 ... \u00a0 \u00a0 public void run()\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10526,"url":"http:\/\/bangla.sitestree.com\/?p=10526","url_meta":{"origin":26860,"position":1},"title":"HelloWWW.java Basic Hello World (Wide Web) Applet","author":"","date":"August 29, 2015","format":false,"excerpt":"********************* import java.applet.Applet; import java.awt.*; ********************* \u00a0 public class HelloWWW extends Applet { \u00a0 private int fontSize = 40; \u00a0 \u00a0 public void init() { \u00a0\u00a0\u00a0 setBackground(Color.black); \u00a0\u00a0\u00a0 setForeground(Color.white); \u00a0\u00a0\u00a0 setFont(new Font(\"SansSerif\", Font.BOLD, fontSize)); \u00a0 } \u00a0 \u00a0 public void paint(Graphics g) { \u00a0\u00a0\u00a0 g.drawString(\"Hello, World Wide Web.\", 5, fontSize+5);\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10487,"url":"http:\/\/bangla.sitestree.com\/?p=10487","url_meta":{"origin":26860,"position":2},"title":"Loading Images","author":"","date":"August 29, 2015","format":false,"excerpt":"JavaMan1.java Applet that loads an image from a relative URL. ************************************************************* import java.applet.Applet; import java.awt.*; \/** An applet that loads an image from a relative URL. \u00a0* >>>>>>>>>>>>>>>>>>> public class JavaMan1 extends Applet { \u00a0 private Image javaMan; \u00a0 public void init() { \u00a0\u00a0\u00a0 javaMan = getImage(getCodeBase(),\"images\/Java-Man.gif\"); \u00a0 } \u00a0\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10395,"url":"http:\/\/bangla.sitestree.com\/?p=10395","url_meta":{"origin":26860,"position":3},"title":"An applet that permits freehand drawing","author":"","date":"August 28, 2015","format":false,"excerpt":"import java.applet.Applet; import java.awt.*; import java.awt.event.*; \/** An applet that lets you perform freehand drawing. \u00a0* \u00a0 \u00a0**************** public class SimpleWhiteboard extends Applet { \u00a0 protected int lastX=0, lastY=0; \u00a0 public void init() { \u00a0\u00a0\u00a0 setBackground(Color.white); \u00a0\u00a0\u00a0 setForeground(Color.blue); \u00a0\u00a0\u00a0 addMouseListener(new PositionRecorder()); \u00a0\u00a0\u00a0 addMouseMotionListener(new LineDrawer()); \u00a0 } \u00a0 protected void record(int\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10483,"url":"http:\/\/bangla.sitestree.com\/?p=10483","url_meta":{"origin":26860,"position":4},"title":"Basic template for a Java applet","author":"","date":"August 29, 2015","format":false,"excerpt":"AppletTemplate.java >>>>>>>>>>>>>>>>>>>> import java.applet.Applet; import java.awt.*; ******************** public class AppletTemplate extends Applet { \u00a0 \/\/ Variable declarations. \u00a0 public void init() { \u00a0\u00a0\u00a0 \/\/ Variable initializations, image loading, etc. \u00a0 } \u00a0 public void paint(Graphics g) { \u00a0\u00a0\u00a0 \/\/ Drawing operations. \u00a0 } } >>>>>>>>>>>>>>>>>>>>>","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27152,"url":"http:\/\/bangla.sitestree.com\/?p=27152","url_meta":{"origin":26860,"position":5},"title":"Loading Images #Programming Code Examples #Java\/J2EE\/J2ME #Applets and Basic Graphics","author":"Author-Check- Article-or-Video","date":"May 12, 2021","format":false,"excerpt":"JavaMan1.java Applet that loads an image from a relative URL. ************************************************************* import java.applet.Applet; import java.awt.*; \/** An applet that loads an image from a relative URL. * >>>>>>>>>>>>>>>>>>> public class JavaMan1 extends Applet { private Image javaMan; public void init() { javaMan = getImage(getCodeBase(),\"images\/Java-Man.gif\"); } public void paint(Graphics g) {\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26860","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26860"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26860\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26860"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}