Home Tutorial Java Swing Move Image in Java Swing

 
 

Move Image in Java Swing
Posted on: October 31, 2009 at 12:00 AM
In this section, you will learn how to move an image using mouse.

How to Move Image in Java Swing

In this section, you will learn how to move an image using mouse. For this purpose, we have specified an image and using the MediaTracker class, the image is tracked by the method addImage(). The Graphics class then rendered this image on frame through the method drawImage(). Now to move this image, we have used public void mouseMoved() and public void mouseDragged() methods.

Here is the code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.border.TitledBorder;

public class MoveImage extends JFrame {
	ShowCanvas canvas;

	public MoveImage() {
		super();
		Container container = getContentPane();
		canvas = new ShowCanvas();
		container.add(canvas);
		setSize(300, 200);
		setVisible(true);
	}

	public static void main(String arg[]) {
		new MoveImage();
	}
}

class ShowCanvas extends JPanel {
	int x, y;
	BufferedImage image;

	ShowCanvas() {
		setBackground(Color.white);
		setSize(450, 400);
		addMouseMotionListener(new MouseMotionHandler());

		Image img = getToolkit().getImage("node.jpg");

		MediaTracker mt = new MediaTracker(this);
		mt.addImage(img, 1);
		try {
			mt.waitForAll();
		} catch (Exception e) {
			System.out.println("Image not found.");
		}
		image = new BufferedImage(img.getWidth(this), img.getHeight(this),
				BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2 = image.createGraphics();
		g2.drawImage(img, 0, 0, this);
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2D = (Graphics2D) g;
		g2D.drawImage(image, x, y, this);
	}

	class MouseMotionHandler extends MouseMotionAdapter {
		public void mouseDragged(MouseEvent e) {
			x = e.getX();
			y = e.getY();
			repaint();
		}

		public void mouseMoved(MouseEvent e) {
		}
	}
}

Related Tags for Move Image in Java Swing:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.