martedì 13 gennaio 2009

Esercizio svolto in classe Martedì 13/01/2009

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TestaMouseInput {
private JFrame f=new JFrame ("Testa input mouse");
private Pannello p=new Pannello();
private Point sPoint=new Point ();
private Point ePoint=new Point ();
public TracciaLinea(){
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500,500);
f.add(p);
f.addMouseListener(new Linea());
f.addMouseMotionListener(new Linea1());
f.setVisible(true);
}
public class Pannello extends JPanel{
public void paintComponent(Graphics g){
g.drawLine(sPoint.x,sPoint.y,ePoint.x,ePoint.y);
}
}
public static void main(String[] args){
TracciaLinea t=new TracciaLinea();
}
public class Linea implements MouseListener{
public void mousePressed(MouseEvent e){
sPoint.x=e.getX()-9;
sPoint.y=e.getY()-34;
}
public void mouseReleased(MouseEvent e){
ePoint.x=e.getX()-9;
ePoint.y=e.getY()-34;
f.repaint();
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
public class Linea1 implements MouseMotionListener{
public void mouseDragged(MouseEvent e){
ePoint.x=e.getX()-9;
ePoint.y=e.getY()-34;
f.repaint();
}
public void mouseMoved(MouseEvent e){}
}
}


Al clic del mouse il programma darà le cordinate del punto cliccato.

Nessun commento: