1 Replies - 669 Views - Last Post: 01 February 2010 - 10:19 AM

#1 WolfCoder   User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 828
  • View blog
  • Posts: 7,696
  • Joined: 05-May 05

Graphics2D Image Drawing Object Example

Posted 29 October 2007 - 07:41 AM

Description: Just replace the image file with something from your own hard drive, and be sure to include the full path. Exercises: Try and make it so that you can just provide a file name without the full path and have it work. Try and add some scaling and rotation methods.Need some practice at JAVA? Wrap up image drawing for Graphics2D contexts to make the task of drawing images simple and to your design.
// JAVA-AWT
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.Graphics;

// Pay attention to these required imports! There's a buffered image, a file to point to an image on disk, and an ImageIO to load it with
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.awt.geom.AffineTransform;

// JAVAX-SWING
import javax.swing.JFrame;
/**
 * Demonstrates the use of images in GUI windows.
 *
 * @author WolfCoder
 */
public class ImageTest extends JFrame
{
	public static WolfCoderImage testImage; // See? I want to make it so I don't have to type all that crap for every image.
	/**
	 * Here is a nice self contained image class to make image handling for standard disk reading easy!
	 * You should put this in it's own file and remove the STATIC for optimal use.
	 */
	public static class WolfCoderImage
	{
		private BufferedImage bufferedImage; // Image itself
		private File imageFile; // File for image
		private static ImageIO imageIO; // Only one of these are needed for ALL images we create, so it's STATIC
		private static AffineTransform transformer; // Usually used as an identity matrix
		/**
		 * Creates a new image for you from the filename string given.
		 *
		 * @param fileName  The file name for the image.
		 */
		public WolfCoderImage(String fileName)
		{
			// Create file
			imageFile = new File(fileName);
			try
			{
				// Read the file...
				bufferedImage = imageIO.read(imageFile);
			}
			catch(IOException e)
			{
				// Something went wrong!
				System.out.println("Error loading "+fileName);
				System.out.println(e.toString());
			}
		}
		/**
		 * Draws the image on a Graphics2D context.
		 *
		 * @param g  The Graphics2D object to draw this image to.
		 */
		public void draw(Graphics2D g,double x,double y)
		{
			// Draw (we just want to place it somewhere, so AffineTransform has this method)
			g.drawRenderedImage(bufferedImage,transformer.getTranslateInstance(x,y));
		}
	}
	/**
	 * This is the test canvas on which the image is drawn.
	 */
	public static class TestCanvas extends Canvas
	{
		/**
		 * Creates a new test canvas.
		 */
		public TestCanvas()
		{
			// Set properties
			setSize(new Dimension(128,128));
			setBackground(Color.WHITE);
		}
		/**
		 * Paints this canvas.
		 */
		public void paint(Graphics g)
		{
			// Use graphics 2D
			Graphics2D g2d = (Graphics2D)g;
			g2d.clearRect(0,0,getWidth(),getHeight()); // Clear the canvas from previous image
			// Draw something
			g2d.setColor(new Color(128,128,128)); // Use grey color
			g2d.drawRect(2,2,128-5,128-5); // Draw a border
			// Now draw the image (lol it might not fit!)
			testImage.draw(g2d,0,0);
		}
	}
	/**
	 * The program begins here.
	 *
	 * @param args  This is unused.
	 */
	public static void main(String[] args)
	{
		// Create self
		ImageTest imageTest = new ImageTest();
		imageTest.setTitle("Image Test"); // Set Title
		imageTest.setSize(new Dimension(640,480)); // Make a generic window
		imageTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Program will stop if this window is closed
		// Create the image here (could be nearly anywhere you know)
		testImage = new WolfCoderImage("C:\Documents and Settings\WolfCoder\My Documents\My Pictures\firefox os tan wallpaper.jpg"); // KAWAII! But... You can't see it. It's the chibi character around the globe on a orange world map background, I use this wallpaper for my laptop.
		// Add canvas
		TestCanvas testCanvas = new TestCanvas();
		imageTest.add(testCanvas,BorderLayout.CENTER); // Canvas will fill AS MUCH OF THE WINDOW as it can from the center, according to this layout.
		// Show window
		imageTest.pack(); // Makes the window as small as possible
		imageTest.setLocationRelativeTo(null); // Best if used AFTER pack for a centered window
		imageTest.setVisible(true);
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Graphics2D Image Drawing Object Example

#2 muneer82   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-January 10

Re: Graphics2D Image Drawing Object Example

Posted 01 February 2010 - 10:19 AM


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1