Step By Step(Java 2D图形篇<二>) (三)

2014-11-24 03:21:37 · 作者: · 浏览: 3
AffineTransform at = AffineTransform.getScaleInstance(2, 2);

10 //这里通过drawRenderedImage方法将源图像变换后的结果直接写入目的BufferedImage

11 //注意:源图像并未发生变换

12 g.drawRenderedImage(bufferedImage, at);

13 ImageIO.write(destImage, "JPG", new File("D:/desktop.jpg"));

14 System.out.println("Over.");

15 }

16 }

6) Icon文件的读入: www.2cto.com

1 public class MyTest extends JPanel {

2 public static void main(String[] args) throws IOException {

3 JFrame frame = new JFrame();

4 frame.setTitle("Read Icon");

5 frame.setSize(300, 300);

6 frame.addWindowListener(new WindowAdapter() {

7 public void windowClosing(WindowEvent e) {

8 System.exit(0);

9 }

10 });

11 Container contentPane = frame.getContentPane();

12 contentPane.add(new MyTest());

13 frame.show();

14 }

15 public void paintComponent(Graphics g) {

16 super.paintComponent(g);

17 ImageIcon icon = new ImageIcon("D:/desktop.jpg");

18 int width = icon.getIconWidth();

19 int height = icon.getIconHeight();

20 int x = 0;

21 int y = 0;

22 //1. 利用ImageIcon的功能直接绘制

23 icon.paintIcon(this, g, x, y);

24 Graphics2D g2d = (Graphics2D)g;

25 //2. 转换到BufferedImage后在绘制

26 BufferedImage bi = icon.getImage();

27 g2d.drawImage(bi, x + 100, y + 100, null);

28 System.out.println("Width = " + width + " Height = " + height);

29 }

30 }


作者 Stephen_Liu