49 BufferedImage bi2 = new BufferedImage(bi.getWidth()
50 ,bi.getHeight(),BufferedImage.TYPE_INT_RGB);
51 //这里主要是为了确保每个图像都显示在各自的单元格内,因此用一个临时的
52 //BufferedImage对象替换原有对象显示。事实上,是可以通过以下语句直接渲染的。
53 //g2.drawImage(bi,aop,offsetx,offsety);但是这样的渲染结果将会使width放大1.5倍。
54 aop.filter(bi, bi2);
55 g2.drawImage(bi2, offsetx, offsety,null);
56 offsetx += bi.getWidth();
57 // 5. 在1,1的位置显示锐化后原图
58 float[] data = SHARPEN3x3;
59 ConvolveOp cop = new ConvolveOp(new Kernel(3, 3, data), ConvolveOp.EDGE_NO_OP, null);
60 g2.drawImage(bi, cop, offsetx, offsety);
61 offsetx += bi.getWidth();
62 // 6. 在1,2的位置显示通过RescaleOp图像处理器改变原图的灰度。
63 RescaleOp rop = new RescaleOp(1.1f, 20.0f, null);
64 g2.drawImage(bi, rop, offsetx, offsety);
65 }
66 public static void main(String[] args) {
67 JFrame frame = new JFrame();
68 frame.setTitle("BufferedImage");
69 frame.setSize(ALL_WIDTH, ALL_HEIGHT);
70 frame.addWindowListener(new WindowAdapter() {
71 public void windowClosing(WindowEvent e) {
72 System.exit(0);
73 }
74 });
75 Container contentPane = frame.getContentPane();
76 MyTest p = new MyTest();
77 contentPane.add(p);
78 frame.show();
79 }
80 }
3) 在介绍ColorModel之前,我们需要先了解在Java 2D 中另外一组比较重要的图形工具对象--图形环境(GraphicsEnvironment)、图形设备(GraphicsDevice)和图形配置(GraphicsConfiguration)。通过下面这个简单的示例代码,可以非常清楚的看出他们之间的关系以及各自的作用。
1 public class MyTest {
2 public static void main(String[] args) {
3 //1. 获取本地的图形环境
4 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
5 //2. 获取所有的屏幕设备
6 GraphicsDevice[] gs = ge.getScreenDevices();
7 //3. 获取每个屏幕设备的配置对象
8 for (int j = 0; j < gs.length; j++) {
9 GraphicsDevice gd = gs[j];
10 System.out.println("Device " + j + ": " + gd);
11 GraphicsConfiguration[] gc = gd.getConfigurations();
12 for (int i = 0; i < gc.length; i++) {
13 System.out.println(" Configuration " + i + ": " + gc[i]);
14 System.out.println(" Bounds: " + gc[i].getBounds());
15 }
16 }
17 }
18 }
19 /* 输出结果如下:
20 Device 0: Win32GraphicsDevice[screen=0]
21 Configuration 0: sun.awt.Win32GraphicsConfig@1b8d6f7[dev=Win32GraphicsDevice[screen=0],pixfmt=0]
22 Bounds: java.awt.Rectangle[x=0,y=0,width=1280,height=800]
23 */
4) 通过图形设备工具类获取和屏幕相关的Metrics信息。
1 public class MyTest {
2 public static void main(String[] args) {
3 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
4 GraphicsDevice gs = ge.getDefaultScreenDevice();
5 DisplayMode[] dmodes = gs.getDisplayModes();
6 for (int i = 0; i < dmodes.length; i++) {
7