java画星星_用java画色彩斑斓的星星

[java]代码库import java.awt.*;

import javax.swing.*;

public class WuJiaoXing extends JPanel {

public WuJiaoXing() {

this.setSize(800, 800);

}

public void paint(Graphics g) {

for (int i = 0; i < 100; i++) {

int c1, c2, c3, w, h, r;

c1 = (int) (Math.random() * 255);

c2 = (int) (Math.random() * 255);

c3 = (int) (Math.random() * 255);

Color c = new Color(c1, c2, c3);// 随机生成颜色的红绿蓝通道值

g.setColor(c);// 设置画笔颜色

w = (int) (Math.random() * 1000);

h = (int) (Math.random() * 800);

r = (int) (Math.random() * 80);

wujiaoxing(g, w, h, r, "fill");

}

}

public void wujiaoxing(Graphics g, int x0, int y0, int r, String f) {

double ch = 72 * Math.PI / 180;// 圆心角的弧度数

int x1 = x0, x2 = (int) (x0 - Math.sin(ch) * r), x3 = (int) (x0 + Math

.sin(ch) * r), x4 = (int) (x0 - Math.sin(ch / 2) * r), x5 = (int) (x0 + Math

.sin(ch / 2) * r);

int y1 = y0 - r, y2 = (int) (y0 - Math.cos(ch) * r), y3 = y2, y4 = (int) (y0 + Math

.cos(ch / 2) * r), y5 = y4;

int bx = (int) (x0 + Math.cos(ch) * Math.tan(ch / 2) * r);

int by = y2;

Polygon a = new Polygon();

Polygon b = new Polygon();

a.addPoint(x2, y2);

a.addPoint(x5, y5);

a.addPoint(bx, by);

b.addPoint(x1, y1);

b.addPoint(bx, by);

b.addPoint(x3, y3);

b.addPoint(x4, y4);

if (f.equals("draw")) {

g.drawPolygon(a);

g.drawPolygon(b);

}

if (f.equals("fill")) {

g.fillPolygon(a);

g.fillPolygon(b);

}

}

public static void main(String[] args) {

new f();

}

}

class f extends JFrame {

WuJiaoXing a = new WuJiaoXing();

public f() {

this.setSize(800, 800);

setLocation(100, 100);

this.add(a);

this.setVisible(true);

}

}

[代码运行效果截图]

137b1a90424bd966e85869592aa68d1b.png

694748ed64b9390909c0d88230893790.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,我可以用 HTML 和 CSS 来编写一个爱心。 首先,我们可以使用 HTML 的 `div` 元素来创建一个容器,然后在其中放置两个 `div` 元素,用于表示爱心的两个半圆: ```html <div id="heart"> <div class="half-circle"></div> <div class="half-circle"></div> </div> ``` 接下来,我们可以使用 CSS 来给这些元素添加样式。首先,我们设置容器的高度和宽度,并将其设为正方形: ```css #heart { height: 200px; width: 200px; } ``` 然后,我们设置两个半圆的高度和宽度,并使用 `border-radius` 属性将它们变成圆形: ```css .half-circle { height: 50%; width: 50%; border-radius: 50%; } ``` 接下来,我们可以使用 `background-color` 属性为两个半圆设置不同的背景颜色,以便它们看起来更加多彩: ```css .half-circle:first-child { background-color: pink; } .half-circle:last-child { background-color: red; } ``` 最后,我们可以使用 CSS 的 `transform` 属性将两个半圆旋转 45 度,并使用 `position` 属性将它们移动到正确的位置,从而形成一个完整的爱心: ```css .half-circle:first-child { transform: rotate(45deg); position: absolute; top: 0; left: 50%; } .half-circle:last-child { transform: rotate(-45deg); position: absolute; top: 50%; ### 回答2: 使用前端语言编写一个美观且色彩斑斓的爱心是非常简单的。你可以使用HTML和CSS来实现这个效果。 首先,你可以创建一个HTML文件,命名为index.html,并在文件中添加一个div元素,作为容器来显示爱心。 ```html <!DOCTYPE html> <html> <head> <title>爱心</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div class="heart"></div> </body> </html> ``` 然后,在同一目录下创建一个style.css文件,并在其中添加以下样式代码: ```css .heart { width: 100px; height: 100px; background: linear-gradient(to right, #ff758c, #ff7eb3); position: relative; transform: rotate(45deg); animation: heart-beat 1s infinite; } @keyframes heart-beat { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.1); } } ``` 在上述代码中,我们使用线性渐变背景来实现色彩斑斓的效果。你可以自定义渐变的颜色和方向,只需修改background属性中的颜色值。 此外,我们还为爱心添加了一个旋转和跳动的动效果。你可以通过修改animation属性中的时间和样式来调整动效果。 保存并运行代码,你将会看到一个美丽的、色彩斑斓的爱心在你的浏览器中展现出来。希望这能满足你的要求!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值