import javax.swing.*; import java.awt.*; import java.awt.event.*; public class DrawLine extends JApplet implements MouseListener,MouseMotionListener{ int x1,y1,x2,y2; // int screenx,screeny; // Dimension screen; Stroke sk=new BasicStroke(2.0f); public void init(){ // screen=Toolkit.getDefaultToolkit().getScreenSize (); // screenx=(int)screen.getWidth(); // screeny=(int)screen.getHeight(); // setBounds((screenx-400)/2, (screeny-700)/2, 400, 700); addMouseListener(this); addMouseMotionListener(this); } public void paint(Graphics g){ Graphics2D gg=(Graphics2D)g; gg.setStroke(sk); gg.drawLine(x1, y1, x2, y2); } @Override public void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } public void mousePressed(MouseEvent arg0) { //x1=arg0.getX(); //y1=arg0.getY(); } @Override public void mouseReleased(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseDragged(MouseEvent arg0) { x2=arg0.getX(); y2=arg0.getY(); x1=x2; y1=y2; repaint(); } @Override public void mouseMoved(MouseEvent arg0) { Point p=new Point(x2,y2); System.out.println(p); //System.out.println("Y"+this.y2); } }
时间: 2024-10-20 22:36:24