java输出一个会动的爱心
时间: 2023-09-10 10:05:10 浏览: 97
### 回答1:
可以使用Java语言的图形库来实现一个会动的爱心,下面是一个示例代码:
```
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
public class MovingHeart extends JPanel implements Runnable {
private int x = 0;
private int y = 0;
private int step = 1;
private int width = 400;
private int height = 400;
private int delay = 10;
public static void main(String[] args) {
JFrame frame = new JFrame("Moving Heart");
frame.add(new MovingHeart());
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Point[] points = getHeartPoints();
g.setColor(Color.RED);
g.fillPolygon(new int[]{points[0].x, points[1].x, points[2].x},
new int[]{points[0].y, points[1].y, points[2].y}, 3);
g.fillPolygon(new int[]{points[0].x, points[1].x, points[3].x},
new int[]{points[0].y, points[1].y, points[3].y}, 3);
}
private Point[] getHeartPoints() {
Point[] points = new Point[4];
int x = this.width / 2;
int y = this.height / 2;
int r = Math.min(this.width, this.height) / 4;
points[0] = new Point(x, y - r);
points[1] = new Point(x - r, y);
points[2] = new Point(x, y + r);
points[3] = new Point(x + r, y);
return points;
}
@Override
public void run() {
while (true) {
if (this.x >= this.width || this.x <= 0) {
this.step *= -1;
}
this.x += this.step;
this.y = (int) (this.height / 2 - Math.sqrt(Math.pow(this.width / 2 - this.x, 2) / 3));
repaint();
try {
Thread.sleep(this.delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public MovingHeart() {
Thread t = new Thread(this);
t.start();
}
}
```
这个程序会在窗口中画一个爱心,而且会使它在窗口中左右移动。
### 回答2:
Java输出一个会动的爱心可以使用JavaFX来实现。以下是一个示例代码:
```java
import javafx.animation.Animation;
import javafx.animation.PathTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import javafx.util.Duration;
public class AnimatedHeart extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Group root = new Group();
Scene scene = new Scene(root, 400, 400, Color.BLACK);
double x = 200;
double y = 200;
double radiusX = 80;
double radiusY = 80;
MoveTo start = new MoveTo(x, y + radiusY);
CubicCurveTo c1 = new CubicCurveTo(x + radiusX, y + radiusY,
x + radiusX, y,
x, y);
CubicCurveTo c2 = new CubicCurveTo(x - radiusX, y,
x - radiusX, y + radiusY,
x, y + radiusY);
Path path = new Path();
path.getElements().addAll(start, c1, c2, new ClosePath());
PathTransition pathTransition = new PathTransition();
pathTransition.setDuration(Duration.seconds(3));
pathTransition.setPath(path);
pathTransition.setNode(root);
pathTransition.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
pathTransition.setCycleCount(Animation.INDEFINITE);
pathTransition.setAutoReverse(true);
pathTransition.play();
primaryStage.setTitle("Animated Heart");
primaryStage.setScene(scene);
primaryStage.show();
}
}
```
运行以上代码将创建一个JavaFX的窗口,其中显示一个会动的爱心。爱心的动画效果由PathTransition类实现,通过定义一个路径和相关的动画参数,用于控制动画的播放。通过设置不同的参数,可以调整动画的速度、循环次数等。
这段代码中使用的是CubicCurveTo类来定义爱心的形状。首先通过顶点和两个控制点的坐标来创建一个二次贝塞尔曲线,通过路径的闭合可以形成一个爱心的形状。然后定义PathTransition动画,将动态路径应用于场景中的节点root,使其沿着调整后的路径进行运动。
在JavaFX中,可以按照自己的需求调整代码,例如改变爱心的颜色、位置、大小等,增加更多的动画效果等。
### 回答3:
可以使用Java的图形库和循环语句来实现一个动态显示的爱心。下面是一个简单的示例代码:
```java
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AnimatedHeart extends JPanel {
private static final long serialVersionUID = 1L;
private int x = 150;
private int y = 150;
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
g.setFont(new Font("Arial", Font.BOLD, 24));
g.drawString("❤", x, y);
}
public void move() {
while (true) {
for (int i = 0; i <= 150; i++) {
x++;
y++;
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i <= 150; i++) {
x--;
y++;
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i <= 150; i++) {
x++;
y--;
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i <= 150; i++) {
x--;
y--;
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Animated Heart");
AnimatedHeart heart = new AnimatedHeart();
frame.add(heart);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
heart.move();
}
}
```
在代码中,我们使用`JPanel`类来绘制爱心图案,并使用`Graphics`对象的`drawString`方法来打印出一个红色的心形符号。通过`move`方法的循环,可以实现爱心在窗口中移动的效果。
要注意的是,为了让爱心以动态的方式显示,我们使用`repaint`方法重新绘制窗口,并使用`Thread.sleep`方法来控制每一帧之间的间隔时间。
希望以上代码能够满足您的需求!
阅读全文