我一直在尝试执行此任务,为了使其正常工作,我需要事件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

    }


}


}

最佳答案

我需要事件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();
      }

   }
}

关于java - MousePressed不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7154534/

10-08 22:33