java 鼠标事件

仙境源地 发布于 2012/09/20 22:10
阅读 1K+
收藏 0

package testv1ch08;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;
import java.util.ArrayList;

import javax.swing.JFrame;

public class MouseTest {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  MouseJFrame mouseJFrame = new MouseJFrame();
  mouseJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  mouseJFrame.setSize(500, 500);
  mouseJFrame.setVisible(true);

 }

}

class MouseJFrame extends JFrame {
 MouseJFrame() {
  setTitle("光标的移动与变化");
  addMouseListener(new Mouse());
  addMouseMotionListener(new MouseMotion());
 }

 public void paintComponent(Graphics g) {
  Graphics2D g2 = (Graphics2D) g;
  for (Rectangle2D r : squares) {
   g2.draw(r);
  }
 }

 public boolean find(Point p) {
  if (p == null)
   return false;
  Point2D p2=(Point2D)p;
  for (Rectangle2D r : squares) {
   if (r.contains(p2))
    return true;
  }
  return false;
 }

 public void add(Point p) {
  double x = p.getX();
  double y = p.getY();
  current = new Rectangle2D.Double(x - size / 2, y - size / 2, size, size);
  squares.add(current);
  repaint();
 }

 private ArrayList<Rectangle2D> squares=null;
 private Rectangle2D current=null;
 private static final double size = 10;

 class Mouse extends MouseAdapter {

  @Override
  public void mouseClicked(MouseEvent e) {
   // TODO Auto-generated method stub
   super.mouseClicked(e);

  }

  @Override
 public void mousePressed(MouseEvent e) {
  // TODO Auto-generated method stub
  if(find(e.getPoint())==false)
   add(e.getPoint());

 }
 }

 class MouseMotion extends MouseMotionAdapter {
  @Override
  public void mouseDragged(MouseEvent e) {
   // TODO Auto-generated method stub
   super.mouseDragged(e);
  }

  @Override
  public void mouseMoved(MouseEvent e) {
   // TODO Auto-generated method stub
   super.mouseMoved(e);
  }

 }
}

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
 at testv1ch08.MouseJFrame.find(MouseTest.java:50)
 at testv1ch08.MouseJFrame$Mouse.mousePressed(MouseTest.java:81)
 at java.awt.Component.processMouseEvent(Unknown Source)
 at java.awt.Component.processEvent(Unknown Source)
 at java.awt.Container.processEvent(Unknown Source)
 at java.awt.Window.processEvent(Unknown Source)
 at java.awt.Component.dispatchEventImpl(Unknown Source)
 at java.awt.Container.dispatchEventImpl(Unknown Source)
 at java.awt.Window.dispatchEventImpl(Unknown Source)
 at java.awt.Component.dispatchEvent(Unknown Source)
 at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
 at java.awt.EventQueue.access$200(Unknown Source)
 at java.awt.EventQueue$3.run(Unknown Source)
 at java.awt.EventQueue$3.run(Unknown Source)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
 at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
 at java.awt.EventQueue$4.run(Unknown Source)
 at java.awt.EventQueue$4.run(Unknown Source)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
 at java.awt.EventQueue.dispatchEvent(Unknown Source)
 at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.run(Unknown Source)
求教

加载中
0
Jeky
Jeky
squares没初始化
仙境源地
怎么初始化啊?
0
Jeky
Jeky
private ArrayList<Rectangle2D> squares=null;
改成:
private ArrayList<Rectangle2D> squares= new ArrayList<Rectangle2D>();
仙境源地
那为何赋值为null不行呢?
0
Jeky
Jeky
表示...很难回答 请完整地看Java语法书
0
673
673

比如:

 Cup(装水的杯子)  c=null;//这个时候c是空杯子;(另外,赋空值是多此一举,jvm默认就是c=null)

 c.paur;//想倒水?空杯子倒不出水,亲。

 

-----------

其实,这个时候的c,只算得上脑子里想的虚杯子,别说倒说,就连往里面装水都装不了。

 

明白?

还是建议看JAVA基础。

0
javacc
javacc

这个论坛里有人分享过鼠标事情的,你搜搜看,我只是看到过具体地址忘了

返回顶部
顶部