java满屏飘爱心代码
要在Java中实现满屏飘爱心的效果,你可以使用Swing图形库来创建一个简单的窗口,并在窗口中绘制爱心。
javaimport javax.swing.*;
import java.awt.*;
import java.util.Random;
public class HeartRain extends JFrame {
private static final int NUM_HEARTS = 100;
public HeartRain() {
setTitle("Heart Rain");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
HeartPanel heartPanel = new HeartPanel();
add(heartPanel);
Timer timer = new Timer(20, e -> {
heartPanel.moveHearts();
repaint();
});
timer.start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new HeartRain().setVisible(true);
});
}
}
class Heart {
int x, y;
int size;
int speed;
public Heart(int x, int y, int size, int speed) {
this.x = x;
this.y = y;
this.size = size;
this.speed = speed;
}
}
class HeartPanel extends JPanel {
private static final int MIN_HEART_SIZE = 20;
private static final int MAX_HEART_SIZE = 50;
private static final int MIN_SPEED = 1;
private static final int MAX_SPEED = 5;
private java.util.List<Heart> hearts;
public HeartPanel() {
hearts = new java.util.ArrayList<>();
Random random = new Random();
for (int i = 0; i < HeartRain.NUM_HEARTS; i++) {
int x = random.nextInt(getWidth());
int y = random.nextInt(getHeight());
int size = random.nextInt(MAX_HEART_SIZE - MIN_HEART_SIZE + 1) + MIN_HEART_SIZE;
int speed = random.nextInt(MAX_SPEED - MIN_SPEED + 1) + MIN_SPEED;
hearts.add(new Heart(x, y, size, speed));
}
}
public void moveHearts() {
for (Heart heart : hearts) {
heart.y += heart.speed;
if (heart.y > getHeight()) {
heart.y = 0;
}
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for (Heart heart : hearts) {
drawHeart(g2d, heart.x, heart.y, heart.size);
}
}
private void drawHeart(Graphics2D g2d, int x, int y, int size) {
g2d.setColor(Color.RED);
int[] xPoints = new int[10];
int[] yPoints = new int[10];
xPoints[0] = x;
yPoints[0] = y - size / 2;
xPoints[1] = x + size / 4;
yPoints[1] = y - size / 4;
xPoints[2] = x + size / 2;
yPoints[2] = y - size / 2;
xPoints[3] = x + size / 4;
yPoints[3] = y + size / 4;
xPoints[4] = x;
yPoints[4] = y;
xPoints[5] = x - size / 4;
yPoints[5] = y + size / 4;
xPoints[6] = x - size / 2;
yPoints[6] = y - size / 2;
xPoints[7] = x - size / 4;
yPoints[7] = y - size / 4;
xPoints[8] = x;
yPoints[8] = y - size / 2;
g2d.fillPolygon(xPoints, yPoints, 9);
}
}
这个例子中,创建了一个HeartRain
类作为窗口的主类,其中包含一个HeartPanel
类用于绘制爱心和处理动画效果。通过Timer
类来定时更新爱心的位置,并在paintComponent
方法中绘制爱心。注意,
如果你想要使这个例子更加有趣,你可以尝试添加一些亮丽的颜色、变化的大小和旋转效果。
javaimport javax.swing.*;
import java.awt.*;
import java.util.Random;
public class HeartRain extends JFrame {
private static final int NUM_HEARTS = 100;
public HeartRain() {
setTitle("Heart Rain");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
HeartPanel heartPanel = new HeartPanel();
add(heartPanel);
Timer timer = new Timer(20, e -> {
heartPanel.moveHearts();
repaint();
});
timer.start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new HeartRain().setVisible(true);
});
}
}
class Heart {
int x, y;
int size;
int speed;
Color color;
double rotation;
public Heart(int x, int y, int size, int speed, Color color, double rotation) {
this.x = x;
this.y = y;
this.size = size;
this.speed = speed;
this.color = color;
this.rotation = rotation;
}
}
class HeartPanel extends JPanel {
private static final int MIN_HEART_SIZE = 20;
private static final int MAX_HEART_SIZE = 50;
private static final int MIN_SPEED = 1;
private static final int MAX_SPEED = 5;
private java.util.List<Heart> hearts;
public HeartPanel() {
hearts = new java.util.ArrayList<>();
Random random = new Random();
for (int i = 0; i < HeartRain.NUM_HEARTS; i++) {
int x = random.nextInt(getWidth());
int y = random.nextInt(getHeight());
int size = random.nextInt(MAX_HEART_SIZE - MIN_HEART_SIZE + 1) + MIN_HEART_SIZE;
int speed = random.nextInt(MAX_SPEED - MIN_SPEED + 1) + MIN_SPEED;
Color color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
double rotation = random.nextDouble() * 2 * Math.PI;
hearts.add(new Heart(x, y, size, speed, color, rotation));
}
}
public void moveHearts() {
for (Heart heart : hearts) {
heart.y += heart.speed;
if (heart.y > getHeight()) {
heart.y = 0;
}
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for (Heart heart : hearts) {
drawHeart(g2d, heart.x, heart.y, heart.size, heart.color, heart.rotation);
}
}
private void drawHeart(Graphics2D g2d, int x, int y, int size, Color color, double rotation) {
g2d.setColor(color);
int[] xPoints = new int[10];
int[] yPoints = new int[10];
for (int i = 0; i < 9; i++) {
double angle = i * Math.PI / 4 + rotation;
int xOffset = (int) (Math.cos(angle) * size);
int yOffset = (int) (Math.sin(angle) * size);
xPoints[i] = x + xOffset;
yPoints[i] = y + yOffset;
}
g2d.fillPolygon(xPoints, yPoints, 9);
}
}
在这个例子中,我添加了color
和rotation
属性到Heart
类,使每个爱心具有不同的颜色和旋转角度。在drawHeart
方法中,通过计算每个点的坐标来实现旋转效果。你可以根据需要进行更多的定制,添加更多的效果和动画。