FileDocCategorySizeDatePackage
DragImage.javaAPI DocExample1549Sat Apr 23 22:35:40 BST 2005None

DragImage

public class DragImage extends JComponent implements MouseMotionListener

Fields Summary
static int
imageWidth
static int
imageHeight
int
grid
int
imageX
int
imageY
Image
image
Constructors Summary
public DragImage(Image i)


     
    image = i;
    addMouseMotionListener(this);
  
Methods Summary
public static voidmain(java.lang.String[] args)

    String imageFile = "L1-Light.jpg";
    if (args.length > 0)
      imageFile = args[0];

	// Turn off double buffering
	//RepaintManager.currentManager(null).setDoubleBufferingEnabled(false);

    Image image = Toolkit.getDefaultToolkit().getImage(
        DragImage.class.getResource(imageFile));
	image = image.getScaledInstance(imageWidth,imageHeight,Image.SCALE_DEFAULT);
    JFrame frame = new JFrame("DragImage");
    frame.getContentPane().add( new DragImage(image) );
    frame.setSize(300, 300);
	frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    frame.setVisible(true);
  
public voidmouseDragged(java.awt.event.MouseEvent e)

    imageX = e.getX();
    imageY = e.getY();
    repaint();
  
public voidmouseMoved(java.awt.event.MouseEvent e)

public voidpaint(java.awt.Graphics g)

    Graphics2D g2 = (Graphics2D)g;

    int w = getSize().width / grid;
    int h = getSize().height / grid;
    boolean black = false;
    for (int y = 0; y <= grid; y++)
      for (int x = 0; x <= grid; x++) {
        g2.setPaint(black ? Color.black : Color.white);
        black = !black;
        g2.fillRect(x * w, y * h, w, h);
      }
    g2.drawImage(image, imageX, imageY, this);