JAVA实现一个皮卡丘动画代码
时间: 2023-05-21 19:05:13 浏览: 537
Python皮卡丘代码.rar
很高兴回答您的问题!以下是JAVA实现一个皮卡丘动画的代码:
```
import java.awt.*;
import javax.swing.*;
public class PikachuAnimation extends JFrame {
private static final long serialVersionUID = 1L;
private int x = 50, y = 50;
private final int SCALE = 2;
private PikachuAnimation() {
super("Pikachu Animation");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setPreferredSize(new Dimension(300, 300));
pack();
setVisible(true);
}
public static void main(String[] args) {
new PikachuAnimation().run();
}
private void run() {
while (true) {
try {
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
x += 5;
if (x > getWidth()) {
x = -150;
}
repaint();
}
}
protected void paintComponent(Graphics g) {
super.paintComponents(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.setColor(Color.BLACK);
g2.setStroke(new BasicStroke(4));
// draw ears
g2.fillOval(x - 25 * SCALE, y - 80 * SCALE, 40 * SCALE, 80 * SCALE);
g2.fillOval(x + 27 * SCALE, y - 80 * SCALE, 40 * SCALE, 80 * SCALE);
g2.fillOval(x - 25 * SCALE, y - 75 * SCALE, 32 * SCALE, 64 * SCALE);
g2.fillOval(x + 29 * SCALE, y - 75 * SCALE, 32 * SCALE, 64 * SCALE);
// draw head
g2.setColor(new Color(250, 219, 51));
g2.fillOval(x - 40 * SCALE, y - 32 * SCALE, 80 * SCALE, 70 * SCALE);
g2.setColor(Color.BLACK);
g2.drawOval(x - 40 * SCALE, y - 32 * SCALE, 80 * SCALE, 70 * SCALE);
// draw nose
g2.fillOval(x - 5 * SCALE, y - 10 * SCALE, 10 * SCALE, 14 * SCALE);
// draw eyes
g2.fillOval(x - 23 * SCALE, y - 30 * SCALE, 18 * SCALE, 18 * SCALE);
g2.fillOval(x + 5 * SCALE, y - 30 * SCALE, 18 * SCALE, 18 * SCALE);
g2.setColor(Color.WHITE);
g2.fillOval(x - 21 * SCALE, y - 27 * SCALE, 9 * SCALE, 9 * SCALE);
g2.fillOval(x + 7 * SCALE, y - 27 * SCALE, 9 * SCALE, 9 * SCALE);
// draw cheeks
g2.setColor(new Color(251, 150, 17));
g2.fillOval(x - 43 * SCALE, y - 8 * SCALE, 26 * SCALE, 16 * SCALE);
g2.fillOval(x + 17 * SCALE, y - 8 * SCALE, 26 * SCALE, 16 * SCALE);
// draw mouth
g2.setStroke(new BasicStroke(3));
g2.drawArc(x - 17 * SCALE, y - 5 * SCALE, 35 * SCALE, 20 * SCALE, 190, 160);
// draw arms
g2.setColor(new Color(250, 219, 51));
g2.fillRect(x - 48 * SCALE, y + 25 * SCALE, 25 * SCALE, 10 * SCALE);
g2.fillRect(x + 23 * SCALE, y + 26 * SCALE, 25 * SCALE, 10 * SCALE);
// draw body
g2.fillRect(x - 35 * SCALE, y + 15 * SCALE, 70 * SCALE, 30 * SCALE);
g2.setColor(Color.BLACK);
g2.drawRect(x - 35 * SCALE, y + 15 * SCALE, 70 * SCALE, 30 * SCALE);
// draw legs
g2.fillRect(x - 22 * SCALE, y + 45 * SCALE, 20 * SCALE, 30 * SCALE);
g2.fillRect(x + 2 * SCALE, y + 45 * SCALE, 20 * SCALE, 30 * SCALE);
g2.drawRect(x - 22 * SCALE, y + 45 * SCALE, 20 * SCALE, 30 * SCALE);
g2.drawRect(x + 2 * SCALE, y + 45 * SCALE, 20 * SCALE, 30 * SCALE);
g2.fillOval(x - 24 * SCALE, y + 72 * SCALE, 25 * SCALE, 13 * SCALE);
g2.fillOval(x + 5 * SCALE, y + 72 * SCALE, 25 * SCALE, 13 * SCALE);
}
}
```
这段代码可以实现一个可爱的皮卡丘在窗口里不停跳动的动画效果。希望对您有所帮助!
阅读全文