java getcolor_Java Color.getColor方法代码示例

import java.awt.Color; //导入方法依赖的package包/类

/**

* Searches for a color with a given name or key, given as a string. The

* color is searched using the following criteria:

*

*

If the name is one of the keys in getColorMap() then the

* corresponding key is returned;

*

Otherwise, if the name is a system property (recognised by

* Color.getColor(String)) then the corresponding color is

* returned;

*

Otherwise, if the name is a color key recognised by

* Color.decode(String)) then the corresponding color is

* returned;

*

Otherwise, if the name is a space-separated sequence of three or

* four byte values standing for the red, green and blue components, and

* optionally an alpha value, then the corresponding color is returned using

* the appropriate Color method;

*

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Java 烟花代码示例: ```java import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.util.ArrayList; import java.util.List; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; public class Fireworks extends JPanel { private static final int FRAME_WIDTH = 600; private static final int FRAME_HEIGHT = 400; private static final int MAX_PARTICLES = 150; private static final int MAX_TRIES = 200; private static final int PARTICLE_SIZE = 5; private List<Particle> particles; private Random rand; public Fireworks() { particles = new ArrayList<Particle>(); rand = new Random(); setBackground(Color.BLACK); } public void animate() { while (true) { createParticles(); repaint(); try { Thread.sleep(30); } catch (InterruptedException ex) {} } } private void createParticles() { int x = rand.nextInt(FRAME_WIDTH); int y = rand.nextInt(FRAME_HEIGHT); int tries = 0; while (isNearOtherParticles(x, y) && tries < MAX_TRIES) { x = rand.nextInt(FRAME_WIDTH); y = rand.nextInt(FRAME_HEIGHT); tries++; } if (tries < MAX_TRIES) { int numParticles = rand.nextInt(MAX_PARTICLES) + 1; Color color = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); for (int i = 0; i < numParticles; i++) { particles.add(new Particle(new Point(x, y), color)); } } } private boolean isNearOtherParticles(int x, int y) { for (Particle p : particles) { if (p.getPosition().distance(x, y) < PARTICLE_SIZE * 2) { return true; } } return false; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); for (Particle p : particles) { g.setColor(p.getColor()); g.fillOval(p.getPosition().x, p.getPosition().y, PARTICLE_SIZE, PARTICLE_SIZE); p.update(); } removeDeadParticles(); } private void removeDeadParticles() { List<Particle> deadParticles = new ArrayList<Particle>(); for (Particle p : particles) { if (p.isDead()) { deadParticles.add(p); } } particles.removeAll(deadParticles); } public static void main(String[] args) { JFrame frame = new JFrame("Fireworks"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); Fireworks fireworks = new Fireworks(); frame.add(fireworks); frame.setVisible(true); fireworks.animate(); } } ``` 这个程序使用 Java Swing 绘制烟花效果。它会在一个窗口中随机生成火花,并在屏幕上显示它们的粒子效果。每个火花由多个粒子组成,这些粒子会在屏幕上随机移动,直到它们消失。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值