Skip to content

Commit

Permalink
Update of the image loader example code
Browse files Browse the repository at this point in the history
  • Loading branch information
vogella committed Oct 13, 2023
1 parent 2f007ec commit 8eaed77
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
17 changes: 17 additions & 0 deletions com.vogella.service.imageloader/.project
Expand Up @@ -25,9 +25,26 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<filteredResources>
<filter>
<id>1697119637238</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
@@ -1,13 +1,18 @@
package com.vogella.imageloader.services;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;

public interface IBundleResourceLoader {

// service does NOT recycle the provided image
// service does NOT dispose the provided image
// the consumer of the image is responsible for calling the dispose()
// method on the created Image object once the Image is not required anymore
// can be automated with the usage of LocalResourceManager

public ImageDescriptor getImageDescriptor(Class<?> clazz, String path);

// service does NOT dispose the provided image nor the created
// caller needs to dispose them
public Image resize(Image image, int width, int height);
}
Expand Up @@ -5,6 +5,10 @@
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.osgi.service.component.annotations.Component;
Expand All @@ -22,4 +26,15 @@ public ImageDescriptor getImageDescriptor(Class<?> clazz, String path) {
return ImageDescriptor.createFromURL(url);
}

@Override
public Image resize(Image image, int width, int height) {
Image scaled = new Image(Display.getDefault(), width, height);
GC gc = new GC(scaled);
gc.setAntialias(SWT.ON);
gc.setInterpolation(SWT.HIGH);
gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, 0, 0, width, height);
gc.dispose();
// image.dispose(); // caller need to do this
return scaled;
}
}

0 comments on commit 8eaed77

Please sign in to comment.