[英]How to pause the movement of the circle upon clicking the screen?
当我在GUI中单击时,我想暂停圆的运动。 当前它从右移到左,可以拖动,这不是我想要的。 提前致谢!
第一类创建圆,第二类绘制圆,第三类是main方法。
/*
* This program creates an animation for a circle.
*/
package circle.animation;
import java.awt.*;
/*
* @author talhaiqbal18
*/
public class CircleAnimation
{
private int centerX, centerY, radius;
private Color color;
private int direction, speed;
private boolean filled;
public CircleAnimation(int x, int y, int r, Color c) {
centerX = x;
centerY = y;
radius = r;
color = c;
direction = 0;
speed = 0;
filled = false;
}
public void draw(Graphics g) {
Color oldColor = g.getColor();
g.setColor(color);
if (filled)
g.fillOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
else
g.fillOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
g.setColor(oldColor);
}
public void fill(Graphics g) {
Color oldColor = g.getColor();
g.setColor(color);
g.fillOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
g.setColor(oldColor);
}
public boolean containsPoint(int x, int y) {
int xSquared = (x - centerX) * (x - centerX);
int ySquared = (y - centerY) * (y - centerY);
int radiusSquared = radius * radius;
return xSquared + ySquared - radiusSquared <= 0;
}
public void move(int xAmount, int yAmount) {
centerX = centerX + xAmount;
centerY = centerY + yAmount;
}
public int getRadius() {
return radius;
}
public int getX() {
return centerX;
}
public int getY() {
return centerY;
}
public void setSpeed(int s) {
speed = s;
}
public void setDirection(int d) {
direction = d % 360;
}
public void turn(int degrees) {
direction = (direction + degrees) % 360;
}
public void move() {
move((int)(speed * Math.cos(Math.toRadians(direction))),
(int)(speed * Math.sin(Math.toRadians(direction))));
}
public void setFilled(boolean b) {
filled = b;
}
}
/*
* This is the color panel class for the circle animation project.
*/
package circle.animation;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/*
* @author talhaiqbal18
*/
public class ColorPanel extends JPanel
{
private CircleAnimation circle;
private javax.swing.Timer timer;
private CircleAnimation selectedCircle;
private int x, y;
public ColorPanel(Color backColor, int width, int height) {
setBackground(backColor);
setPreferredSize(new Dimension(width, height));
circle = new CircleAnimation(350, height / 2, 350, Color.blue);
circle.setDirection(180);
circle.setSpeed(6);
timer = new javax.swing.Timer(1, new MoveListener());
timer.start();
addMouseListener(new PanelListener());
addMouseMotionListener(new PanelMotionListener());
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
circle.fill(g);
}
private class MoveListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int x = circle.getX();
int radius = circle.getRadius();
int width = getWidth();
if (x - radius <= 0 || x + radius >= width) {
circle.turn(180);
}
circle.move();
repaint();
}
}
private class PanelListener extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
x = e.getX();
y = e.getY();
if (circle.containsPoint(x, y))
selectedCircle = circle;
}
public void mouseReleased(MouseEvent e)
{
Object selectedcircle = null;
}
}
private class PanelMotionListener extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e)
{
int newX = e.getX();
int newY = e.getY();
int dx = newX - x;
int dy = newY - y;
if (selectedCircle != null) {
selectedCircle.move(dx,dy);
x = newX;
y = newY;
repaint(); }
}
}
}
/*
* This is the main method which will implement the actions of the previous classes.
*/
package circle.animation;
import java.awt.*;
import javax.swing.JFrame;
/*
* @author talhaiqbal18
*/
public class MainMethod
{
public static void main(String[] args) throws Exception {
JFrame theGUI = new JFrame();
theGUI.setTitle("Circle Animation");
theGUI.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
ColorPanel panel = new ColorPanel(Color.white, 100, 100);
Container pane = theGUI.getContentPane();
pane.add(panel);
theGUI.setVisible(true);
}
}
将以下方法mouseClicked
添加到PanelListener:
private class PanelListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
if (circle.containsPoint(x, y)) selectedCircle = circle;
}
@Override
public void mouseReleased(MouseEvent e) {
// nothing here
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked");
if (timer.isRunning()) {
timer.stop();
} else {
timer.start();
}
}
}
顺便说一句,我不得不更改您的代码中的某些内容才能使其运行-在x = circle.getX();
之前删除了“ int” x = circle.getX();
私有类MoveListener实现ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
x = circle.getX();
int radius = circle.getRadius();
int width = getWidth();
if (x - radius <= 0 || x + radius >= width) {
circle.turn(180);
}
circle.move();
repaint();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.