import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TracciaLinea {
private JFrame f=new JFrame ("tracciamento linee rette");
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){}
}
}
1 commento:
Molto bene
Posta un commento