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);
}
}