MousePressed无法正常工作

时间:2011-08-22 22:51:01

标签: java user-interface mouseevent

我一直在尝试执行此任务,并且为了工作我需要事件mousePressed才能工作,但由于某种原因,它没有响应鼠标。其目的是在按下鼠标时绘制另一个黄色圆圈。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

public class CatchMonster extends JPanel
{
    private int height = 300;
    private int width = 600;
    private final int delay = 4001;

    private ImageIcon image;
    private Timer timer;
    private int x, y, moveX, moveY, xPoint, yPoint;

public CatchMonster() {

    DotListener dot = new DotListener();
    addMouseListener(dot);


    timer = new Timer(delay, new timerListener());
    x = 40;
    y = 40;

    moveX = moveY = 3;
    setPreferredSize(new Dimension(width, height));
    setBackground(Color.black);
    timer.start();

}

public void paintComponent(Graphics g) {
    super.paintComponents(g);
    g.setColor(Color.yellow);
    g.fillOval(x, y, 60, 60);
}

private class timerListener implements ActionListener 
{
    public void actionPerformed(ActionEvent e) {
        Random gn = new Random();
        x = gn.nextInt(width);
        y = gn.nextInt(height);

        repaint();
    }
    }

private class DotListener implements MouseListener
{
    public void mousePressed(MouseEvent event)
    {
        repaint();
    }

    @Override
    public void mouseClicked(MouseEvent event) {


    }

    @Override
    public void mouseEntered(MouseEvent event) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent event) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent event) {
        // TODO Auto-generated method stub

    }


}

}

1 个答案:

答案 0 :(得分:2)

  

我需要事件mousePressed才能工作但由于某种原因它没有响应鼠标。其目的是在按下鼠标时绘制另一个黄色圆圈。

但是它不会绘制另一个圆圈,因为它所做的就是调用repaint(),那怎么会画出新的东西呢?如果你想要它创建另一个圆圈,你必须给它逻辑这样做。例如,如果要绘制多个黄色椭圆,则需要创建Point对象的ArrayList,并在mousePressed方法中将Point添加到该数组列表中。然后在paintComponent方法中,您可以遍历数组列表,为它包含的每个Point绘制椭圆。

此外,您想要更改此内容:

   public void paintComponent(Graphics g) {
      super.paintComponents(g); // this is not the "super" method of paintComponent
      g.setColor(Color.yellow);
      g.fillOval(x, y, 60, 60);
   }

到此:

   public void paintComponent(Graphics g) {
      super.paintComponent(g); // See the difference?
      g.setColor(Color.yellow);
      g.fillOval(x, y, 60, 60);
   }

例如:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class CatchMonster extends JPanel {
   private int height = 300;
   private int width = 600;
   private final int delay = 4001;

   private ImageIcon image;
   private Timer timer;
   private int x, y, moveX, moveY, xPoint, yPoint;
   private List<Point> points = new ArrayList<Point>();

   public CatchMonster() {

      DotListener dot = new DotListener();
      addMouseListener(dot);

      timer = new Timer(delay, new timerListener());
      x = 40;
      y = 40;

      moveX = moveY = 3;
      setPreferredSize(new Dimension(width, height));
      setBackground(Color.black);
      timer.start();

   }

   public void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.setColor(Color.yellow);
      g.fillOval(x, y, 60, 60);

      int radius = 30;
      g.setColor(Color.green);
      for (Point p : points) {
         int x = p.x - radius;
         int y = p.y - radius;
         g.fillOval(x, y, 2 * radius, 2 * radius);
      }
   }

   private class timerListener implements ActionListener {
      public void actionPerformed(ActionEvent e) {
         Random gn = new Random();
         x = gn.nextInt(width);
         y = gn.nextInt(height);

         repaint();
      }
   }

   public static void main(String[] args) {
      JFrame frame = new JFrame("Foo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new CatchMonster());
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   private class DotListener extends MouseAdapter {
      public void mousePressed(MouseEvent event) {
         points.add(event.getPoint());
         repaint();
      }

   }
}