Java 创建BuffereImage只保存面板背景,而不保存在其上绘制的内容

Java 创建BuffereImage只保存面板背景,而不保存在其上绘制的内容,java,swing,Java,Swing,我有一个叫做drawPanel的JPanel对象。我在它上面画了各种各样的东西,比如矩形,当我尝试创建一个BuffereImage并将其保存为以下内容时,它只保存一个空白图像,只有背景色,而不是绘制在框架上的矩形 BufferedImage image = createImage(drawPanel); File outputfile = new File("MyImage.jpg"); try { ImageIO.write(image, "jpg", outputfile); } c

我有一个叫做drawPanel的JPanel对象。我在它上面画了各种各样的东西,比如矩形,当我尝试创建一个BuffereImage并将其保存为以下内容时,它只保存一个空白图像,只有背景色,而不是绘制在框架上的矩形

BufferedImage image = createImage(drawPanel);
File outputfile = new File("MyImage.jpg");
try {
    ImageIO.write(image, "jpg", outputfile);
} catch (IOException e) {
     e.printStackTrace();
}



public BufferedImage createImage(JPanel panel) {
    int w = panel.getWidth();
    int h = panel.getHeight();
    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bi.createGraphics();
    panel.print(g);
    return bi;
}
请帮助我解决此问题。

此Graphics2D g2=Graphics2D drawPanel.getGraphics;这是你的问题。调用print、printAll或paint将使用getGraphics擦除组件上绘制的任何内容

简而言之,永远不要使用它。很长的答案是,创建一个自定义组件,它从类似JPanel的东西扩展而来,重写它的paintComponent方法,并在调用它时在其中执行所有自定义绘制

有关更多详细信息,请参见和

此Graphics2D g2=Graphics2D drawPanel.getGraphics;这是你的问题。调用print、printAll或paint将使用getGraphics擦除组件上绘制的任何内容

简而言之,永远不要使用它。很长的答案是,创建一个自定义组件,它从类似JPanel的东西扩展而来,重写它的paintComponent方法,并在调用它时在其中执行所有自定义绘制

有关更多详细信息,请参见和 只需将您的方法createImage替换为我的方法即可:-

public BufferedImage createImage(JPanel panel) {
    //Get top-left coordinate of drawPanel w.r.t screen
    Point p = new Point(0, 0);
    SwingUtilities.convertPointToScreen(p, panel);

    //Get the region with wiht and heighht of panel and 
    // starting coordinates of p.x and p.y
    Rectangle region = panel.getBounds();
    region.x = p.x;
    region.y = p.y;
    
    //Get screen capture over the area of region
    BufferedImage bi = null;
    try {
        bi = new Robot().createScreenCapture( region );
    } catch (AWTException ex) {
        Logger.getLogger(MyPaintBrush.class.getName()).log(Level.SEVERE, null, ex);
    }
    
    return bi;
}
一个机器人小黑客 只需将您的方法createImage替换为我的方法即可:-

public BufferedImage createImage(JPanel panel) {
    //Get top-left coordinate of drawPanel w.r.t screen
    Point p = new Point(0, 0);
    SwingUtilities.convertPointToScreen(p, panel);

    //Get the region with wiht and heighht of panel and 
    // starting coordinates of p.x and p.y
    Rectangle region = panel.getBounds();
    region.x = p.x;
    region.y = p.y;
    
    //Get screen capture over the area of region
    BufferedImage bi = null;
    try {
        bi = new Robot().createScreenCapture( region );
    } catch (AWTException ex) {
        Logger.getLogger(MyPaintBrush.class.getName()).log(Level.SEVERE, null, ex);
    }
    
    return bi;
}

为了更快地获得更好的帮助,请发布一个最简单、完整、可验证的示例或简短、独立的示例,正确的例子。也许除了panel.printg之外,还可以使用panel.paintg解决您的问题problem@AlexColeman:我真的认为printAllg会更好——如果他也想画子组件的话。@HovercraftFullOfEels听起来可能更好,没有打开编译器,所以我只是尝试一下,让他试试:PHow drawPanel能工作吗?为了更快地获得更好的帮助,请发布一个最小的完整的可验证示例或简短的自包含示例,正确的例子。也许除了panel.printg之外,还可以使用panel.paintg解决您的问题problem@AlexColeman:我真的认为printAllg会更好——如果他也想画子组件的话。@HovercraftFullOfEels听起来可能更好,没有打开编译器,所以我只是试着让他试试:抽绘图板能用吗?哦,老兄…我花了一整天写这段代码…:你能在现有代码中做些小修改吗?哦,老兄…我花了一整天写这段代码…:你能在现有代码中做些小修改吗?这是迄今为止最大的帮助我在这个网站上见过。加里,我真是太感谢你了。你太棒了这是迄今为止我在这个网站上得到的最大帮助。加里,我真是太感谢你了。你太棒了