import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;

public class EggDrag extends Applet
{	public EggDrag()
	{	super();
		egg = new Ellipse2D.Double(0, 0, 
			EGG_WIDTH, EGG_HEIGHT);
		
		contorno = new Rectangle2D.Double();
		
		MyMouseMotionListener listener = new MyMouseMotionListener();
		addMouseListener(listener);
		addMouseMotionListener(listener);
	}

	public void paint(Graphics g)
	{	super.paint(g);
		Graphics2D g2 = (Graphics2D)g;
		int w = this.getWidth();
		int h = this.getHeight();
//		contorno.setFrame(0, 0, w, h); // questa non funziona
		contorno.setFrame(0, 0, w-1, h-1);
		g2.draw(contorno);
		g2.fill(egg);
//		g2.drawString("x="+egg.x+" y="+egg.y,w - 100,h - 30);
	}

	private Ellipse2D.Double egg;
	private static final double EGG_WIDTH = 30, EGG_HEIGHT = 50;
	
	private Rectangle2D.Double contorno;


	// definizione della classe interna

	private class MyMouseMotionListener extends MouseAdapter implements MouseMotionListener {
		private double mouseX, mouseY;
		public void mousePressed(MouseEvent event) {
			mouseX = event.getX();
			mouseY = event.getY();
		}
		public void mouseDragged(MouseEvent event) {
			int currMouseX = event.getX();
			int currMouseY = event.getY();
			if(egg.contains(mouseX, mouseY)) { //test su mouseX e mouseY !!!
				egg.x += currMouseX - mouseX;
				egg.y += currMouseY - mouseY;
				mouseX = currMouseX;
				mouseY = currMouseY;
				repaint();
			}
		}
		public void mouseMoved(MouseEvent e) {}
	}
}



Questa la bacheca di Informatica Grafica, corso di laurea specialistica in Ing. Edile-architettura
by FdA
ultima modifica: 02/02/2005 11.30