测试客户端的使用_举例说明如何使用客户端生成工具生成出来的文件做测试-程序员宅基地

插入源代码:(以下代码只是我用来测试cnblogs推荐的客户端中添加源代码的方法,效果还是比在线差了点,内容请忽略!!!!!)

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.silianbo;

/**
*
* @author silianbo
*/
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JToolBar;
import javax.swing.JWindow;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;
public class ScreenShot {
public static void main(String[] args) {

  EventQueue.invokeLater(() -> {
      try {
          ScreenShotWindow ssw=new ScreenShotWindow();
          ssw.setVisible(true);
      } catch (AWTException e) {
      }
  });
}
}
/*
* 截图窗口
*/
class ScreenShotWindow extends JWindow
{
private int orgx, orgy, endx, endy;
    private BufferedImage image=null;
    private BufferedImage tempImage=null;
    private BufferedImage saveImage=null;

    private ToolsWindow tools=null;

public ScreenShotWindow() throws AWTException{
   //获取屏幕尺寸
   Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
   this.setBounds(0, 0, d.width, d.height);

   //截取屏幕
   Robot robot = new Robot();
   image = robot.createScreenCapture(new Rectangle(0, 0, d.width,d.height));

   this.addMouseListener(new MouseAdapter() {
    @Override
   public void mousePressed(MouseEvent e) {
    //鼠标松开时记录结束点坐标,并隐藏操作窗口
             orgx = e.getX();
             orgy = e.getY();

             if(tools!=null){
              tools.setVisible(false);
             }
   }
   @Override
   public void mouseReleased(MouseEvent e) {
    //鼠标松开时,显示操作窗口
    if(tools==null){
     tools=new ToolsWindow(ScreenShotWindow.this,e.getX(),e.getY());
    }else{
     tools.setLocation(e.getX(),e.getY());
    }
    tools.setVisible(true);
    tools.toFront();
   }
  });

   this.addMouseMotionListener(new MouseMotionAdapter() {

   @Override
   public void mouseDragged(MouseEvent e) {
    //鼠标拖动时,记录坐标并重绘窗口
                endx = e.getX();
                endy = e.getY();

                //临时图像,用于缓冲屏幕区域放置屏幕闪烁
                Image tempImage2=createImage(ScreenShotWindow.this.getWidth(),ScreenShotWindow.this.getHeight());
                Graphics g =tempImage2.getGraphics();
                g.drawImage(tempImage, 0, 0, null);
                int x = Math.min(orgx, endx);
                int y = Math.min(orgy, endy);
                int width = Math.abs(endx - orgx)+1;
                int height = Math.abs(endy - orgy)+1;
                // 加上1防止width或height0
                g.setColor(Color.BLUE);
                g.drawRect(x-1, y-1, width+1, height+1);
                //减1加1都了防止图片矩形框覆盖掉
                saveImage = image.getSubimage(x, y, width, height);
                g.drawImage(saveImage, x, y, null);

                ScreenShotWindow.this.getGraphics().drawImage(tempImage2,0,0,ScreenShotWindow.this);
   }
  });
}

    @Override
    public void paint(Graphics g) {
        RescaleOp ro = new RescaleOp(0.8f, 0, null);
        tempImage = ro.filter(image, null);
        g.drawImage(tempImage, 0, 0, this);
    }
    //保存图像到文件
public void saveImage() throws IOException {
  JFileChooser jfc=new JFileChooser();
  jfc.setDialogTitle("保存");

  //文件过滤器,用户过滤可选择文件
  FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG", "jpg");
  jfc.setFileFilter(filter);

  //初始化一个默认文件(此文件会生成到桌面上)
  SimpleDateFormat sdf = new SimpleDateFormat("yyyymmddHHmmss");
     String fileName = sdf.format(new Date());
     File filePath = FileSystemView.getFileSystemView().getHomeDirectory();
     File defaultFile = new File(filePath + File.separator + fileName + ".jpg");
     jfc.setSelectedFile(defaultFile);

  int flag = jfc.showSaveDialog(this);
  if(flag==JFileChooser.APPROVE_OPTION){
   File file=jfc.getSelectedFile();
   String path=file.getPath();
   //检查文件后缀,放置用户忘记输入后缀或者输入不正确的后缀
   if(!(path.endsWith(".jpg")||path.endsWith(".JPG"))){
    path+=".jpg";
   }
   //写入文件
   ImageIO.write(saveImage,"jpg",new File(path));
   System.exit(0);
  }
}
}
/*
* 操作窗口
*/
class ToolsWindow extends JWindow
{
private final ScreenShotWindow parent;

public ToolsWindow(ScreenShotWindow parent,int x,int y) {
  this.parent=parent;
  this.init();
  this.setLocation(x, y);
  this.pack();
  this.setVisible(true);
}

private void init(){

  this.setLayout(new BorderLayout());
  JToolBar toolBar=new JToolBar("Java 截图");

  //保存按钮
  JButton saveButton=new JButton(new ImageIcon("images/save.gif"));
  saveButton.addActionListener((ActionEvent e) -> {
      try {
          parent.saveImage();
      } catch (IOException e1) {
          e1.printStackTrace();
      }
  });
  toolBar.add(saveButton);

  //关闭按钮
  JButton closeButton=new JButton(new ImageIcon("images/close.gif"));
  closeButton.addActionListener((ActionEvent e) -> {
      System.exit(0);
  });
  toolBar.add(closeButton);

  this.add(toolBar,BorderLayout.NORTH);
}
}

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u010278862/article/details/77996357

智能推荐

蛮力法解决01背包问题-程序员宅基地

文章浏览阅读1.6w次,点赞5次,收藏66次。蛮力法:设计算法求解背包问题,并编程实现。背包问题: 给定重量分别为,价值分别为的n件物品,和一个承重为W的背包。求这些物品中一个最有价值的子集,并能装到背包中。背包问题的蛮力解法是穷举这些物品的所有子集,找出能够装到背包中的所有子集,并在这些子集中找出价值最大的子集。实验:编写程序,实现背包问题的蛮力算法。并针对以下两个实例,求出能装到背包中的价值最大的子集。要求输出:最优可..._蛮力法解决01背包问题

Oracle PL-SQL 的使用_oracle数据库plsql使用-程序员宅基地

文章浏览阅读776次。使用PL-SQL1.关于PL-SQL(功能、特点及语法结构)2.数据类型(%type与%rowtype)3.PL-SQL控制语句4.异常处理5.函数============================================PL/SQL是过程语言PL与结构化查询语言SQL结合而成的编程语言。 PL/SQL是针对Oracle数据库的; 它是过程语言 + 结构化查询语言的结合; 过程语言PL:如变量声明,流程的控制,循环等; 查询语言SQL:SQL语..._oracle数据库plsql使用

Google Pay Error : Something went wrong on our end.Please try again_谷歌账号无法使用bard,提示somethingwentwrong-程序员宅基地

文章浏览阅读7.3k次。谷歌付费时出错,Something went wrong on our end.Please try again。出现问题的原因是:付费功能在后台没激活。激活就好了。(第一次上架,使用测试账号进行beta测试)..._谷歌账号无法使用bard,提示somethingwentwrong

从Vue2到Vue3【五】——新的组件(Fragment、Teleport、Suspense)_fragment teleport-程序员宅基地

文章浏览阅读5.1k次,点赞93次,收藏84次。本文详细讲解了vue3中新增的三个组件(Fragment、Teleport、Suspense),这些新增的组件为Vue 3带来了更多的灵活性和便利性。它们使得组件化开发更加简单且功能更丰富,为开发者提供更多的选择和工具来构建高效、可维护的应用程序。_fragment teleport

爬虫:BeautifulSoup4库基础_beautifulsoup4官网-程序员宅基地

文章浏览阅读464次。beautifulsoup4库的安装、使用、应用实例。_beautifulsoup4官网

《小学生C++趣味编程》视频 第 8单元指针、类-程序员宅基地

文章浏览阅读113次。《小学生C++趣味编程》视频 第 8单元指针、类

随便推点

【语音识别】电话按键语音识别(连续语音数字)【含Matlab源码 3416期】-程序员宅基地

文章浏览阅读636次,点赞13次,收藏23次。电话按键语音识别(连续语音数字)完整的代码,包运行;运行操作视频见CSDN资源!适合小白!

ABAP学习----ALV注意事项_abap alv多久学会-程序员宅基地

文章浏览阅读1k次。2018年/8月/1日。 到今天为止,学习ABAP大概快一个月了,我知道一个月,对于任何一门计算机语言来说,都只能说才了解,更何况是在自学,没有视频的情况下。ABAP语言相对其他语言来说,较为封闭,因为它只能在SAP系统里才能编写实现,而SAP系统对于个体户来说,安装太不现实。应该说几乎所有的ABAP开发人员都是在项目上学习的。幸运的是,我碰巧来到一个实施SAP的项目,目前在学习ABAP开发。 ..._abap alv多久学会

查找树的指定层级_非递归层次遍历方法实现二叉树中指定节点的层次数查找-程序员宅基地

文章浏览阅读336次。数据结构教材中,提供了基于队列实现一个二叉树的非递归层次遍历算法。但对于一个任意二叉树,如果要查找其中任何一个节点所在的层次数,教科书中并没有给出基于层次遍历的非递归算法。鉴于层次遍历算法比较容易理解,因此本人基于层次遍历的非递归算法,进行适当改造修改,实现了二叉树中指定节点层次数定位的非递归层次遍历算法。基本数据结构定义、操作与算法实现:/*二叉树结构定于与相关算法*/typedef struc..._查找指定节点在树中的层次

爬虫练习之循环爬取网页中全部链接(requsets同步)_爬取网页中的链接-程序员宅基地

文章浏览阅读2.8w次,点赞14次,收藏95次。先贴代码,之后再写注释,已测试可用import reimport requests# 获取并检验要爬取的网站def url_get(): url = input("请输入要爬取的首页url:") try: kv = {'user_agent': 'Mozilla/5.0'} requests.get(url, headers=kv_爬取网页中的链接

C++ 字符串与字符数组 详解_c++数字组合字符串-程序员宅基地

文章浏览阅读857次,点赞22次,收藏29次。char *本身是一个字符指针变量,但是它既可以指向字符串常量,又可以指向字符串变量,指向的类型决定了对应的字符串能不能改变!

POJ 3532 Resistance 题解《挑战程序设计竞赛》-程序员宅基地

文章浏览阅读155次。为什么80%的码农都做不了架构师?>>> ...

推荐文章

热门文章

相关标签