0% found this document useful (0 votes)
7 views11 pages

JavaImageProject Documentation

This Java image processing project reads a PNG image, manipulates its pixels to create a flipped version, and displays it in a continuously rotating animation using Java Swing. It consists of four main classes: PNGReader for reading images, PNGWriter for saving images, FlipImage for processing the pixel transformations, and ImageAnimation for rendering the animated output. The project showcases file handling, pixel manipulation, and GUI rendering in Java.

Uploaded by

Kidula George
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views11 pages

JavaImageProject Documentation

This Java image processing project reads a PNG image, manipulates its pixels to create a flipped version, and displays it in a continuously rotating animation using Java Swing. It consists of four main classes: PNGReader for reading images, PNGWriter for saving images, FlipImage for processing the pixel transformations, and ImageAnimation for rendering the animated output. The project showcases file handling, pixel manipulation, and GUI rendering in Java.

Uploaded by

Kidula George
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Image Processing Project

Technical Documentation

Overview
This project is a Java image processing system that reads a PNG image, manipulates its
pixels at the coordinate level, and generates a transformed output. It is extended with an
animation viewer that renders the processed image using Java Swing and rotates it
continuously in a windowed display.

The project demonstrates:


- File handling in Java
- Image reading and writing using BufferedImage
- Pixel-level RGB manipulation
- Coordinate transformations (flipping and rotation)
- GUI rendering with Java Swing and Graphics2D

System Architecture
The project is composed of four classes, each with a distinct responsibility:

File Role
[Link] Reads the input image and extracts pixel
data
[Link] Creates a new image canvas and saves it
to disk
[Link] Applies the 180-degree flip transformation
[Link] Loads the output image and renders a live
rotation animation

Flow of Execution
[Link] --> PNGReader --> pixel flip logic --> PNGWriter -->
[Link]
|

ImageAnimation
(Swing GUI
viewer)
Project Structure
ImageFlipProject/
|
|-- [Link] # Main flip program
|-- [Link] # Image reader
|-- [Link] # Image writer
|-- [Link] # Swing animation viewer
|-- [Link] # Input image
|-- [Link] # Generated output image
1. [Link]
Purpose
Reads a PNG image from disk and provides access to its dimensions and individual pixel
RGB values. It acts as the data source for the transformation pipeline.

Imports Used
import [Link];
import [Link];
import [Link];

BufferedImage is the core Java class for holding raster image data in memory. ImageIO
provides static methods to read and write image files. File wraps the filesystem path.

Full Code
public class PNGReader {

private BufferedImage image;

// Loads image from file


public PNGReader(String filename) {
try {
image = [Link](new File(filename));
} catch (Exception e) {
[Link]();
}
}

// Returns image width in pixels


public int getWidth() {
return [Link]();
}

// Returns image height in pixels


public int getHeight() {
return [Link]();
}

// Returns RGB components of a pixel as int[]{r, g, b}


public int[] getPixel(int x, int y) {
int rgb = [Link](x, y);
int r = (rgb >> 16) & 0xff;
int g = (rgb >> 8) & 0xff;
int b = rgb & 0xff;
return new int[]{r, g, b};
}
}
How getPixel Works
The getRGB() method returns a single integer packing all three colour channels. Bit shifting
extracts each channel: shifting right by 16 bits and masking with 0xFF isolates red; 8 bits for
green; no shift for blue. The result is returned as a three-element integer array.

2. [Link]
Purpose
Creates a blank image canvas of a specified size, accepts pixel writes at given coordinates,
and saves the final image to a PNG file on disk.

Imports Used
import [Link];
import [Link];
import [Link];

Full Code
public class PNGWriter {

private BufferedImage image;


private String filename;

// Creates empty image canvas


public PNGWriter(String filename, int width, int height) {
[Link] = filename;
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
}

// Writes a pixel at (x, y) using RGB array


public void setPixel(int x, int y, int[] rgb) {
int r = rgb[0];
int g = rgb[1];
int b = rgb[2];
int value = (r << 16) | (g << 8) | b;
[Link](x, y, value);
}

// Saves completed image to disk


public void close() {
try {
[Link](image, "png", new File(filename));
} catch (Exception e) {
[Link]();
}
}
}
How setPixel Works
The three RGB integer values are packed back into a single integer using bit shifting and the
bitwise OR operator. Red is shifted left 16 bits, green 8 bits, and blue occupies the lowest
byte. This packed integer is passed to setRGB() to write the pixel colour into the canvas.
3. [Link]
Purpose
The main program. Reads the input image, loops over every pixel, calculates the new flipped
position, and writes it to the output canvas.

Full Code
public class FlipImage {

public static void main(String[] args) {

// Step 1: Load image


PNGReader reader = new PNGReader("[Link]");
int width = [Link]();
int height = [Link]();

// Step 2: Create output canvas


PNGWriter writer = new PNGWriter("[Link]", width, height);

// Step 3: Loop through every pixel


for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {

// Get original pixel


int[] pixel = [Link](x, y);

// Flip both axes


int newX = width - 1 - x;
int newY = height - 1 - y;

// Write to flipped position


[Link](newX, newY, pixel);
}
}

// Step 4: Save output


[Link]();
[Link]("Flipped image saved as [Link]");
}
}

Flip Logic Explained


The transformation mirrors the image along both the horizontal and vertical axes
simultaneously, which is equivalent to a 180-degree rotation.

Formula Effect
newX = width - 1 - x Mirrors pixel horizontally (left becomes
right)
newY = height - 1 - y Mirrors pixel vertically (top becomes
bottom)
Both applied together Produces a 180-degree rotated image

4. [Link]
Purpose
Loads the generated [Link] output and displays it inside a Swing window. A timer fires
repeatedly to increment a rotation angle and trigger a repaint, creating a smooth continuous
rotation animation.

Imports Used
import [Link].*; // JPanel, JFrame, Timer
import [Link].*; // Graphics, Graphics2D, RenderingHints
import [Link];
import [Link];
import [Link];

Each import serves a specific role:


- [Link] provides the GUI components: JPanel as the drawing surface, JFrame
as the window, and Timer as the animation driver.
- [Link] provides the Graphics and Graphics2D classes used for actual rendering,
and RenderingHints for anti-aliasing.
- BufferedImage, ImageIO, and File are used to load the PNG from disk.

Full Code
public class ImageAnimation extends JPanel {

private BufferedImage image;


private double angle = 0; // current rotation angle in radians

public ImageAnimation() {

// Load the flipped output image


try {
image = [Link](new File("[Link]"));
} catch (Exception e) {
[Link]("Error loading image!");
[Link]();
}

// Timer fires every 50ms (~20 frames per second)


Timer timer = new Timer(50, e -> {
angle += 0.05; // increment rotation angle
repaint(); // trigger paintComponent()
});
[Link]();
}

@Override
protected void paintComponent(Graphics g) {
[Link](g);
if (image == null) return;

Graphics2D g2 = (Graphics2D) g;

// Enable smooth anti-aliased rendering


[Link](RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

int centerX = getWidth() / 2;


int centerY = getHeight() / 2;

// Move the coordinate origin to the panel centre


[Link](centerX, centerY);

// Apply current rotation angle


[Link](angle);

// Draw image so its centre aligns with the origin


[Link](
image,
-[Link]() / 2,
-[Link]() / 2,
null
);
}

public static void main(String[] args) {


JFrame frame = new JFrame("Image Animation Viewer");
ImageAnimation panel = new ImageAnimation();
[Link](600, 600);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](panel);
[Link](true);
}
}
Animation System Breakdown

Class Declaration: extends JPanel


ImageAnimation extends JPanel, which means it IS a Swing panel component. By overriding
paintComponent(), all custom drawing is performed inside this class rather than through an
external renderer.

Constructor: Loading the Image and Starting the Timer


The constructor loads [Link] using [Link](). It then creates a [Link]
with a 50-millisecond delay. Every time the timer fires, the lambda increments the angle
variable by 0.05 radians and calls repaint(). Calling repaint() queues a call to
paintComponent() on the Event Dispatch Thread, which is the correct way to trigger redraws
in Swing.

paintComponent: The Rendering Method


This method is called automatically every time Swing redraws the panel. It must always call
[Link](g) first to clear the previous frame. The Graphics object is cast to
Graphics2D to access the transform and rendering hint APIs.

Anti-Aliasing
The rendering hint KEY_ANTIALIASING with VALUE_ANTIALIAS_ON instructs the renderer
to smooth out jagged edges on the rotated image. Without this, diagonal edges appear as
staircase-like pixel steps.

Coordinate Transform: translate and rotate


Rather than calculating new pixel positions manually, Graphics2D uses an affine transform.
First, translate() moves the drawing origin from the top-left corner to the centre of the panel.
Then rotate(angle) applies a rotation around that new origin. When drawImage() is called
with negative offsets equal to half the image dimensions, the image is drawn centred on the
origin, which means it rotates around its own centre point.

Transform Step Effect


[Link](centerX, centerY) Moves origin to panel centre
[Link](angle) Rotates all subsequent drawing by current
angle
drawImage(-width/2, -height/2, ...) Centres the image on the new origin

Timer and Frame Rate


The Timer delay is 50 milliseconds, which corresponds to approximately 20 frames per
second. The angle increment of 0.05 radians per frame produces roughly one full rotation
every 6.3 seconds (2*pi / 0.05 = 125.6 frames at 50ms each).
main Method: Window Setup
The main method creates a JFrame window of 600 by 600 pixels, adds the animation panel
to it, and makes it visible. EXIT_ON_CLOSE ensures the JVM terminates when the window
is closed.

How to Compile and Run


Step 1: Open a terminal in the project folder
cd ~/Projects/Scada/Assignments

Step 2: Compile all files


javac *.java

This compiles all four Java files and generates the corresponding .class files.

Step 3: Run the flip program


java FlipImage

Expected output:
Flipped image saved as [Link]

Step 4: Run the animation viewer


java ImageAnimation

A 600x600 window will open displaying the flipped image rotating continuously.

Common Issues
Problem Fix
[Link] not found Ensure [Link] is in the same folder as
the .class files. File names are case-
sensitive.
[Link] not generated Ensure [Link]() is called. Without it
the file is never written to disk.
Animation window does not open Ensure FlipImage was run first so that
[Link] exists before launching
ImageAnimation.
Compilation error Run javac *.java from the correct directory.
Ensure all four .java files are present.

Summary
This project demonstrates how images are stored as arrays of pixel data, how Java reads
and writes image files using BufferedImage and ImageIO, how coordinate arithmetic
transforms an image through flipping, and how the Swing framework combined with
Graphics2D and a Timer can produce smooth real-time animation. The same foundations
can be extended to support 90 and 270-degree rotation, grayscale conversion, blur filters,
and edge detection algorithms.

You might also like