Image with mouse drag and move event : Image « 2D Graphics GUI « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » 2D Graphics GUI » ImageScreenshots 
Image with mouse drag and move event
Image with mouse drag and move event



import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

public class BufferedImageMouseDrag extends JFrame {
  DisplayCanvas canvas;

  public BufferedImageMouseDrag() {
    super();
    Container container = getContentPane();

    canvas = new DisplayCanvas();
    container.add(canvas);

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    setSize(450400);
    setVisible(true);
  }

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

class DisplayCanvas extends JPanel {
  int x, y;

  BufferedImage bi;

  DisplayCanvas() {
    setBackground(Color.white);
    setSize(450400);
    addMouseMotionListener(new MouseMotionHandler());

    Image image = getToolkit().getImage("largeJava2sLogo.gif");

    MediaTracker mt = new MediaTracker(this);
    mt.addImage(image, 1);
    try {
      mt.waitForAll();
    catch (Exception e) {
      System.out.println("Exception while loading image.");
    }

    if (image.getWidth(this== -1) {
      System.out.println("no gif file");
      System.exit(0);
    }

    bi = new BufferedImage(image.getWidth(this), image.getHeight(this),
        BufferedImage.TYPE_INT_ARGB);
    Graphics2D big = bi.createGraphics();
    big.drawImage(image, 00this);
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2D = (Graphics2Dg;

    g2D.drawImage(bi, x, y, this);
  }

  class MouseMotionHandler extends MouseMotionAdapter {
    public void mouseDragged(MouseEvent e) {
      x = e.getX();
      y = e.getY();
      repaint();
    }
  }
}
           
       
Related examples in the same category
1. Image size Image size
2. Image demoImage demo
3. Paint an IconPaint an Icon
4. Image Processing: Brightness and ContrastImage Processing: Brightness and Contrast
5. Image Animation and ThreadImage Animation and Thread
6. Image Color Gray EffectImage Color Gray Effect
7. Image BufferingImage Buffering
8. Image Effect: CombineImage Effect: Combine
9. AffineTransform demoAffineTransform demo
10. Image Effect: Rotate Image using DataBufferImage Effect: Rotate Image using DataBuffer
11. Image Effect: Sharpen, blurImage Effect: Sharpen, blur
12. Image scale
13. Image crop
14. Demonstrating the Drawing of an Image with a Convolve Operation
15. Demonstrating Use of the Image I/O Library
16. Adding Image-Dragging Behavior
17. Sending Image Objects through the ClipboardSending Image Objects through the Clipboard
18. Anti AliasAnti Alias
19. Image OperationsImage Operations
20. Image ViewerImage Viewer
21. Standalone Image Viewer - works with any AWT-supported format
22. Toolkit.getImage() which works the same in either Applet or Application
23. Image Processing Test Image Processing Test
24. Image Transfer TestImage Transfer Test
25. Double Buffered Image
26. Graband Fade: displays image and fades to black
27. Graband Fade with Rasters
28. Rotate Image 45 Degrees
29. Convert java.awt.image.BufferedImage to java.awt.Image
30. Filter image by multiplier its red, green and blue color
31. Drags within the imageDrags within the image
ww__w__._j_ava2s.c__o_m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.