Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract Image loading to another util object.
- Loading branch information
Showing
7 changed files
with
162 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.bone.ircballoon | ||
|
||
import org.eclipse.swt.SWT | ||
import org.eclipse.swt.widgets.Display | ||
import org.eclipse.swt.graphics.Image | ||
import org.eclipse.swt.graphics.GC | ||
|
||
import scala.util.control.Exception._ | ||
|
||
object ImageUtil | ||
{ | ||
import java.net.URL | ||
|
||
lazy val display = Display.getDefault | ||
|
||
def loadFromResource(path: String) = allCatch.opt { | ||
new Image(display, getClass().getResourceAsStream(path)) | ||
} | ||
|
||
def loadFromFile(filePath: String) = allCatch.opt { | ||
new Image(display, filePath) | ||
} | ||
|
||
def loadFromURL(url: String) = allCatch.opt { | ||
val inputStream = new URL(url).openStream | ||
val image = new Image(Display.getDefault, inputStream) | ||
inputStream.close() | ||
image | ||
} | ||
|
||
def resize(image: Image, size: (Int, Int)) = | ||
{ | ||
val (width, height) = size | ||
val scaled = new Image(display, width, height) | ||
val 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() | ||
|
||
scaled | ||
} | ||
} | ||
|
Oops, something went wrong.