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

Display image in Applet

  • To load the image and draw the Java applet, we use java.awt.Image and java.awt.Graphics classes. The steps involved are described in more detail below:
  • Loading the image : The applet must first be loaded with the image file before continuing. This is typically done by invoking the init() method, which is called when the applet is first loaded. The getImage() method, which takes the URL of the image as input and returns an Image object, can be used to load an image. A "image.jpg" image file loaded from the applet's directory can be seen in the below example.
    public void init() {
        Image img = getImage(getCodeBase(), "image.jpg");
    }
    





  • Drawing the image : The java.awt.Graphics class can be used to draw the image on the applet after it has been loaded. This is usually done using the paint() method, which is called whenever the applet needs to be redraw. You can draw an image by passing an Image object, the x and y coordinates of where it should be drawn, and the applet itself to the Graphics class's drawImage() method. Here is an example applet that demonstrates how to draw an image.
    public void paint(Graphics g) {
        Image img = getImage(getCodeBase(), "image.jpg");
        g.drawImage(img, 0, 0, this);
    }
    

    In this case, the applet itself, the Image object, and the x and y coordinates of (0, 0) are passed as parameters to the drawImage() method. This will cause the applet's top-left image to be drawn.


  • Handling errors : Loading an image from a distant server can take some time, and there is a chance that something could go wrong. To handle errors and catch any exceptions that might be thrown during image loading, use a try-catch block. Here is an example of loading an image and handling any possible exceptions.




    public void init() {
        try {
            Image img = getImage(new URL("http://example.com/image.jpg"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    The drawImage() method in this case receives as parameters the applet itself, the Image object, and the (0, 0) x and y coordinates. This will cause the applet's top-left image to be drawn.


    It's crucial to keep in mind that images should load asynchronously in the init() method to avoid interrupting the applet's user interface. Use methods like getImage(), which load the image in the background and return right away, in order to achieve this. The handling of any exceptions that might be thrown when loading the image is something else you should remember to do.



how to display an image in an applet




import java.awt.*;
import java.applet.*;

public class ImageApplet extends Applet {
  Image img;

  public void init() {
    img = getImage(getCodeBase(), "image.jpg");
  }

  public void paint(Graphics g) {
    g.drawImage(img, 0, 0, this);
  }
}
Displaying Graphics 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.