?? dmstandard.java
字號:
package demo;
import java.io.IOException;
import java.io.InputStream;
import sun.audio.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.graphics.Point;
/**
* <p>Title: DmStandard</p>
*
* <p>Description: 提供一些基本方法, 如 獲取 當前客戶屏幕區 Display 圖像 等</p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: Duomy.com</p>
*
* @author Javen Wong, javenwong@hotmail.com
* @version 1.0
*/
public class DmStandard{
//一些路徑常量
private static final String HEAD_PATH_PREFIX = "/image/NEWFACE/";
private static final String FACE_PATH_PREFIX = "/image/Face/";
private static final String INTERFACE_PATH_PREFIX = "/image/PopFace/";
private static final String IMAGE_PATH_PREFIX = "/image/";
private static final String APP_ICON_FILENAME = "Status_0.ico";
private static final String SOUND_PATH_PREFIX = "/sound/";
private static final String SOUND_ONLINE = "online.wav";
private static final String SOUND_MESSAGE = "message.wav";
private static final String SOUND_NORMAL = "normal.wav";
/**
* 獲取當前可用的屏幕大小
* @return Point
*/
public static Point getClientArea(){
Display display = getDisplay();
return new Point(display.getClientArea().width, display.getClientArea().height);
}
/**
* 得到窗體居中的位置 注,這里得到的是在當前可用屏幕大小下得到的(拋開任務欄)
* @param shell
* @return Point
*/
public static Point getMyCenterPoint( Shell shell ){
Display display = getDisplay();
Monitor primary = display.getPrimaryMonitor ();
Rectangle bounds = primary.getBounds ();
Rectangle rect = shell.getBounds ();
int x = bounds.x + (getClientArea().x - rect.width) / 2;
int y = bounds.y + (getClientArea().y - rect.height) / 2;
return new Point(x,y);
}
/**
* 得到窗體靠右上邊的位置
* @param shell
* @return Point
*/
public static Point getMyRightPoint( Shell shell ){
Display display = getDisplay();
Monitor primary = display.getPrimaryMonitor ();
Rectangle bounds = primary.getBounds ();
Rectangle rect = shell.getBounds ();
/*
int x = bounds.x + (getClientArea().x - rect.width) / 2;
int y = bounds.y + (getClientArea().y - rect.height) / 2;
*/
int x = bounds.x + getClientArea().x - rect.width;
int y = bounds.y;
return new Point(x,y);
}
/**
* 獲取當前默認Display的方法
* @return Display
*/
public static Display getDisplay(){
return Display.getDefault();
}
/**
* 從文件創建Image對象
*
* @param path
* the relative path to the icon
*/
private static Image createImageFromJar(Display d,String path) {
InputStream is = DmStandard.class.getResourceAsStream(path);
return createImageFromStream(d,is);
}
/**
* 從一個輸入流中創建一個圖片
*
* @param is
* @return
*/
private static Image createImageFromStream(Display d,InputStream is) {
try {
if (is != null) {
ImageData imageData = new ImageData(is);
if (imageData != null) {
//ImageData mask = imageData.getTransparencyMask();
//return new Image(d, imageData, mask);
return new Image(d, imageData);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(is != null)
is.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
return null;
}
/**
* 獲取 image/ 下的圖像
* @param d
* @param imageName
* @return Image
*/
public static Image getImage( Display d, String imageName) {
/*
String fileName = DmStandard.class.getClassLoader().getResource(
"image/" + imageName).getFile();
try{
fileName = java.net.URLDecoder.decode(fileName, "UTF-8");
} catch (Exception ex){
ex.printStackTrace();
}
return new Image( Display.getCurrent(), fileName);
*/
return createImageFromJar( d,IMAGE_PATH_PREFIX+imageName );
}
/**
* 獲取頭像
* @param d
* @param imageName
* @return Image
*/
public static Image getNewFaceImage( Display d, String imageName) {
/*
String fileName = (DmStandard.class.getClassLoader().getResource(
"image/NEWFACE/" + imageName + ".bmp")).getFile();
try{
fileName = java.net.URLDecoder.decode(fileName, "UTF-8");
} catch (Exception ex){
ex.printStackTrace();
}
return new Image( Display.getCurrent(), fileName);
*/
return createImageFromJar( d,HEAD_PATH_PREFIX+imageName+".bmp" );
}
/**
* 獲取表情
* @param d
* @param imageName
* @return Image
*/
public static Image getFaceImage( Display d, String imageName) {
/*
String fileName = (DmStandard.class.getClassLoader().getResource(
"image/Face/" + imageName + ".gif")).getFile();
try{
fileName = java.net.URLDecoder.decode(fileName, "UTF-8");
} catch (Exception ex){
ex.printStackTrace();
}
return new Image( Display.getCurrent(), fileName);
*/
return createImageFromJar( d,FACE_PATH_PREFIX+imageName+".gif" );
}
/**
* 獲取界面圖片
* @param d
* @param imageName
* @return Image
*/
public static Image getInterfaceImage( Display d, String imageName) {
/*
String fileName = (DmStandard.class.getClassLoader().getResource(
"image/PopFace/" + imageName)).getFile();
try{
fileName = java.net.URLDecoder.decode(fileName, "UTF-8");
} catch (Exception ex){
ex.printStackTrace();
}
return new Image( Display.getCurrent(), fileName);
*/
return createImageFromJar( d,INTERFACE_PATH_PREFIX+imageName );
}
/**
* 獲取應用程序圖標
*/
public static Image getAppIcon( Display d ){
return createImageFromJar( d,INTERFACE_PATH_PREFIX+APP_ICON_FILENAME );
}
public static InputStream getSoundFromJar(Display d, String path){
return DmStandard.class.getResourceAsStream(path);
}
/**
* 播放消息聲音
*
*/
public static void playNormalSound(){
try {
AudioStream as = new AudioStream(getSoundFromJar(Display.getDefault(), SOUND_PATH_PREFIX+SOUND_NORMAL));
AudioPlayer.player.start(as);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -