[英]mouseClicked event on a shape continually being repainted on a canvas
I have a circle being repainted continually to show animation. 我有一个圆圈不断被粉刷以显示动画。 I would like to have the circle flash different colors if clicked.
如果要单击,我希望圆圈闪烁不同的颜色。 When I tried implementing MouseListener to get a mouseClicked event, it did not work.
当我尝试实现MouseListener来获取mouseClicked事件时,此方法不起作用。 I believe that is due to the constant repainting.
我相信这是由于不断的重新粉刷。 Is there another way to have this circle bounce around and still catch a mouse click?
还有另一种方法可以使这个圆圈跳动并仍然可以单击鼠标吗? I added a KeyEvent to test, and it worked fine.
我添加了一个KeyEvent进行测试,并且工作正常。 There is no "main" as this was called from another program.
没有“ main”,因为这是从另一个程序调用的。
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Random;
import java.util.Timer;
public class Catch extends Canvas {
int xCor, yCor, xMove, yMove;
Color currentColor;
Random ranNumber;
boolean flashing = false;
public Catch() {
enableEvents(java.awt.AWTEvent.KEY_EVENT_MASK);
requestFocus();
xCor = 500;
yCor = 350;
xMove = 5;
yMove = 5;
currentColor = Color.black;
ranNumber = new Random();
Timer t = new Timer(true);
t.schedule(new java.util.TimerTask() {
public void run() {
animate();
repaint();
}
}, 10, 10);
}
public void paint(Graphics g) {
g.setColor(currentColor);
g.fillOval(xCor, yCor, 20, 20);
}
public void processKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_PRESSED) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
flashing = !flashing;
}
}
}
public void animate() {
xCor += xMove;
yCor += yMove;
// and bounce if we hit a wall
if (xCor < 0 || xCor + 20 > 1000) {
xMove = -xMove;
}
if (yCor < 0 || yCor + 20 > 700) {
yMove = -yMove;
}
if (flashing) {
int r = ranNumber.nextInt(256);
int g = ranNumber.nextInt(256);
int b = ranNumber.nextInt(256);
currentColor = new Color(r, g, b);
}
}
public boolean isFocusable() {
return true;
}
}
Your approach is a little out of date, we don't tend to use enableEvents
any more, but instead make use of a number of different "observers" which provide notification about certain events. 您的方法有些过时了,我们不再倾向于使用
enableEvents
,而是使用许多提供某些事件通知的“观察者”。
I'd start by having a look at Painting in AWT and Swing and Performing Custom Painting and How to Write a Mouse Listener 我首先看一下AWT和Swing中的 绘画以及执行自定义绘画以及如何编写鼠标侦听器。
I'd also avoid using KeyListener
and instead use the Key Bindings API which was designed to overcome many of the shortcommings of KeyListener
. 我还将避免使用
KeyListener
,而应使用旨在克服KeyListener
许多缺点的Key Bindings API。
While cutting edge would be to use JavaFX, if you have a knowledge of AWT, then stepping up to Swing would simpler, for example: 虽然最前沿的是使用JavaFX,但是如果您了解AWT,那么使用Swing会更简单,例如:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Ball ball;
public TestPane() {
ball = new Ball();
Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ball.update(getSize());
repaint();
}
});
timer.start();
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
ball.setHighlighted(ball.wasClicked(e.getPoint()));
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
ball.paint(g2d);
g2d.dispose();
}
}
public class Ball {
private int radius = 10;
private int xDelta, yDelta;
private Ellipse2D shape = new Ellipse2D.Double(0, 0, radius * 2, radius * 2);
private boolean highlighted;
private int cycleCount;
public Ball() {
Random rnd = new Random();
xDelta = rnd.nextInt(3) + 1;
yDelta = rnd.nextInt(3) + 1;
}
public void update(Dimension bounds) {
Rectangle shapeBounds = shape.getBounds();
shapeBounds.x += xDelta;
shapeBounds.y += yDelta;
if (shapeBounds.x + shapeBounds.width > bounds.width) {
shapeBounds.x = bounds.width - shapeBounds.width;
xDelta *= -1;
} else if (shapeBounds.x < 0) {
shapeBounds.x = 0;
xDelta *= -1;
}
if (shapeBounds.y + shapeBounds.height > bounds.height) {
shapeBounds.y = bounds.height - shapeBounds.height;
yDelta *= -1;
} else if (shapeBounds.y < 0) {
shapeBounds.y = 0;
yDelta *= -1;
}
shape.setFrame(shapeBounds);
if (highlighted) {
cycleCount++;
if (cycleCount > 12) {
highlighted = false;
}
}
}
public boolean wasClicked(Point p) {
return shape.contains(p);
}
public void setHighlighted(boolean value) {
highlighted = value;
cycleCount = 0;
}
public void paint(Graphics2D g) {
if (highlighted) {
g.setColor(Color.RED);
} else {
g.setColor(Color.BLUE);
}
g.fill(shape);
}
}
}
You should also have a look at How to use Swing Timers 您还应该看看如何使用Swing计时器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.