保存路径

时间:2015-08-09 23:43:51

标签: java

我创建的代码可以创建打印屏幕并将其保存为imiage,但我真的不知道如何更改保存主项目文件夹中其他文件夹的文件的路径。有什么想法吗?

private static void print(JPanel comp, String nazwa) {

    // Create a `BufferedImage` and create the its `Graphics`
    BufferedImage image = GraphicsEnvironment.getLocalGraphicsEnvironment()
            .getDefaultScreenDevice().getDefaultConfiguration()
            .createCompatibleImage(comp.getWidth(), comp.getHeight());
    Graphics graphics = image.createGraphics();
    // Print to BufferedImage
    comp.paint(graphics);
    graphics.dispose();
    // Output the `BufferedImage` via `ImageIO`

    try {
        ImageIO.write(image, "png", new File(nazwa+".png"));

    } catch (IOException e) {
        e.printStackTrace();
    }
}

2 个答案:

答案 0 :(得分:0)

File构造函数中编写完整路径:

new File("/home/cipek/images/filename.png")

答案 1 :(得分:0)

但是我还有问题。我不确定这是否合适。 cipek-是我的项目名称 images-我想保留图片的文件夹 但是家呢?我重写“home”但它不起作用。我不想给出完整的路径,因为我会在其他计算机上使用这个程序,所以每次都会有不同的路径。