2016-03-20 19 views
0

Program başladığında gülümseyen bir yüz yapmalıyım, ancak pencerenin çalıştığı zaman pencereyi genişletmedikçe animasyondaki gülümseme hareket etmiyor, şu anda tam bir daire olarak görünüyor. My work so far vs task.Java'da animasyon

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

cevap

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

sizin koduyla sorun paintComponent bir döngüde denilen olmadığını, oldu. Yöntem sadece diyalog ilk kez ve yeniden boyutlandırma gibi belirli olaylarda çizildiğinde çağrılır. Ayrıca, paintComponent numaralı telefonu arayacak olan repaint() yöntemiyle de manuel olarak yapabilirsiniz.

Aşağıdaki örnekte, iletişim kutusunu updateMouth Yöntem ve MouthUpdateTime örneğini kullanarak düzenli olarak güncelleyen bir Zamanlayıcı kullanılır. updateMouth, mouthOpening değerini ve UI'yi repaint kullanarak güncelleştirir.

Zamanlayıcı, MouthUpdateTimer örneğiyle oluşturulur. Zamanlayıcı, verilen örneğin her UPDATE_INTERVALL ms ms (burada 50 msn) olan actionPerformed'u çağırır ve daha sonra updateMouth ağzınızı canlandırmak için kullanır.

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 
    } 
} 
} 
İlgili konular