猿问

想咨询两个问题,具体情况如下所示:

/*
* Shapes2.java
*
* Created on 2007年8月2日, 上午9:39
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

/**
*
* @author user
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*; 
import javax.swing.*;
public class Shapes2 extends JFrame{

/** Creates a new instance of Shapes2 */
public Shapes2() {
super("Drawing 2D shapes");

getContentPane().setBackground(Color.yellow);
setSize(400,400);
setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);
int xPoints[]={55,67,109,73,83,55,27,37,1,43};
int yPoints[]={0,36,36,54,96,72,96,54,36,36};
Graphics2D g2d=(Graphics2D)g;

GeneralPath star=new GeneralPath();
star.moveTo(xPoints[0],yPoints[0]);

for(int count=1;count<xPoints.length;count++)
star.lineTo(xPoints[count],yPoints[count]);
star.closePath();

g2d.translate(200,200);

for(int count=1;count<=20;count++)
{
g2d.rotate(Math.PI/10.0);
g2d.setColor(new Color((int)(Math.random()*256),
(int)(Math.random()*256),
(int)(Math.random()*256)));
g2d.fill(star);
}

}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Shapes2 application=new Shapes2();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}
我想问两个问题:(1) rotate()函数是否是以坐标原点为中心旋转的
(2) 对于上面那个star.closePath()这一句,删去以后效果好像是一样的.那为什么原程序要加这一句呢?

宝慕林4294392
浏览 116回答 2
2回答

POPMUISE

radius += Math.PI / 2;&nbsp;try{File f = new File(fpath+imagename);//根据路径和文件名创建文件对象;Image image = ImageIO.read(f)ImageIcon ico = null;AffineTransform trans = null;trans = new AffineTransform();x = image.getWidth(null);y = image.getHeight(null);trans.rotate(radius,x/2 , y/2);BufferedImage buffer1 = new BufferedImage(x,y,BufferedImage.TYPE_INT_RGB);Graphics2D g2 = buffer1.createGraphics();g2.setTransform(trans);ico = new ImageIcon(buffer1);g2.drawImage(image,null,null);limage.setIcon(ico);//传入Label}catch(Exception e) {e.printStackTrace();}buffer1的宽高要根据旋转的角度定的,180度时宽高保持不变,90或270度时宽高要互调一下&nbsp;

手掌心

Q1:public abstract void rotate(double theta,double x,double y)将当前的 Graphics2D Transform 与平移后的旋转转换连接。后续呈现的变换是平移到指定位置,旋转指定弧度,然后向回平移相同的距离。这等同于以下调用序列:&nbsp;translate(x, y);rotate(theta);translate(-x, -y);使用正角度 theta 进行旋转,可将正 x 轴上的点转向正 y 轴。&nbsp;参数:theta - 旋转的角度,以弧度表示x - 旋转原点的 x 坐标y - 旋转原点的 y 坐标Q2:public void closePath()通过向最后 moveTo 的坐标绘制直线闭合当前子路径。如果已经闭合路径,则此方法无效。
随时随地看视频慕课网APP

相关分类

Java
我要回答