📄 lwtoolkit.java
字号:
/**
* Caption: Zaval Light-Weight Visual Components Library
* $Revision: 2.79 $
* $Date: 2003/08/22 11:24:16 $
*
* @author: Andrei Vishnevsky
* @version: 3.50
*
* Zaval Light-Weight Visual Components Library (LwVCL) is a pure Java
* alternative to humble AWT-based and SWING-based GUI interfaces for
* wide ranges of platforms, including J2SE, PersonalJava and J2ME.
*
* Designed as light-weight but, alternatively to Swing, built separately
* from AWT (not on top of the java.awt library like Swing), the LwVCL is
* the good alternative to highly performant, memory-efficient, flexible
* GUI solution for embedded, stand-alone and applet applications.
*
* For more info on this product read Zaval Light-Weight Visual Components Library Tutorial
* (It comes within this package).
* The latest product version is always available from the product's homepage:
* http://www.zaval.org/products/lwvcl/
* and from the SourceForge:
* http://sourceforge.net/projects/zaval0003/
*
* Contacts:
* Support : support@zaval.org
* Change Requests : change-request@zaval.org
* Feedback : feedback@zaval.org
* Other : info@zaval.org
*
* Copyright (C) 2001-2003 Zaval Creative Engineering Group (http://www.zaval.org)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* (version 2) as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
package org.zaval.lw;import java.awt.*;import java.awt.image.*;import java.util.*;import java.io.*;/** * This class provides set of useful static methods for the lightweight library. */public class LwToolkit{ /** * The horizontal alignment constant. */ public static final int HORIZONTAL = 1; /** * The vertical alignment constant. */ public static final int VERTICAL = 2; /** * The none vertical and none horizontal alignment constant. */ public static final int NONE = 0; /** * The dark blue color definition. */ public static final Color darkBlue = new Color(0, 0, 140); /** * The default font definition. */ public static final Font FONT = new Font("dialog", Font.PLAIN, 12); /** * The default small font definition. */ public static final Font SFONT = new Font("dialog", Font.PLAIN, 10); /** * The default bold font definition. */ public static final Font BFONT = new Font("dialog", Font.BOLD, 12); /** * The default font metrics definition. */ public static final FontMetrics FONT_METRICS = Toolkit.getDefaultToolkit().getFontMetrics(FONT); /** * The default foreground color definition. */ public static final Color FG_COLOR = Color.black; /** * The default background color definition. */ public static final Color BACK_COLOR = new Color(204, 204, 204); /** * The default preferred size definition. */ public static final Dimension PS_SIZE = new Dimension(); private static final int MAX_CASH_SIZE = 500000; private static final Toolkit tool = Toolkit.getDefaultToolkit(); private static final String resources = "rs/"; private static Hashtable images; private static int currentCashSize; private static Component observer = new Canvas(); private static MediaTracker media = new MediaTracker(observer); /** * Creates a desktop instance. * @return a desktop instance. */ public static final LwDesktop createDesktop() { return new LwRoot(); } /** * Gets a desktop container for the specified component. Desktop container is top-level * container where the specified component is resided. * @param <code>c</code> the specified component. * @return a desktop. */ public static LwDesktop getDesktop(LwComponent c) { for (;c != null && !(c instanceof LwDesktop);c = c.getLwParent()); return c==null?null:(LwDesktop)c; } /** * Reads a properties file by the specified path and returns it as java.util.Properties instance. * The specified path points to the properties file relatively "org/zaval/lw" light weight * directory. * @param <code>name</code> the specified path. */ public static Properties getProperties(String name) { InputStream is = LwToolkit.class.getResourceAsStream(name); if (is != null) { Properties p = new Properties(); try { p.load(is); return p; } catch (IOException e) { e.printStackTrace (); } } return null; } /** * Reads an image using the specified path. The specified path points to the image relatively * "org/zaval/lw/rs" light weight resources directory. The method tries to find the image in * the internal cash and if the image is absent, reads the image. The method is supposed to * be used for fetching light weight library resource images. * @param <code>name</code> the specified path. */ public static Image getRsImage(String name){ return getImage(resources + name); } /** * Reads an image by the specified path. The path is relative to org/zaval/lw directory. * @param <code>name</code> the specified path. */ public static Image getImage(String name) { if (images == null) images = new Hashtable(); Image img = (Image)images.get(name); if (img != null) return img; InputStream is = LwToolkit.class.getResourceAsStream(name); if (is == null) return null; ByteArrayOutputStream bo = new ByteArrayOutputStream(); byte[] buf = new byte[512]; int count = 0; try { while ((count = is.read(buf, 0, buf.length))>=0) bo.write(buf, 0, count); img = tool.createImage(bo.toByteArray()); media.addImage(img, 0); media.waitForID(0); if (media.isErrorID(0)) System.out.println ("Cannot load " + (resources + name)); } catch (Exception e) { e.printStackTrace(); return null; } finally { try { if (is != null) is.close(); } catch(Exception ee) {} } if (bo.size() + currentCashSize < MAX_CASH_SIZE) { currentCashSize += bo.size(); images.put(name, img); } return img; } //public static Image getImage(String image) //{ //} /** * Gets the font metrics for the specified font. * @param <code>f</code> the specified font. * @return a font metrics for the specified font. */ public static FontMetrics getFontMetrics(Font f) { return f == null?FONT_METRICS:tool.getFontMetrics(f); } /** * Draws the horizontal dotted line between the (x1,y1) and (x2,y1) coordinates. * @param <code>gr</code> the specified context to be used for drawing. * @param <code>x1</code> the x coordinate of the start of the line. * @param <code>x2</code> the x coordinate of the end of the line. * @param <code>y1</code> the y coordinate of the start and end of the line. */ public static void drawDotHLine(Graphics gr, int x1, int x2, int y1) { for (;x1 <= x2; x1+=2) gr.drawLine (x1, y1, x1, y1); } /** * Draws the vertical dotted line between the (x1,y1) and (x1,y2) coordinates. * @param <code>gr</code> the specified context to be used for drawing. * @param <code>y1</code> the y coordinate of the start of the line. * @param <code>y2</code> the y coordinate of the end of the line. * @param <code>x1</code> the x coordinate of the start and end of the line. */ public static void drawDotVLine(Graphics gr, int y1, int y2, int x1) { for (;y1 <= y2; y1+=2) gr.drawLine (x1, y1, x1, y1); } /** * Draws the dotted outline of the specified rectangle using the current color. * The resulting rectangle will cover an area (w + 1) pixels wide by (h + 1) pixels tall. * @param <code>gr</code> the specified context to be used for drawing. * @param <code>x</code> the x coordinate of the rectangle to be drawn (left top corner). * @param <code>y</code> the y coordinate of the rectangle to be drawn (left top corner). * @param <code>w</code> the width of the rectangle to be drawn. * @param <code>h</code> the height of the rectangle to be drawn. */ public static void drawDotRect(Graphics gr, int x, int y, int w, int h) { int x2 = x + w - 1, y2 = y + h - 1; drawDotVLine(gr, y, y2, x); drawDotHLine(gr, x, x2, y); drawDotVLine(gr, y, y2, x2); drawDotHLine(gr, x, x2, y2); } /** * Returns the immediate child component for the parent and child. * @param <code>parent</code> the parent component. * @param <code>child</code> the child component. * @return an immediate child component. */ public static LwComponent getDirectChild(LwComponent parent, LwComponent child) { for (;child != null && child.getLwParent() != parent; child = child.getLwParent()); return child; } /** * Returns the immediate child component at the specified location of the parent component. * @param <code>x</code> the x coordinate relatively the parent component. * @param <code>y</code> the y coordinate relatively the parent component. * @param <code>p</code> the parent component. * @return an immediate child component. */ public static int getDirectCompAt(int x, int y, LwContainer p) { for (int i=0; i<p.count(); i++) { LwComponent c = (LwComponent)p.get(i); if (c.isVisible()) { Rectangle b = c.getBounds(); if (b.contains(x, y)) return i; } } return -1; } /** * Checks if the component is contained in the component hierarchy of this container. * @param <code>p</code> the specified parent component. * @param <code>c</code> the specified child component. * @return <code>true</code> if the component is contained in the parent component hierarchy; * otherwise <code>false</code>. */ public static boolean isAncestorOf(LwComponent p, LwComponent c) { while (c != null && c != p) c = c.getLwParent(); return c != null; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -