import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Test extends JApplet { JPanel panel = new JPanel(); int x1, y1, x2, y2; Graphics gra; public void init() { this.getContentPane().add(panel); gra = this.getGraphics(); panel.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent arg) { x1 = arg.getPoint().x; y1 = arg.getPoint().y; } } ); panel.addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent arg) { x2 = arg.getPoint().x; y2 = arg.getPoint().y; gra.drawLine(x1, y1, x2, y2); x1 = x2; y1 = y2; } } ); } }
时间: 2024-10-12 13:50:57