Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
How To Move Image Smoothly 
 

In this example you will see how to move image smoothly on mouse motion using Java.

 

How To Move Image Smoothly

                         

In this example you will see how to move image smoothly on mouse motion using Java. 

To move an image, we have used the Listener interface that provides the mouse motion events in Java. So in this example you will learn where the MouseMotionListener interface is called to move an image on mouse motion. In this example we have used the JFrame class of javax.swing.* package that allows to create frame. 

The methods mouseMoved() and mouseDragged() of Listener interface are defined in the code. The method mouseDragged() is invoked when a mouse button is pressed on the image. Then the image will dragged. The method mouseMoved() is invoked when the mouse cursor has been moved on to a image without pushing the image. The method checkImage() will create the Off-screen image if it has not been created. This method should always be called before using the off-screen image. To draw into the offscreen image, paintOffscreen(image.getGraphics()) is called. The g.drawImage(image, 0, 0, null) put the offscreen image on the screen.

 

 

Here is the code of SmoothMoveExample.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class SmoothMoveExample extends JPanel implements
 
MouseMotionListener {
  private int X, Y;
  private Image image;

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new SmoothMoveExample());
    frame.setSize(350300);
    frame.show();
  }
  public SmoothMoveExample() {
    addMouseMotionListener(this);
    setVisible(true);
  }
  public void mouseMoved(MouseEvent event) {
     X = (int) event.getPoint().getX();
    Y = (int) event.getPoint().getY();
    repaint();
  }
  public void mouseDragged(MouseEvent event) {
    mouseMoved(event);
  }
  public void update(Graphics graphics) {
    paint(graphics);
  }
  public void paint(Graphics g) {
  Dimension dim = getSize();
    checkImage();
    Graphics graphics = image.getGraphics();
    graphics.setColor(getBackground());
    graphics.fillRect(00, dim.width, dim.height);
  paintOffscreen(image.getGraphics());
    g.drawImage(image, 00null);
  }
  private void checkImage() {
    Dimension dim = getSize();
    if (image == null || image.getWidth(null) != dim.width
        || image.getHeight(null) != dim.height) {
      image = createImage(dim.width, dim.height);
    }
  }
  public void paintOffscreen(Graphics g) {
    int size = 150;
    g.setColor(Color.pink);
    g.fillOval(X - size  / 2, Y - size  / 2, size , size );
  }
}

 

Output will be displayed as:

 

Download Source Code

 

 

                         

» View all related tutorials
Related Tags: c com image class list object diff components stack icons user sed entity icon order component ai define container show

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.