instancetest.java
来自「java learn PPT java learn PPT java learn」· Java 代码 · 共 48 行
JAVA
48 行
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
public class InstanceTest extends Applet
{
// 要绘制的Shape
private Shape shape;
public void init()
{
// 基于原点创建一个单位正方形
shape = new Rectangle2D.Float(-0.5f, -0.5f, 1.0f, 1.0f);
}
public void paint(Graphics g)
{
// 把传入的Graphics容器转换为一个可用的 Graphics2D 对象
Graphics2D g2d = (Graphics2D)g;
// 保存一个恒等变换来清除Graphics2D容器
final AffineTransform identity = new AffineTransform();
Random r = new Random();
int width = getSize().width;
int height = getSize().height;
// 创建形状的 500 个实例
for(int i = 0; i < 500; i++)
{
// 清除Graphics2D的变换
g2d.setTransform(identity);
// 随机设置变换
g2d.translate(r.nextInt()%width, r.nextInt()%height);
g2d.rotate(Math.toRadians(360*r.nextDouble()));
g2d.scale(20*r.nextDouble(), 10*r.nextDouble());
// draw the shape
g2d.setColor(new Color(r.nextInt()));
g2d.fill(shape);
}
}
} // InstanceTest
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?