Beanshell:Loading an Image into the task

From OpenJUMP Wiki
Jump to: navigation, search

This script will load in image into the current task. Change minx, maxx, miny, maxy and filenameOrURL as desired.


{
minx = 100; maxx = 376; miny = 100; maxy = 210; //Image coordinates
filenameOrURL = new URL("http://www.google.ca/intl/en_ca/images/logo.gif");
//filenameOrURL = "c:/junk/Arrow.jpg";
layerName = "Image";
image = Toolkit.getDefaultToolkit().getImage(filenameOrURL);
import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jump.feature.*;
import com.vividsolutions.jump.geom.EnvelopeUtil;
import com.vividsolutions.jump.workbench.model.*;
import com.vividsolutions.jump.workbench.ui.renderer.style.Style;
import com.vividsolutions.jump.workbench.ui.Viewport;
mediaTracker = new MediaTracker(wc.layerViewPanel);
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
wc.layerManager.addLayer(StandardCategoryNames.WORKING, layerName, 
      FeatureDatasetFactory.createFromGeometry(
      Collections.singleton(EnvelopeUtil.toGeometry(
      new Envelope(minx, maxx, miny, maxy))))).addStyle(new Style() {
  public void paint(Feature f, Graphics2D g, Viewport viewport) {    
    originalTransform = g.getTransform();
    try {    
      envelope = f.geometry.envelope;
      xScale = viewport.scale*envelope.width/image.getWidth(null);
      yScale = viewport.scale*envelope.height/image.getHeight(null);
      g.scale(xScale, yScale);
      upperLeftCorner = viewport.toViewPoint(new
 Coordinate(envelope.minx, envelope.maxy));
      g.translate(upperLeftCorner.x/xScale, upperLeftCorner.y/yScale);
      g.drawImage(image, 0, 0, null);    
    } 
    finally {
        g.setTransform(originalTransform);  
    }
  }
  public void initialize(Layer layer) {}
  public Object clone() { throw new UnsupportedOperationException(); }
  public void setEnabled(boolean enabled) {}
  public boolean isEnabled() { return true; }
});
wc.layerManager.getLayer(layerName).getBasicStyle().renderingFill = false;
wc.layerManager.getLayer(layerName).getBasicStyle().renderingLine = false;
}