/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.applet.*; import java.awt.*; // this line inherits applet class from the class Applet public class FirstApplet extends Applet { int x_pos = 10; int y_pos = 100; int radius = 20; private Image dbImage; private Graphics dbg; public void run() { // lower ThreadPriority Thread.currentThread().setPriority(Thread.MIN_PRIORITY); while (true) { // changing the x - position of the ball/circle x_pos++; // repaint the applet repaint(); try { // Stop thread for 20 milliseconds Thread.sleep(20); } catch (InterruptedException ex) { //do nothing } // set ThreadPriority to maximum value Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } // Double buffering - allows smooth animations public void update(Graphics g) { if (dbImage == null) { dbImage = createImage(this.getSize().width, this.getSize().height); dbg = dbImage.getGraphics(); } // clear screen in background dbg.setColor(getBackground()); dbg.fillRect(0, 0, this.getSize().width, this.getSize().height); // draw elements in background dbg.setColor(getForeground()); paint(dbg); // draw image on the screen g.drawImage(dbImage, 0, 0, this); } public void paint(Graphics g) { // set color g.setColor(Color.red); // paint a filled colored circle g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius); } }