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

class EggFrame extends JFrame {
	public static void main(String args[]) {
		System.out.println("Starting EggFrame...");
		EggFrame mainFrame = new EggFrame();
		mainFrame.setSize(200, 200);
		mainFrame.setTitle("EggFrame");
		mainFrame.show();
	}
	
	public EggFrame() {
		addWindowListener(new MyWindowListener());		
		getContentPane().add(new Pannello());
	}

	private class MyWindowListener extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			System.exit(0);
		}
	}
}

class Pannello extends JPanel {
	public Pannello() {
		egg = new Ellipse2D.Double(0, 0, EGG_WIDTH, EGG_HEIGHT);
		
		contorno = new Rectangle2D.Double();
		
		MyMouseInputListener listener = new MyMouseInputListener();
		addMouseListener(listener);
		addMouseMotionListener(listener);
	}
	
	public void paintComponent(Graphics g) {	
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D)g;
		int w = this.getWidth();
		int h = this.getHeight();
		contorno.setFrame(0, 0, w-1, h-1);
		g2.draw(contorno);
		g2.fill(egg);
	}

	private Ellipse2D.Double egg;
	private static final double EGG_WIDTH = 20, EGG_HEIGHT = 35;
	
	private Rectangle2D.Double contorno;
	
	private class MyMouseInputListener 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