Ich arbeite in Java mit Swing. Ich habe einen Ball kreiert, der sich an den Seiten des Bildschirms bewegt. Ich möchte die Animation anhalten, wenn ich auf den Rahmen klicke. Ich implementiere MouseListener, aber ohne Erfolg.Wie pausiere ich diesen Ball?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Ball extends JFrame implements MouseListener
{
int x = 20;
int y = 20;
int rad = 20;
boolean temp1 = true;
boolean temp2 = true;
boolean temp3 = true;
Ball()
{
setSize(500, 500);
setVisible(true);
}
public void mouseClicked(MouseEvent me)
{
System.out.println("Hee");
temp3 = false;
}
public void mouseEntered(MouseEvent me){
temp3 = false;
}
public void mouseExited(MouseEvent me){
System.out.println("");
}
public void mouseReleased(MouseEvent me){
System.out.println("");
}
public void mousePressed(MouseEvent me){
System.out.println("");
}
void move()
{
if(x == rad && y == rad)
{
temp1 = temp2 = true;
}
if(x < (getWidth() - rad) && temp1)
{
x = x + 1;
}
if(x == (getWidth() - rad) && y < getHeight() -rad)
{
x = getWidth() - rad;
y = y + 1;
}
if(y == getHeight() - rad && temp2)
{
temp1 = false;
y = getHeight() - rad;
x = x - 1;
}
if(x == rad)
{
temp2 = false;
x = rad;
y = y -1;
}
try{
Thread.sleep(1);
}catch(Exception e)
{
}
}
public void paint(Graphics g)
{
super.paint(g);
g.fillOval(x, y, rad, rad);
}
public static void main(String[] args)
{
Ball b = new Ball();
while(b.temp3)
{
b.move();
b.repaint();
}
}
}
Siehe [Erkennung/Korrektur für das Aufhängen der Nähe Klammer eines Codeblocks] (http: // (meta.stackexchange.com/q/251795/155831) für ein Problem, das ich nicht länger stören konnte. –