私は、プログラムが始まると微笑む顔を作る必要がありますが、アニメーションの笑顔は、実行時にウィンドウを伸ばさない限り移動せず、今は何らかの理由で完全な円として表示されます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);
}
}