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

class EggFrame2 extends JFrame {
	public static void main(String args[]) {
		System.out.println("Starting EggFrame...");
		EggFrame2 mainFrame = new EggFrame2();
		mainFrame.setSize(200, 200);
		mainFrame.setTitle("EggFrame2");
		mainFrame.show();
	}
	
	public EggFrame2() {
		addWindowListener(new MyWindowListener());		
		
		eggPanel=new Pannello();
		
		JPanel	buttonPanel=new JPanel();
		buttonPanel.add(upButton=new JButton("Up"));
		buttonPanel.add(downButton=new JButton("Down"));
		buttonPanel.add(leftButton=new JButton("Left"));
		buttonPanel.add(rightButton=new JButton("Right"));
				
		Container  contentPane=getContentPane();
		contentPane.add(eggPanel,"Center");
		contentPane.add(buttonPanel,"South");
		
		upButton.addActionListener(new ButtonListener());
		downButton.addActionListener(new ButtonListener());
		leftButton.addActionListener(new ButtonListener());
		rightButton.addActionListener(new ButtonListener());
	}

	private	Pannello eggPanel;
	private JButton	upButton, downButton, leftButton, rightButton;
	private final int DELTA=5;
	
	private class ButtonListener implements ActionListener	{
		public void actionPerformed(ActionEvent event)	{
			Object source=event.getSource();
			if (source==downButton)
				eggPanel.moveEgg(0,DELTA);
			if (source==upButton)
				eggPanel.moveEgg(0,-DELTA);
			if (source==leftButton)
				eggPanel.moveEgg(-DELTA,0);
			if (source==rightButton)
				eggPanel.moveEgg(DELTA,0);				
		}
	}

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

class Pannello extends JPanel {
	public Pannello() {
		egg = new Ellipse2D.Double(5, 5, 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();
		
		Color defaultCol=g2.getColor();
		g2.setColor(Color.white);
		Rectangle2D.Double sfondo=new Rectangle2D.Double(0,0,w-1,h-1);
		g2.fill(sfondo);
		g2.setColor(defaultCol);
		
		contorno.setFrame(0, 0, w-1, h-1);
		g2.draw(contorno);
		g2.fill(egg);
		
		Rectangle2D.Double	bordo=new Rectangle2D.Double(egg.x, egg.y, egg.width,egg.height);
		defaultCol=g2.getColor();
		g2.setColor(colBordo);
		g2.draw(bordo);
		g2.setColor(defaultCol);
	}
	
	public void moveEgg(int dx,int dy)	{
		egg.setFrame(egg.x+dx,egg.y+dy,egg.width,egg.height);
		repaint();
	}

	private Ellipse2D.Double egg;
	private final static double EGG_WIDTH = 20, EGG_HEIGHT = 35;
	private final static double EPS=2;
	private Color colBordo=Color.white;	//Rettangolo che contorna l'uovo evidenziato solo
										//quando ilmouse ci passa sopra
	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)) { 
				egg.x += currMouseX - mouseX;
				egg.y += currMouseY - mouseY;
				mouseX = currMouseX;
				mouseY = currMouseY;
				repaint();
			}
			if((mouseX>=egg.x+egg.width-EPS)&&(mouseX<=egg.x+egg.width+EPS)&&
				(mouseY>=egg.y+egg.height-EPS)&&(mouseY<=egg.y+egg.height+EPS))	{
				egg.width += currMouseX - mouseX;
				egg.height += currMouseY - mouseY;
				mouseX = currMouseX;
				mouseY = currMouseY;
				repaint();	
			}
			if((mouseX>=egg.x-EPS)&&(mouseX<=egg.x+EPS)&&
				(mouseY>=egg.y-EPS)&&(mouseY<=egg.y+EPS))	{
				egg.width -= currMouseX - mouseX;
				egg.height -= currMouseY - mouseY;
				egg.x += currMouseX - mouseX;
				egg.y += currMouseY - mouseY;
				mouseX = currMouseX;
				mouseY = currMouseY;
				repaint();	
			}
			if((mouseX>=egg.x+egg.width-EPS)&&(mouseX<=egg.x+egg.width+EPS)&&
				(mouseY>=egg.y-EPS)&&(mouseY<=egg.y+EPS))	{
				egg.width += currMouseX - mouseX;
				egg.height -= currMouseY - mouseY;
				egg.y += currMouseY - mouseY;
				mouseX = currMouseX;
				mouseY = currMouseY;
				repaint();	
			}
			if((mouseX>=egg.x-EPS)&&(mouseX<=egg.x+EPS)&&
				(mouseY>=egg.y+egg.height-EPS)&&(mouseY<=egg.y+egg.height+EPS))	{
				egg.width -= currMouseX - mouseX;
				egg.height += currMouseY - mouseY;
				egg.x += currMouseX - mouseX;
				mouseX = currMouseX;
				mouseY = currMouseY;
				repaint();	
			}
		}
		public void mouseMoved(MouseEvent event) {
			int currMouseX = event.getX();
			int currMouseY = event.getY();
			if ((currMouseX>=egg.x)&&(currMouseX<=egg.x+egg.width)&&
				(currMouseY>=egg.y)&&(currMouseY<=egg.y+egg.height))
				colBordo=Color.red;
			else
				colBordo=Color.white;
			repaint();				
			}
	}	
}

 


Bacheca di Informatica Grafica

Forum studenti:

by FdA
ultima modifica: 09/02/2004 13.23