⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dmstandard.java

📁 用java写的一个浏览器
💻 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 + -