点击它后,有什么方法可以让椭圆消失。 在这种情况下,我无法理解鼠标侦听器的位置和方式,因为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);
}
}
}
答案 0 :(得分:1)
Ellipse2D
。ellipse.contains(x,y)
答案 1 :(得分:1)
根据你的职能:
@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);
}
此行g2d.fillOval(x, y, 50, 50);
实际上是在每次重绘时绘制圆圈。
如果你的mouseListener在圆圈中点击(使用安德鲁的答案),你可能想要使用一些标志来查看它是否被点击。
if (!ovalClicked)
{
g2d.setColor(Color.red);
g2d.fillOval(x, y, 50, 50);
}
(最好使用paintComponent()
而不是paint()
)