Home » Desktop Java » awt » Drawing an image example

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Drawing an image example

In this tutorial we are going to show you how to draw on an Image. This is quite useful when you want to further customize the graphics of your application, if you are not pleased with the original Image.

In short in order to Draw on a Buffered Image one should take the following steps:

  • Load an image from a source usingToolkit.getDefaultToolkit().getImage method
  • Use an ImageObserver to monitor the loading of the image. When the image is fully load the user will be notified
  • Create class that extends Component and overrite the paint method
  • Use graphics.setColor(Color.red) and graphics.fill(new Ellipse2D.Float(0, 0, 100, 50)) to draw a simple shape in the Image
  • Use Graphics2D.drawImage to draw the image

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.desktop;

import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;

public class DrawImage {

  static Image image;

  public static void main(String[] args) {

// The image URL - change to where your image file is located!

String imageURL = "image.png";

// This call returns immediately and pixels are loaded in the background

image = Toolkit.getDefaultToolkit().getImage(imageURL);

// Create a frame

Frame frame = new Frame();

// Add a component with a custom paint method

frame.add(new CustomPaintComponent());

// Display the frame

int frameWidth = 300;

int frameHeight = 300;

frame.setSize(frameWidth, frameHeight);

frame.setVisible(true);

 }

 /**
  * To draw on the screen, it is first necessary to subclass a Component 
  * and override its paint() method. The paint() method is automatically called 
  * by the windowing system whenever component's area needs to be repainted.
  */
  static class CustomPaintComponent extends Component {

public void paint(Graphics g) {

  // Retrieve the graphics context; this object is used to paint shapes

  Graphics2D g2d = (Graphics2D)g;

  /**
   * Draw an Image object
   * The coordinate system of a graphics context is such that the origin is at the 
   * northwest corner and x-axis increases toward the right while the y-axis increases 
   * toward the bottom.
   */

  int x = 0;

  int y = 0;

  g2d.drawImage(image, x, y, this);

}

  }

}

 
This was an example on drawing an image.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of