16. 3. 5. 缩放一个Object Scaling an object

 

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

import javax.swing.JFrame;
import javax.swing.JPanel;


//Scale:缩放、比例
public class Scale extends JPanel{
	public void paint(Graphics g){
		super.paint(g);
		Graphics2D g2d = (Graphics2D)g;
		g2d.setColor(new Color(150,150,150));
		g2d.fillRect(0, 0, 80, 50);
		//===============================================================
		//这一节里设置一个tx1,把g2d设置为tx1,比例是缩小了
		AffineTransform tx1 = new AffineTransform();//设置一个2D变换
		tx1.translate(200, 20);//平移的位置
		tx1.scale(0.5, 0.5);//缩放比例
		
		g2d.setTransform(tx1);
		g2d.fillRect(0, 0, 80, 50);
		//===============================================================
		
		//===============================================================
		//这一节里设置一个tx2,把g2d设置为tx2,比例是放大了
		AffineTransform tx2 = new AffineTransform();//设置一个2D变换
		tx2.translate(200, 20);//平移的位置
		tx2.scale(1.5, 1.5);//缩放比例
		
		g2d.setTransform(tx2);
		g2d.fillRect(0, 0, 80, 50);
		//===============================================================		
	}
	
	public static void main(String[] args) {
		JFrame frame = new JFrame("Scaling");
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frame.add(new Scale());
	    frame.setSize(330, 160);
	    //如果组件当前未显示,或者 c 为 null,则此窗口将置于屏幕的中央。
	    //设置窗口相对于指定组件的位置。
	    frame.setLocationRelativeTo(null);
	    frame.setVisible(true);  
	}
}


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值