JAVA画图

 
package gui;

import java.awt.*;
import java.awt.event.*;

public class PaintFrame extends Frame {

 private static final long serialVersionUID = 1L;

 private int _x, _y, x, y;
 private boolean bps = false;

 public void paint(Graphics g) {
  if (bps) {
   Color c = g.getColor();
   g.setColor(Color.RED);
   g.drawLine(_x, _y, x, y);
   g.setColor(c);
  }
 }

 public PaintFrame() {
  this.setBounds(230, 100, 600, 500);
  this.setTitle("画图");
  this.setResizable(false);
  this.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e) {
    System.exit(0);
   }
  });

  this.addMouseListener(new MouseAdapter() {
   public void mousePressed(MouseEvent e) {
    _x = e.getX();
    _y = e.getY();
   }

   public void mouseReleased(MouseEvent e) {
    bps = false;
   }
  });

  this.addMouseMotionListener(new MouseMotionAdapter() {
   public void mouseMoved(MouseEvent arg0) {
    bps = true;

   }

   public void mouseDragged(MouseEvent e) {
    x = e.getX();
    y = e.getY();
    PaintFrame.this.paint(PaintFrame.this.getGraphics());
    _x = x;
    _y = y;
   }
  });

  this.setVisible(true);
 }

 public static void main(String[] args) {
  new PaintFrame();

 }

}

posted @ 2011-10-09 13:36  jason.android  阅读(158)  评论(0)    收藏  举报