为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

宝慕林4294392 2022-12-17 17:13:35
/** 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 hereShapes2 application=new Shapes2();application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}我想问两个问题:(1) rotate()函数是否是以坐标原点为中心旋转的(2) 对于上面那个star.closePath()这一句,删去以后效果好像是一样的.那为什么原程序要加这一句呢?
查看完整描述

2 回答

?
POPMUISE

TA贡献1765条经验 获得超5个赞

radius += Math.PI / 2; 
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度时宽高要互调一下

 


查看完整回答
反对 回复 2022-12-21
?
手掌心

TA贡献1942条经验 获得超3个赞

Q1:

public abstract void rotate(double theta,
double x,
double y)将当前的 Graphics2D Transform 与平移后的旋转转换连接。后续呈现的变换是平移到指定位置,旋转指定弧度,然后向回平移相同的距离。这等同于以下调用序列: 
translate(x, y);
rotate(theta);
translate(-x, -y);
使用正角度 theta 进行旋转,可将正 x 轴上的点转向正 y 轴。 

参数:
theta - 旋转的角度,以弧度表示
x - 旋转原点的 x 坐标
y - 旋转原点的 y 坐标

Q2:

public void closePath()通过向最后 moveTo 的坐标绘制直线闭合当前子路径。如果已经闭合路径,则此方法无效。

查看完整回答
反对 回复 2022-12-21
  • 2 回答
  • 0 关注
  • 115 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号