0% found this document useful (0 votes)
86 views

Slot18 2D Graphics Images

This document discusses working with images in Java, including the main classes for images (Image and BufferedImage), loading an image, drawing an image, and providing a demonstration of how to build an image viewing application. It describes loading images from files or URLs, drawing loaded images to graphics contexts, and includes code examples for an image slider application.

Uploaded by

Huy Kunn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

Slot18 2D Graphics Images

This document discusses working with images in Java, including the main classes for images (Image and BufferedImage), loading an image, drawing an image, and providing a demonstration of how to build an image viewing application. It describes loading images from files or URLs, drawing loaded images to graphics contexts, and includes code examples for an image slider application.

Uploaded by

Huy Kunn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Lecture 05

Two Dimensional Graphics


Part 3

Working with Images

Reference: Java-Tutorials/tutorial-2015/2d/images/index.html
Contents

Introduction
The java.awt.Image Class
The java.awt.image.BufferedImage Class
The java.awt
Reading/Loading an Image
Drawing an Image
Demonstrations
Teach yourself
Creating and drawing to an image
Writing/Saving an image

2
1- Introduction

There are a number of common tasks when working


with images.
Loading an external GIF, PNG JPEG image format file
into the internal image representation used by Java 2D.
Directly creating a Java 2D image and rendering to it.
Drawing the contents of a Java 2D image on to a
drawing surface.
Saving the contents of a Java 2D image to an external
GIF, PNG, or JPEG image file.

3
Introduction…
 The are two main classes that you must learn about to work with
images:
 The java.awt.Image class is the superclass that represents graphical images
as rectangular arrays of pixels.
 The java.awt.image.BufferedImage class, which extends the Image class to
allow the application to operate directly with image data (for example,
retrieving or setting up the pixel color). Applications can directly construct
instances of this class.
 The BufferedImage class is a cornerstone of the Java 2D immediate-
mode imaging API. It manages the image in memory and provides
methods for storing, interpreting, and obtaining pixel data.
Since BufferedImage is a subclass of Image it can be rendered by
the Graphics and Graphics2D methods that accept
an Image parameter.
 A BufferedImage is essentially an Image with an accessible data buffer.
It is therefore more efficient to work directly with BufferedImage.
A BufferedImage has a ColorModel and a Raster of image data. The
4 ColorModel provides a color interpretation of the image's pixel data.
Introduction…

The Raster performs the following functions:


Represents the rectangular coordinates of the image
Maintains image data in memory
Provides a mechanism for creating multiple subimages
from a single image data buffer
Provides methods for accessing specific pixels within the
image

5
2- The java.awt.Image Class

public abstract class Image extends Object


The abstract class Image is the superclass of all
classes that represent graphical images. The
image must be obtained in a platform-specific
manner.
https://docs.oracle.com/javase/8/docs/api/java/aw
t/Image.html
Constructor: Image(void)
Common Methods

6
3- The java.awt.image.BufferedImage

public class BufferedImage extends Image implements


WritableRenderedImage, Transparency
The BufferedImage subclass describes an Image with an
accessible buffer of image data. A BufferedImage is comprised
of a ColorModel and a Raster of image data. The number and
types of bands in the SampleModel of the Raster must match
the number and types required by the ColorModel to represent
its color and alpha components. All BufferedImage objects
have an upper left corner coordinate of (0, 0). Any Raster used
to construct a BufferedImage must therefore have minX=0 and
minY=0.This class relies on the data fetching and setting
methods of Raster, and on the color characterization methods
of ColorModel.

7
4- Reading/Loading an Image

Loading a local image:


BufferedImage img = null;
try {
img = ImageIO.read(new File("strawberry.jpg"));
} catch (IOException e) { }
Package javax.swing.imageio.ImageIO
Loading a remote image: contains methods for reading/writing
try { image files
URL url = new
URL(getCodeBase(),"examples/strawberry.jpg");
img = ImageIO.read(url);
The method getCodeBase() will create a
} catch (IOException e) { } connection to server to get the resource

You can use the class java.awt.Toolkit to read an image file as


8 following”
Image Img = Toolkit.getDefaultToolkit().getImage(FileName);
5- Drawing an Image
Use the method drawImage(…) of the Graphics class
 boolean Graphics.drawImage(image, x, y,…);
 x, y: left corner of the drawing area
 The observer parameter notifies the application of updates to an image that is loaded
asynchronously. The observer parameter is not frequently used directly and is not
needed for the BufferedImage class, so it usually is null.

9
Demonstration

This demonstration will help you


how to develop a program for
viewing image list stored in your
10
computer.
Demonstration…

Click for adding images to the


list
Click for viewing an image

Remove some images from the


Change slide
list
rate
Click for auto sliding
11
Design

JSplitPane1 Properties
Orientation:
HORIZONTAL_SPLIT
DividerLocation: 250
lstFiles Properties
SelectionMode: SINGLE
Model: Blank
pView Properties
PreferredSize: (1000,800)

12
Code

The program
supports auto-
sliding through a
thread. So, an
inner class is
declared.

13
Code
Step 2
Step 3

Step
1:
Right
click
to the
listbox
We want the list of filenames (the variable list)
will be the model of the listbox lstList. Return to
the design window to complete this work.

14 Step 4
Code

15
Code

16
Code

17
Code

18
Code

19
Code

20
Summary

Introduction
The java.awt.Image Class
The java.awt.image.BufferedImage Class
The java.awt
Reading/Loading an Image
Drawing an Image
Demonstrations

21
Summary

Introduction
Reading/Loading an Image
Drawing an Image
Creating and Drawing to an Image
Demonstration

22
Thank You

23

You might also like