2017-04-15 6 views
1

私はスイングでjavaで働いています。私は画面の両側を動くボールを作りました。私が望むのは、フレームをクリックするとアニマオンを一時停止することです。私はimplementin MouseListenerだが、役に立たない。このボールを一時停止するにはどうすればよいですか?

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(); 
    } 
} 

}

+0

参照[コードブロックの吊り閉じ括弧のための検出/修正](のhttp://をmeta.stackexchange.com/q/251795/155831)私はもはや気にすることができない問題を解決する。 –

答えて

4

コードを持つ2つの根本的な問題がある:

  1. main(..)方法でループがイベントディスパッチスレッドをブロックしているが。
  2. MouseListenerは決してフレームに追加されません。

コードはまだそれが変更しましたが、ここでは、両方の固定これらの問題あるべきであるかの方法があります。

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); 
     // the correct way to animate a Swing GUI 
     ActionListener animationListener = new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       if (temp3) { 
        move(); 
        repaint(); 
       } 
      } 
     }; 
     Timer timer = new Timer(20, animationListener); 
     timer.start(); 
     // add the listener to the frame! 
     this.addMouseListener(this); 
    } 

    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(); 
    } 
} 
+0

ありがとう!魅力のように動作します! –

関連する問題