• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Applets » How to loading images in java
Next →
← Prev

How to loading images in java

By Dinesh Thakur

The Java AWT provides several classes to manage loading and displaying images. The most common image code you see on the Web is called an animator. There are numerous animation applets and classes available on the WWW. The Java Developer’s Kit provides a set of demo applications that draw images using static, single-buffered, and double-buffered animation.

First, we will show you the basics of drawing single images. In fact, two of the classes we developed earlier actually did everything you need to know to draw an image into a graphics context.

if (image != null) {
   g.drawlmage (image,xloc,yloc, getBackground() ,this);
} 

This call simply draws the “image” object at (xloc,yloc) in the graphics context g. Any transparent pixels are drawn in the background color of the component, and “this” object is notified when the image is drawn. Even though this is a very standard way of drawing images, there are numerous other ways to call drawlmage.

Of course, before you can draw an image, you have to obtain one. Because Java is very biased toward supporting the WWW, the AWT provides a full-featured set of dynamic image loaders. The following code excerpt shows both local and network-based image loading.

if (inAnApplet) {
image = getlmage (getCodeBase (), "./class1312.gif ");
} else {
image = tk.getImage ("./class1312.gif");
} 

If the image is stored locally on the machine (typical for applications) you use the default Toolkit object to load the image file. The Toolkit.getImage method receives one parameter (the filename) and begins loading the image file into a new Image object. The preceding example shows a simple GIF image file. The AWT also provides integral support for jpeg images.

If the image is to be obtained from the network, the Applet.getlmage method should be used. You pass the directory containing the image (as a URL) along with the base filename (the same GIF as before). The HTTP server on the other end of the Applet connection should process the HTTP request for the image URL just like any other WWW HTTP request. Note that there is no requirement that the HTTP server be a full-fledged WWW server, it just must support the basic operations required to pass the image. If you were developing a large distributed imaging system, you might provide a custom image server that could only serve images, or otherwise differentiate itself from a standard web server.

After starting the image load as already shown, you can proceed to other work. If the image is loaded locally by the toolkit, it will usually load immediately. If the image must be obtained from a web server, there may be a substantial (perhaps infinite!) delay before the image arrives. The AWT provides two methods for dealing with image-loading delays. We chose to use the very simple MediaTracker method.

tracker = new MediaTracker(this);
tracker.addlmage(image,0); 

MediaTracker provides for checking the status of the image loading process for as many images as you need. The preceding example shows the case of waiting on a single image.

First, you must create a new MediaTracker instance. The MediaTracker constructor is passed an object that is normally where the image will eventually be painted. Note that this doesn’t absolutely mean that the parameter is the actual graphics context or component, but it usually is. For our purposes, we’ll assume that this is the case. After the MediaTracker is instantiated, you would add the images to be tracked by calling MediaTracker.addlmage () . There are two forms of addlmage; the first receives only the Image object and a unique id that you will use later as a reference ID when working with the tracker. The second form of addlmage also receives a width and height that will be used to scale the image as it loads. According to the MediaTracker class documentation, images managed with lower ID’s receive some preferential treatment while loading. It’s not clear how you can impact the server to deliver them any differently, but there are potential local optimizations that might be done. Note that the image doesn’t begin loading the instant you call addlmage. Until you call a status or wait method, the image-loading process remains pending.

The MediaTracker can be run in it’s own thread or in the main tread. If you are loading images that must completely load before you can do anything else, you might wait in the main thread. If you can do other useful work before worrying about the image, you would probably use multiple threads via the Runnable interface.
Our example uses the main thread, since we load only a single image and are willing to wait for it to finish loading.

System.out.println ("waiting") ;
try {
tracker.waitForID(0) ;
} catch (InterruptedException e) {
}
System.out.println("not waiting") ; 

Essentially, if you call one of the MediaTracker. wait xxx methods you just block until the image loads, or something “bad” happens. Our example waits for a single image, referenced by the id we passed into addlmage. When the image finishes loading, wai tForID returns, and we can be assured that the image is ready to display. If there is an error (fairly common when the networked version of getlmage is used), the MediaTracker throws an exception, and we can try and deal with it. Depending on what your application requirements are, you might try loading the image from an alternate source or just fake something up internal to your app. One obvious alternative is to follow the HTML<IMG> tag’s lead and just use drawText to render an alternate text string into an image object.

MediaTracker also provides checkAll and checkID methods that give you the capability to check on and initiate the load status of images. You can use these methods to check on the load status of important images and alter the application activity depending on what the status is. For instance, an animation program might render frames faster or slower depending on how fast the images are being received from the server. You might even decide to pause the animation or skip some frames and wait for special sync frames to arrive. This is much the way some MPEG-viewers operate.

The statusID and statusAll methods provide yet another way to begin the loading of images queued up in MediaTracker. However, their primary purpose is to give you an indication of the current status of images being tracked.

For tracking image loading where you need more precise information than MediaTracker provides, we suggest you investigate the ImageObserver interface. We personally use MediaTracker because we believe that it provides sufficient capability for normal application image loading.

You’ll also like:

  1. How to Displaying Images in java
  2. Check Images in Java Applet Example
  3. How To Create SEO Friendly Images in WordPress
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW