2016-03-20 8 views
0

私は、プログラムが始まると微笑む顔を作る必要がありますが、アニメーションの笑顔は、実行時にウィンドウを伸ばさない限り移動せず、今は何らかの理由で完全な円として表示されますMy work so far vs taskアニメーションin java

import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
import javax.swing.*; 
import javax.swing.Timer; 

public class FaceAnimation extends JFrame { 
private StillClock face = new StillClock(); 

public FaceAnimation() { 
add(face); 


} 
private class TimerListener implements ActionListener { 
@Override /** Handle the action event */ 
public void actionPerformed(ActionEvent e) { 
} 
} 

public static void main(String[] args) { 
JFrame frame = new FaceAnimation(); 
frame.setTitle("FaceAnimation"); 
frame.setSize(400, 400); 
frame.setLocationRelativeTo(null); // Center the frame 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.setVisible(true); 
} 
} 

class StillClock extends JPanel { 
private int mouthOpening; 





@Override /** Draw the face */ 
protected void paintComponent(Graphics g) { 
super.paintComponent(g); 

// Initialize face parameters 

int xCenter = getWidth()/2; 
int yCenter = getHeight()/2; 
int radius = (int)(Math.min(getWidth(), getHeight())* 0.4); 

int x = xCenter - radius; 
int y = yCenter - radius; 


g.setColor (Color.BLACK); 
g.drawOval(x, y, 2 * radius, 2 * radius); 

g.setColor (Color.BLACK); 
g.fillOval (xCenter - radius * 3/5, yCenter - radius* 2/5, 20, 20); 

g.setColor(Color.BLACK); 
g.fillOval(xCenter + radius * 2/5, yCenter - radius*2/5, 20, 20); 

g.setColor(Color.RED); 
mouthOpening = mouthOpening + 10; 
g.drawArc(xCenter - 3*radius/5, yCenter - radius/2, 6*radius/5,  radius*6/5, 270-mouthOpening/2, mouthOpening); 

} 
@Override 
public Dimension getPreferredSize() { 
    return new Dimension(800, 800); 
} 
} 

答えて

0
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.Timer; 

public class FaceAnimation extends JFrame { 
    private StillClock face = new StillClock(); 

    public FaceAnimation() { 
     add(face); 
    } 

    public static void main(String[] args) { 
     JFrame frame = new FaceAnimation(); 
     frame.setTitle("FaceAnimation"); 
     frame.setSize(400, 400); 
     frame.setLocationRelativeTo(null); // Center the frame 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 
} 

class StillClock extends JPanel { 

    private int mouthOpening; 
    private Timer timer; 

    public StillClock() { 
     timer = new Timer(25, new TimerListener()); 
     timer.start(); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 

     int xCenter = getWidth()/2; 
     int yCenter = getHeight()/2; 
     int radius = (int) (Math.min(getWidth(), getHeight()) * 0.4); 

     int x = xCenter - radius; 
     int y = yCenter - radius; 

     g.setColor(Color.BLACK); 
     g.drawOval(x, y, 2 * radius, 2 * radius); 

     g.setColor(Color.BLACK); 
     g.fillOval(xCenter - radius * 3/5, yCenter - radius * 2/5, 20, 20); 

     g.setColor(Color.BLACK); 
     g.fillOval(xCenter + radius * 2/5, yCenter - radius * 2/5, 20, 20); 

     g.setColor(Color.RED); 
     g.drawArc(xCenter - 3 * radius/5, yCenter - radius/2, 6 * radius/5, radius * 6/5, 270 - mouthOpening/2, mouthOpening); 
     repaint(); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(800, 800); 
    } 

    private class TimerListener implements ActionListener { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      if (mouthOpening > 180) { 
      mouthOpening = 0; 
     } else { 
      mouthOpening = mouthOpening + 10; 
     } 
     } 
    } 
} 
0

あなたのコードの問題はpaintComponentがループ内で呼び出されていないことでした。このメソッドは、ダイアログが最初に描画されたとき、およびサイズ変更などの特定のイベントでのみ呼び出されます。 repaint()という方法で手動で行うこともできます。これはpaintComponentとなります。

次の例では、updateMouthメソッドとMouthUpdateTimeのインスタンスを使用して定期的にダイアログを更新するタイマーを使用します。 updateMouthは、repaintを使用して、お客様のmouthOpening値とUIを更新します。

タイマは、MouthUpdateTimerのインスタンスで作成されます。タイマーは指定されたインスタンスのactionPerformedUPDATE_INTERVALLms(ここでは50ms)ごとに呼び出し、updateMouthを使用して口をアニメートします。

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.Timer; 

public class FaceAnimation extends JFrame { 

    private StillClock face = new StillClock(); 

    public FaceAnimation() { 
     add(face); 
    } 

private class TimerListener implements ActionListener { 

    @Override 
    /** Handle the action event */ 
    public void actionPerformed(ActionEvent e) {} 
} 

public static void main(String[] args) { 
    JFrame frame = new FaceAnimation(); 
    frame.setTitle("FaceAnimation"); 
    frame.setSize(400, 400); 
    frame.setLocationRelativeTo(null); // Center the frame 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 
} 
} 


class StillClock extends JPanel { 

private int mouthOpening = 30; // start with 30° 
private int UPDATE_INTERVAL = 50; //wait 50 ms between each update 

public StillClock() { 
    new Timer(UPDATE_INTERVAL, new MouthUpdateTimer()).start(); 

} 

@Override 
/** Draw the face */ 
protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    // Initialize face parameters 
    int xCenter = getWidth()/2; 
    int yCenter = getHeight()/2; 
    int radius = (int) (Math.min(getWidth(), getHeight()) * 0.4); 

    int x = xCenter - radius; 
    int y = yCenter - radius; 


    g.setColor(Color.BLACK); 
    g.drawOval(x, y, 2 * radius, 2 * radius); 

    g.setColor(Color.BLACK); 
    g.fillOval(xCenter - radius * 3/5, yCenter - radius * 2/5, 20, 20); 

    g.setColor(Color.BLACK); 
    g.fillOval(xCenter + radius * 2/5, yCenter - radius * 2/5, 20, 20); 

    g.setColor(Color.RED); 

    g.drawArc(xCenter - 3 * radius/5, yCenter - radius/2, 6 * radius/5, radius * 6/5, 
      270 - mouthOpening/2, mouthOpening); 
} 

/** 
* Updates mouthOpening and updates the UI. 
*/ 
private void updateMouth(){ 
    if(mouthOpening > 180){ // redraw after 180° 
     mouthOpening = 30; 
    } else { 
     mouthOpening = mouthOpening + 1; 
    } 
    repaint(); // redraw the dialog 
} 

@Override 
public Dimension getPreferredSize() { 
    return new Dimension(800, 800); 
} 

private class MouthUpdateTimer implements ActionListener { 

    @Override 
    public void actionPerformed(ActionEvent e){ 
      updateMouth(); // calls updateMouth 
    } 
} 
}