Image圖片資源獲取及釋放工具類,eclipse開發(fā)避免不了使用大量的圖片資源,CacheImage可以幫我們完成這項(xiàng)工作,使用起來相當(dāng)方便。

import ?java.util.HashMap;
import
?java.util.Iterator;
import
?java.util.Map;
import
?org.eclipse.swt.graphics.Image;
import
?org.eclipse.ui.plugin.AbstractUIPlugin;

public ? class ?CacheImage?
{
?
private ? final ?Map < String,?Image > ?imageMap? = ? new ?HashMap < String,?Image >
();

?
private ? static
?CacheImage?INSTANCE;

?
private ?CacheImage()?
{
?}


?
// ?單例模式,獲得CacheImage實(shí)例
? public ? static ?CacheImage?getINSTANCE()? {
??
if ?(INSTANCE? == ? null
)
???INSTANCE?
= ? new
?CacheImage();
??
return
?INSTANCE;
?}


?
// ?獲得圖像
? public ?Image?getImage(String?applicationID,String?imageName)? {
??
if ?(imageName? == ? null
)
???
return ? null
;
??Image?image?
=
?(Image)?imageMap.get(imageName);
??
if ?(image? == ? null )?
{
???image?
=
AbstractUIPlugin.imageDescriptorFromPlugin(
?????applicationID,imageName).createImage();
???imageMap.put(imageName,?image);
??}

??
return ?image;
?}


?
// ?釋放圖像資源
? public ? void ?dispose()? {
??Iterator?iterator?
=
?imageMap.values().iterator();
??
while
?(iterator.hasNext())
???((Image)?iterator.next()).dispose();
??imageMap.clear();
?}

}



窗口居中工具類

import ?org.eclipse.swt.graphics.Rectangle;
import
?org.eclipse.swt.widgets.Display;
import
?org.eclipse.swt.widgets.Shell;

public ? class ?CenterUtil?
{
????
public ? static ? void ?centerShell(Display?display,Shell?shell)
{
?????????Rectangle?displayBounds?
=
?display.getPrimaryMonitor().getBounds();
?????????Rectangle?shellBounds?
=
?shell.getBounds();
?????????
int ?x? = ?displayBounds.x? + ?(displayBounds.width? - ?shellBounds.width) >> 1
;
?????????
int ?y? = ?displayBounds.y? + ?(displayBounds.height? - ?shellBounds.height) >> 1
;
?????????shell.setLocation(x,?y);
?????}

?}