【Java开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了Java开源代码-CompositeTest.java的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
// 封装方形的属性 (位置, 大小等)让它可以有规律的更新
// 的一个简单方法
class AlphaBox
{
// 随机数生成器
private static Random random = null;
// 所有的绘制都将从一个单位正方形开始
private static Rectangle2D square = null;
// 恒等变换
private static AffineTransform identity = null;
// 盒子的属性
private AlphaComposite alpha;
private double xPos; // x, y 位置
private double yPos;
private double xVel; // x, y 速度
private double yVel;
private double size; // 宽和高
private Color color; // 实例的颜色
private Dimension windowSize;
public AlphaBox(Dimension d)
{
windowSize = d;
// 定义所有的空对象
if(random == null)
{
random = new Random();
}
if(square == null)
{
square = new Rectangle2D.Float(-0.5f, -0.5f, 1.0f, 1.0f);
}
if(identity == null)
{
identity = new AffineTransform();
}
// 所有的合成都将是 SRC_OVER 的而且透明
// 使用这些值的随机数来得到一些很酷的效果
alpha = AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 0.25f);
// 随机得到盒子的属性
xPos = windowSize.width*random.nextDouble();
yPos = windowSize.height*random.nextDouble();
xVel = 1+2*random.nextDouble();
if(random.nextDouble() > 0.5) xVel = -xVel;
yVel = 1+2*random.nextDouble();
if(random.nextDouble() > 0.5) yVel = -yVel;
size = 25+100*random.nextDouble();
color = new Color(random.nextInt()).brighter();
}
// 根据盒子当前的属性把它绘制到所传入的 Graphics2D容器中
public void paint(Graphics2D g2d)
{
// 让盒子在窗体上弹跳
xPos += xVel;
if(xPos > windowSize.width)
{
xPos = windowSize.width;
xVel = -xVel;
}
if(xPos < 0)
{
xPos = 0;
xVel = -xVel;
}
yPos += yVel;
if(yPos > windowSize.height)
{
yPos = windowSize.height;
yVel = -yVel;
}
if(yPos < 0)
{
yPos = 0;
yVel = -yVel;
}
// render the box
g2d.setTransform(identity);
g2d.translate(xPos, yPos);
g2d.scale(size, size);
g2d.setComposite(alpha);
g2d.setPaint(color);
g2d.fill(square);
}
} // AlphaBox
public class CompositeTest extends Applet implements Runnable
{
// 动画线程 -- 稍后我们会谈到
private volatile Thread animation;
// AlphaBox 对象数组
private AlphaBox[] boxes;
public void init()
{
animation = new Thread(this);
// 创建这些盒子
final int n = 10;
boxes = new AlphaBox[n];
Dimension size = this.getSize();
for(int i = 0; i < n; i++)
{
boxes[i] = new AlphaBox(size);
}
}
public void start()
{
animation.start();
}
public void stop()
{
animation = null;
}
// 覆盖 update 方法,让它不要清除窗体
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
// 绘制每一个AlphaBox
for(int i = 0; i < boxes.length; i++)
{
boxes[i].paint(g2d);
}
}
public void run()
{
// 稍后我们会提到这个内容!
Thread t = Thread.currentThread();
while (t == animation)
{
try
{
t.sleep(10);
}
catch (InterruptedException e)
{
}
repaint();
}
}
} // CompositeTest
上一篇:
CompositeSeparator1.java
下一篇:
飞行射击游戏源代码_飞行射击游戏android代码_飞行射击游戏APK包