`

Swing 图形处理

阅读更多

窗体设置为无框: setUndecorated(true);

 

截屏幕

Robot robot=new Robot(); 

int width =Toolkit .getDefaultToolkit().getScreenSize().width;

int height=Toolkit .getDefaultToolkit().getScreenSize().height;

Rectangle  rect=new   Rectangle(0, 0,width,height);

BufferImage    screenImage =robot.createScreenCapture(rect);

 

JDK6 ,AWT新增加了两个类:DesktopSystemTray

前者可以用来打开系统默认浏览器浏览指定的URL,打开系统默认邮件客户端给指定的邮箱发邮件,用默认应用程序打开或编辑文件(比如,用记事本打开以txt为后缀名的文件),用系统默认的打印机打印文档;

后者可以用来在系统托盘区创建一个托盘程序

  1.         if (Desktop.isDesktopSupported()) {// 判断当前平台是否支持Desktop类   
  2.             desktop = Desktop.getDesktop();   
  3.         }   
  4.         if (SystemTray.isSupported()) {// 判断当前平台是否支持系统托盘   
  5.             static  SystemTray st = SystemTray.getSystemTray();   
  6.             Image image = Toolkit.getDefaultToolkit().getImage("test/black_dragon1.png");// 获取托盘图标的图片   
  7.             PopupMenu pm=new PopupMenu();
  8.             TrayIcon ti = new TrayIcon(image, "Desktop Demo Tray", pm);  // 定义托盘图标的图片  
  9.             try {   
  10.                 st.add(ti);   
  11.             } catch (AWTException ex) {   
  12.                 ex.printStackTrace();   
  13.             }   
  14.         }

 

 面板绘图

例子:new JPanel() {
            public void paint(Graphics g) {
                super.paint(g);
                Rectangle rct = State.currentState.getSelectedRect();//获取状态。。。处理在State类模式中
                if (rct != null) {
                    Color oldColor = g.getColor();
                    // 用红色绘制出选中矩形
                    g.setColor(Color.RED);
                    g.drawRect(rct.x, rct.y, rct.width, rct.height);
                }
            }
        };

 

 图形缩放:

public class ImageCut {
/**
  * 缩放图像
  *
  * @param srcImageFile
  *            源图像文件地址
  * @param result
  *            缩放后的图像地址
  * @param scale
  *            缩放比例
  * @param flag
  *            缩放选择:true 放大; false 缩小;
  */
public static void scale(String srcImageFile, String result, int scale, boolean flag) {
  try {
     BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件
     int width = src.getWidth(); // 得到源图宽
     int height = src.getHeight(); // 得到源图长
     if (flag) { // 放大
       width = width * scale;
       height = height * scale;
     } else { // 缩小
      width = width / scale;
      height = height / scale;
     } 
     Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);
//返回图像的缩放版本。默认的图像缩放算法   
     BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 预定义一个图像 
     Graphics g = tag.getGraphics(); //返回Graphics,可用于绘制预定义的图像。
     g.drawImage(image, 0, 0, null); // 用图像的缩放版本去绘制缩放后的图   

     g.dispose();  //释放此图形的上下文以及它使用的所有系统资源。调用 dispose 之后,就不能再使用 Graphics 对象
     ImageIO.write(tag, "JPEG", new File(result)); // 输出到文件流 
  } catch (IOException e) {
   e.printStackTrace();
  }
}
public static void main(String[] args) {
   ImageCut.scale("1.jpg","放大.jpg",4,true);
}

 

图形裁剪:

CropImageFilter 用于裁剪图像的 ImageFilter 类。此类扩展了基本 ImageFilter 类,可提取现有 Image 中的给定矩形区域,为包含刚提取区域的新图像提供源。也就是它要与 FilteredImageSource 对象结合使用,以生成现有图像的裁剪版本。

CropImageFilter  cropFilter = new CropImageFilter(int x,int y ,int w,int h );//按指定 x、y、w 和 h 参数从源 Image 提取绝对矩形区域

Image   image = Toolkit.getDefaultToolkit().createImage( new FilteredImageSource(image.getSource(), cropFilter)); 

(其他操作同图形缩放)
 

图像类型转换

只需要把   ImageIO.write(tag, "JPG", new File(result)); 中的“JPG”改为想要的类型即可

图像变黑白

BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件

src = new  ColorConvertOpColorSpace.getInstance( ColorSpace.CS_GRAY, null )).filter(src, null);

ImageIO.write(src, "JPEG", new File(result)); //输出文件

关于图形处理的java.awt.image的各种类查看

http://www.xasxt.com/index.php/article/jiaocheng/shipin_1328.html

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics