2


I'm working on a Java/Eclipse SWT application that displays and edits map data captured by a special device in a stacked fashion, i.e. there are different layers of "geospatial features" that can be shown/hidden or modified. It was found to be helpful to have an aerial imagery layer which could be easily retrieved e.g. from google maps.

I thought of using the SWT Browser Widget to retrieve and render this satellite view, which actually works like a charm. The Problem is that I need to have a hidden Browser Widget which would work in the background and return me a swt.graphics.Image etc. of the rendered content or even better directly use a given GC for drawing.

I also thought about simpler solutions but there are two restrictions:

  1. I can't just use static maps from Google because the map tile I need would have to be larger than they allow and the partial reloading that they provide (e.g. when moving the map view port) would also be very handy.
  2. I can't simply feed my data into Google Maps for several reasons.

So in general: How do I have a (hidden) instance of a Browser Widget render its output to an Image/GC instead of the screen. Is there something else except from the Browser Widget which could do the job?

Baz
  • 36,440
  • 11
  • 68
  • 94
Tom
  • 21
  • 2

2 Answers2

1

I think you can use the org.eclipse.swt.widgets.Control.print(GC) method to print a control to an image. I have not tried it for Browser though.

Gorkem Ercan
  • 3,220
  • 1
  • 18
  • 26
1

Here is sample to start with using SWT Browser control

  public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Browser Test");
    shell.setSize(500, 500);
    shell.setLayout(new GridLayout(1,false));

    final Browser browser = new Browser(shell, SWT.NONE);

    browser.setUrl("https://maps.google.com/maps?hl=en&tab=wl");
    //browser.setVisible(false);
    browser.setLayoutData(new GridData(GridData.FILL_BOTH));

    Button b = new Button(shell, SWT.NONE);
    b.setText("Show");
    b.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event event) {


            Image img = new Image(display, 500, 500);
            GC gc = new GC(img);
            browser.print(gc);
            gc.dispose();

            showImage(img);

        }
    });

    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }


private static class ImageDialog extends Dialog
{

    private Image img;

    protected ImageDialog(Shell parentShell,Image img) {
        super(parentShell);
        this.img = img;
    }
    @Override
    protected Control createDialogArea(Composite parent) {

        Composite comp =  (Composite) super.createDialogArea(parent);

        Label lbl = new Label(comp,SWT.NONE);

        lbl.setImage(img);

        return comp;
    }

    @Override
    protected void okPressed() {
        img.dispose();
        super.okPressed();
    }

}
protected static void showImage(Image img) {

    ImageDialog dialog = new ImageDialog(Display.getDefault().getActiveShell(), img);
    dialog.open();

}

another approach that we can think of

user capture div as image and save to computer

execute java script into SWT Browser Browser.execute(java script) to capture div into an image.

Community
  • 1
  • 1
sambi reddy
  • 3,065
  • 1
  • 13
  • 10