java图像输出错误

标签 java eclipse

这个应用程序并没有真正的用途,但我习惯于作为练习,因为我是新手哈哈。我已经成功地将屏幕截图保存到设定位置,但现在当我尝试添加一个功能以使用控制台设置输出位置时,应用程序似乎在我输入 1 或 0 后就结束了。

public class Mainclass {

    public static void main(String[] args) throws IOException,
            HeadlessException, AWTException {
        Scanner scan = new Scanner(System.in);
        String login = System.getenv().get("USERNAME");
        String desk = "/Desktop/lol/";
        int num = 0;
        int numd = 100000;
        int dd = 10000000;
        int dl = 999999;
        String start = null;
        String location = "C:/users/" + login + desk + num + ".png";
        int loc = 1;
        System.out.println("Enter a new custom location to print image");
        // the if statment is used to make it possible to change the image
        // location output
        loc = scan.nextInt();
        if (loc == (1)) {
            location = scan.nextLine();
            start = scan.nextLine();
        } else {
            location = "C:/users/" + login + desk + num + ".png";
            start = scan.nextLine();
        }
        while (start.equals("start")) {
            num++;
            numd--;
            dd++;
            dl--;
            BufferedImage image = new Robot()
                .createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit()
                    .getScreenSize()));
            ImageIO.write(image, "png", new File(location));
            ImageIO.write(image, "png", new File("C:/users/" + login + desk
                + num + ".png"));
            ImageIO.write(image, "png", new File("C:/users/" + login + desk
                + numd + ".png"));
            ImageIO.write(image, "png", new File("C:/users/" + login + desk
                + dd + ".png"));
            ImageIO.write(image, "png", new File("C:/users/" + login + desk
                + dl + ".png"));
            JFrame window = new JFrame();
            window.pack();
            window.setSize(1920, 1080);
            window.setResizable(false);
            window.setLocationRelativeTo(null);
            window.setVisible(true);
        }
    }
}

最佳答案

那么您眼前的问题可以通过以下方式解决:

    loc = scan.nextInt();
    String discard = scan.nextLine(); // add this as the new line stays in input
    if (loc == 1) {
        location = scan.nextLine();
        start = scan.nextLine();
    } else {
        location = "C:/users/" + login + desk + num + ".png";
        start = scan.nextLine();
    }

现在您这样做的方式会向无限窗口发送垃圾邮件 - 这就是您想要的吗?

关于java图像输出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34106989/

相关文章:

java - 在 Eclipse 中,如何拥有与项目相关的启动配置?

eclipse - 在 tomcat 中构建和部署应用程序期间,Web 应用程序目录和数据丢失

eclipse - Birt:在 birt 查看器中将 rptlibrary 文件放在哪里?

java - eclipse中的冗余 super 接口(interface)警告

eclipse - 缺少...的 POM,没有可用的依赖信息

java - 无法正确设置组件大小

java - 如何将具有小数、负小数的字符串解析为整数?

java - JPA:查找所有具有一组通用标签的文章

java - Mockito 可以在不考虑参数的情况下 stub 方法吗?

java - 为Android直播音频流