给我解释这一行的意思,就一行~~~

来源:百度知道 编辑:UC知道 时间:2024/05/20 03:46:59
就是下面有星点标注的那句话~~~
import java.awt.*;
import java.applet.*;
import java.awt.geom.*;
import java.util.*;
public class RandomShapes extends Applet {

private Shape shape;
public void init() {
shape = new Rectangle2D.Double(-1.0,-1.0,1.0,1.0);

}

public void paint(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;//★★★这句话是什么意思,为什么有这样的用法?
AffineTransform identity = new AffineTransform();

Random rand = new Random();

int width = getSize().width;
int height = getSize().height;

g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, width, height);

for(int n = 0; n < 300;n++)
{
g2d.setTransform(identity);
g2d.translate(rand.nextInt()%width, rand.nextInt()%height);
g2d.rotate(Math.toRadians(360*rand.nextDouble()));
g2d.scale(60*rand.nextDouble(),60*rand.nextDouble());
g2d.setColor(new Color(rand.nextInt()));
g

Graphics2D类是Graphics类的子类,用来绘制2D图形,这句话的意思是把Graphics类的对象g,强制转换成Graphics2D类的对象g2d。

Graphics2D g2d=(Graphics2D)g;//★★★这句话是什么意思,为什么有这样的用法?
把Graphics 对象强制转换为Graphics2D类型,因为要用到Graphics2D类中特有的一些方法吧。
Graphics2D 是Graphics的子类,所以要强制类型转换