加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
BalloonComponent.java 1.39 KB
一键复制 编辑 原始数据 按行查看 历史
林瑞亨 提交于 2020-04-09 20:15 . alter two object.
import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Rectangle;
import java.util.Random;
/**
This class displays a number of balloons.
*/
public class BalloonComponent extends JComponent
{
private int balloonCount;
/**
Constructor for a BalloonComponent with a number of balloons
@param n the number of balloons to show
*/
public BalloonComponent(int n)
{
balloonCount = n;
}
public void paintComponent(Graphics g)
{
final Color SKY_BLUE = new Color(165,218,239);
final int MAX_RADIUS = 30;
Graphics2D g2 = (Graphics2D) g;
// Draw the sky
Rectangle sky = new Rectangle (0, 0 , getWidth(), getHeight());
g2.setColor(SKY_BLUE);
g2.fill(sky);
// Use this random number generator
Random generator = new Random(42);
// Generate and draw balloons
// Your work here
for(int i=0;i<balloonCount;i++)
{
int centerx = generator.nextInt(getWidth());
int centery = generator.nextInt(getHeight());
int radius = generator.nextInt(MAX_RADIUS);
Color color = new Color(generator.nextInt(255),generator.nextInt(255),generator.nextInt(255));
Balloon b = new Balloon(centerx,centery,radius,color);
b.draw(g2);
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化