gpt4 book ai didi

java - 鼠标按下不工作

转载 作者:行者123 更新时间:2023-11-30 09:46:25 24 4
gpt4 key购买 nike

我一直在尝试完成这项任务,为了让它工作,我需要事件 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

}


}

最佳答案

I need the event mousePressed to work but for some reason it's not responding to the mouse. Its purpose is to paint another yellow circle when the mouse is pressed.

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

另外,你想改变这个:

   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 - 鼠标按下不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7154534/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com