Sg2.9
Placing images from URL?
Recent RSS
Placing images from URL?
Date:  9. May 2007, 11:14

I'm trying to place an image from an URL.
I came up with something like this:

importPackage(java.net);
importPackage(java.awt);

var url = "http://www.mydomain.com/my_image.jpg";
var image = Toolkit.getDefaultToolkit().getDefaultToolkit().createImage(url);

var my_image = new PlacedItem(image);
my_image.moveTo(100,100);

When I print out the image variable it gives me a java-reference wich gives me the impression the image has been loaded:

sun.awt.image.ToolkitImage@186f3b3

But as it seems PlacedItem() doesn't work like I thought it would.. the above script gives me the following error:

ReferenceError: "PlacedItem" is not defined.

    at scriptsgetImageFromURL.js:7

can anyone tell me how to place the image in the document?

I'm running Sg 2.0.0.20 From wich I removed the java.vm file to make it run on my Win XP / CS2 (as proposed somewhere on these forums)

  • --------

edit:

I found the place() and paste() commands, but do I need to construct the image first from the java-reference before being able to place/paste it?

Re: Placing images from URL?
Date:  10. May 2007, 12:36

As far as I can tell you're not actually creating an image from the url..
I think you should be using: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Toolkit.html#createImage(java.net.URL)

And I would look at the Raster item -> new Raster(image)
It also accepts java.awt images..

Re: Placing images from URL?
From:  Jürg Lehni
Date:  11. May 2007, 22:45

Hi,

There are a few issues with your code, but the basic direction is right.

You need to wait for the image to load though, as it is loaded asynchronously. The easiest way to do this is by using the MediaTracker class. Also, passing a string to createImage assumes this is a local filename, not a URL.

PlacedItem seems not be exposed, which is a bug in Scriptographer that I will fix. You can use it through Packages.com.scriptographer.ai.PlacedItem. Or reimport the whole package:

importPackage(Packages.com.scriptographer.ai);

This code here works from me. But I will add a constructor to Raster that does this behind the scene, and I will also look into PlacedItem. I think this only supports local files though...

var img = java.awt.Toolkit.getDefaultToolkit().createImage(new java.net.URL("http://www.scriptographer.com/static/img/sg-red.gif"));
var tracker = new java.awt.MediaTracker(new java.awt.Container());
tracker.addImage(img, 0);
tracker.waitForAll();
var raster = new Raster(img);
Re: Placing images from URL?
Date:  12. May 2007, 01:18

Doh - I missed the scrollbars before - I thought the code ended at getDefaultToolkit()..

Re: Placing images from URL?
Date:  14. May 2007, 19:01

thanks, both of you.
Lehni, the code worked perfectly :)

Scripts
08.08.14, 15:24
15.05.14, 14:23
02.03.14, 19:16
18.11.13, 14:48
22.03.13, 03:05
22.02.13, 15:45
Posts
10.01.17, 16:37
19.02.16, 06:03
19.02.16, 06:00
17.01.16, 11:00
12.01.16, 13:10
25.11.15, 08:19
Script of the Moment
Transform Each 08.01.12