有什么办法可以让椭圆形在点击后消失。 我无法理解在这种情况下在哪里以及如何使用鼠标监听器,因为 While 循环始终运行,因此 repaint() 也是如此。 提前致谢。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Game extends JPanel {
int x,y;
int offset=400;
int i; //j;
private void moveBall() {
double degrees=(double) i;
double radians=Math.toRadians(degrees);
double Sinu=Math.sin(radians);
double Sinu200=Math.sin(radians)*300;
int SinuInt=(int) Sinu200;
y=offset+SinuInt;
double Cos=Math.cos(radians);
double Cos200=Math.cos(radians)*300;
int CosInt=(int) Cos200;
x=offset+CosInt;
i++; // j--;
if (i==360) i=0;
}
private int sin(double radians) {
// TODO Auto-generated method stub
return 0;
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.red);
g2d.fillOval(x, y, 50, 50);
}
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Rolling album");
Game game = new Game();
game.i=90;
frame.add(game);
frame.setSize(1100, 1000);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
while (true) {
game.moveBall();
game.repaint();
Thread.sleep(30);
}
}
}
最佳答案
- 创建通告
Ellipse2D
. - 测试
ellipse.contains(x,y)
关于java - 运动中图形对象的鼠标监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20748763/