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

📄 jtputil.java

📁 用java实现的画图程序
💻 JAVA
字号:
package com.sunking.tp.util;import java.io.*;import java.net.*;import java.awt.*;import java.util.*;import javax.swing.*;/** * <p>Title: </p> * <p>Description: 实用工具类</p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a> * @version 1.0 */public class JTPUtil {    /**     *存储图片资源的哈希表     */    private static Hashtable hImages;    /**     *取图片     * @param name 图片文件名     * @return     */    public static ImageIcon getImage(String name) {        if (hImages == null) {            hImages = getImageResource();        }        if (hImages != null) {            return (ImageIcon) hImages.get(name);        }        return null;    }    /**     *从JTP.jar文件里的JTP文件中取得图片资源     * @return     */    private static Hashtable getImageResource() {        try {            String url = "jar:";            url += "file:" + new File("").getAbsolutePath() + "/";            while (url.indexOf(File.separator) != -1) {                int index = url.indexOf(File.separator);                url = url.substring(0, index) + "/" +                    url.substring(index + 1, url.length());            }            url += "JTP.jar!/JTP";            Vector vResult = new Vector();            ObjectInputStream in = new ObjectInputStream(new URL(url).                openStream());            Object obj = in.readObject();            in.close();            return (Hashtable) obj;        }        catch (ClassNotFoundException ex) {            return null;        }        catch (IOException ex) {            return null;        }    }    /**     *生成JTP图象资源文件     * @param argv     */    public static void main(String argv[]) {        Hashtable v = new Hashtable();        File f = new File("images/");        File fs[] = f.listFiles();        for (int i = 0; i < fs.length; i++) {            v.put(fs[i].getName(),                  new javax.swing.ImageIcon(fs[i].getAbsolutePath()));            System.err.println(fs[i].getName());        }        writeObjectToFile("JTP", v);    }    /**     *写入文件     * @param fileName     * @param obj     * @return     */    public static boolean writeObjectToFile(String fileName, Object obj) {        try {            if (! (obj instanceof Serializable))                return false;            ObjectOutputStream out = new ObjectOutputStream(new                FileOutputStream(fileName));            out.writeObject(obj);            out.flush();            out.close();            return true;        }        catch (Exception ex) {            ex.printStackTrace();            return false;        }    }    /**     *取图柄拖拉后的大小.     * @param index  图柄序号     * @param oldRec 原大小     * @param mx   拖动横坐标改变大小     * @param my   拖动纵坐标改变大小     * @return     */    public static Rectangle getNewBounds(int index, Rectangle oldRec, int mx,                                         int my) {        int x = oldRec.x, y = oldRec.y, w = oldRec.width, h = oldRec.height;        switch (index+4) {  //请注意这里加了4以和Cursor中的变量相匹配            case Cursor.SW_RESIZE_CURSOR:                x += mx;                w -= mx;                h += my;                break;            case Cursor.SE_RESIZE_CURSOR:                w += mx;                h += my;                break;            case Cursor.NW_RESIZE_CURSOR:                x += mx;                y += my;                w -= mx;                h -= my;                break;            case Cursor.NE_RESIZE_CURSOR:                y += my;                w += mx;                h -= my;                break;            case Cursor.N_RESIZE_CURSOR:                y += my;                h -= my;                break;            case Cursor.S_RESIZE_CURSOR:                h += my;                break;            case Cursor.W_RESIZE_CURSOR:                x += mx;                w -= mx;                break;            case Cursor.E_RESIZE_CURSOR:                w += mx;                break;            default:                break;        }        if (w < 0) {            x += w;            w = -w;        }        if (h < 0) {            y += h;            h = -h;        }        return new Rectangle(x, y, w, h);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -