Al click del mouse si memorizzino le cordinate per tracciare una retta fino ad un altro punto fatto con un altrettanto click del mouse
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Mouse extends JPanel implements MouseListener {
private int x1;
private int y1;
private int x2;
private int y2;
private int conta =0;
public Mouse(){
addMouseListener(this);
}
public void mouseClicked(MouseEvent e){
int x= e.getX();
int y=e.getY();
conta++;
if(conta==1){
x1=x;
y1=y;
}
if(conta==2){
x2=x;
y2=y;
conta=0;
repaint();
}
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void paintComponent(Graphics g){
g.drawLine(x1, y1, x2, y2);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestaMouse {
public static void main(String[]args){
JFrame f1 = new JFrame("traccia retta");
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setSize(400,400);
Mouse m1 = new Mouse();
f1.add(m1,BorderLayout.CENTER);
f1.setVisible(true);
}
}
Nessun commento:
Posta un commento