Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Animation in Applet

  • The java.awt.Graphics class and repaint() method can be used to add animation to Java applets. The following steps are required in order to create an animation in a Java applet:
  • Override the paint() method: The paint() method is called whenever the applet needs to be redrawn. You can override this method to draw the objects that you want to animate.
  • Update the position of the objects: In the paint() method, update the position of the objects that you want to animate. This can be done using mathematical formulas or other algorithms.
  • Call the repaint() method: After updating the position of the objects, call the repaint() method. This will trigger another call to the paint() method, allowing you to redraw the objects in their new positions.
  • Use a java.util.Timer or java.util.ScheduledThreadPoolExecutor: To control the speed of the animation, you can use a java.util.Timer or java.util.ScheduledThreadPoolExecutor to schedule calls to the repaint() method at a specified interval.





Simple example of Applet by html file

import java.awt.*;
import java.applet.*;
import java.util.Timer;
import java.util.TimerTask;

public class BouncingBallApplet extends Applet {
  int x, y;
  int dx = 5, dy = 5;
  
  public void init() {
    setBackground(Color.WHITE);
    x = 0;
    y = 50;
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
      public void run() {
        x += dx;
        y += dy;
        if (x > getWidth() - 50 || x < 0)
          dx = -dx;
        if (y > getHeight() - 50 || y < 0)
          dy = -dy;
        repaint();
      }
    }, 0, 50);
  }

  public void paint(Graphics g) {
    g.setColor(Color.RED);
    g.fillOval(x, y, 50, 50);
  }
}




  • In this example, the run() method is called at predetermined intervals of 50 milliseconds using a java.util.Timer. The paint() method is called after the run() method, which updates the position of the ball and calls the repaint() method, to redraw the ball in its new location.

  • This is merely a straightforward illustration of the fundamental idea of animation in Java applets. Additional graphical primitives, such as lines, shapes, and images, as well as more complex algorithms to update the positions of the objects, can be used to make animations that are more complex.


EventHandling in Applet Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet

Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.