日期:2014-05-20 浏览次数:20680 次
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Line2D;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Line2DGainedDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MainFrame();
}
});
}
}
class MainFrame extends JFrame{
private static final long serialVersionUID = -9118470974041744156L;
private Line2D line2D = new Line2D.Double(100, 100, 200, 100);
public MainFrame(){
initialized();
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void initialized() {
JPanel panel;
add(panel = new JPanel(){
private static final long serialVersionUID = 1L;
@Override
public void paint(Graphics g) {
super.paint(g);
((Graphics2D)g).draw(line2D);
}
});
panel.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
super.mouseMoved(e);
System.out.println(e.getPoint());
if(line2D.intersectsLine(new Line2D.Double(e.getPoint(), e.getPoint()))){
JOptionPane.showMessageDialog(MainFrame.this, "你选择了线条");
}
}
});
}
}