0

Hi does anyone know a good way to take a screenshot of a simulation, in such way that you can specify the resolution and get a higher quality image? the only way i can think of is zooming in and stitch multiple image together, but it takes a long time...

update:
I've managed to successfully export the whole area, the magic parameter is the: .setAnimationParameterEnabled(Panel.ANIM_BOUNDS_CLIPPING_XJAL, false)

it will force Anylogic to draw the whole area, and not just the visible area.
But it doesn't always work. I have to run the code, move around the area, zoom in/out and try again. At some point it gets really glitchy, probably because it starts to draw every thing, and then the code works. The problem is that i can't figure out exactly what to do to make it work...

   java.awt.Component alPanel = getExperiment().getPresentation().getPanel();
getExperiment().getPresentation().getPanel().setAnimationParameterEnabled(Panel.ANIM_BOUNDS_CLIPPING_XJAL, false);
getExperiment().getPresentation().setMaximized(false);
getExperiment().getPresentation().setPanelSize(5000, 5000);
java.awt.image.BufferedImage imageExperiment = new java.awt.image.BufferedImage(
    alPanel.getWidth(),
    alPanel.getHeight(),
    java.awt.image.BufferedImage.TYPE_INT_RGB
);

getExperiment().drawPresentation(getExperiment().getPresentation().getPanel(), imageExperiment.createGraphics(), false);
java.awt.Component component = getExperiment().getPresentation().getPanel();

  // call the Component's paint method, using
  // the Graphics object of the image.
  component.paintAll( imageExperiment.getGraphics() ); // alternately use .printAll(..)

try {
    // write the image as a PNG
    javax.imageio.ImageIO.write(
      imageExperiment,
      "png",
      new File("screenshotAnylogic.png"));
} catch(Exception e) {
    e.printStackTrace();
}

2 Answers2

1

Okay... so after a lot of experimentation, i found that the "magic parameter" wasn't as magic as i thought. but this piece of code should be able to create a screenshot that extends the visible area:

public void capturePanel (ShapeGroup p, String fileName) {

        Panel argPanel = p.getPresentable().getPresentation().getPanel();
        BufferedImage capture = new BufferedImage(4000, 4000, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = capture.createGraphics();
        g.setClip( -200, -200,  4000, 4000 );
        p.draw( argPanel, g, null, true );
        g.dispose();

      try {



        ImageIO.write(capture, "png", new File(fileName));



    } catch (IOException ioe) {

        System.out.println(ioe);

    }
}
0

Well, afaik there is no built in method in Anylogic for that.You could try to use Java to realize that though. It is possible to get the Panel that contains the simulation via getExperiment().getPresentation().getPanel() and you can create an image from that. This is explained here for example and the code would look like this:

public static BufferedImage getScreenShot(Component component)
{
  BufferedImage image = new BufferedImage(
  component.getWidth(),
  component.getHeight(),
  BufferedImage.TYPE_INT_RGB
  );
  // call the Component's paint method, using
  // the Graphics object of the image.
  component.paint( image.getGraphics() ); // alternately use .printAll(..)
  return image;
}

public static void saveComponentScreenshot(Component component)
{
  try {
      // write the image as a PNG
      ImageIO.write(
        getScreenShot(component),
        "png",
        new File("screenshot.png"));
    } catch(Exception e) {
      e.printStackTrace();
    }
}

Unfortunately this does not give you the bigger viewport you probably want to have. Maybe the method public final void drawPresentation(Panel panel, java.awt.Graphics2D g, boolean publicOnly) that is available from the Experiment object returned from getExperiment() could help you to draw the simulation on a custom Panel with the wanted dimensions. Pretty hacky but it's all that I can come up with ^^

Community
  • 1
  • 1
T_D
  • 1,688
  • 1
  • 17
  • 28
  • well this got me half the way, thanks! as you mentioned it doesn't solve the bigger view port problem. I've been looking into your suggestion, and I can't find the objects outside the area currently visible. I don't know where Anylogic stores that. So i've been looking at just expanding the current panel to eg. 5000x5000 take the screenshot and then restore it to the previous size. But for some reason i can't set a size larger than my screen eg. my screen height is 1050px and setting the panel larger than that doesn't work :-( – Nikolaj Klitlund Børty May 12 '15 at 08:05