prefuselib.java

来自「用applet实现很多应用小程序」· Java 代码 · 共 457 行 · 第 1/2 页

JAVA
457
字号
package prefuse.util;

import java.awt.BasicStroke;
import java.awt.Font;
import java.awt.Stroke;
import java.awt.geom.Rectangle2D;

import prefuse.Constants;
import prefuse.Display;
import prefuse.data.Schema;
import prefuse.visual.VisualItem;

/**
 * General library routines used by the prefuse toolkit.
 * 
 * @author <a href="http://jheer.org">jeffrey heer</a>
 */
public class PrefuseLib {

    /**
     * Group delimiter string used to demarcate the hierarchical structure
     * of nested data groups; the default value is a dot (.).
     */
    private static final String GROUP_DELIMITER 
        = PrefuseConfig.get("data.delimiter");
    /**
     * Scale factor for psychophysical scaling of area values, defaults to
     * 0.5, resulting in linear scaling.
     */
    private static final double SIZE_SCALE_FACTOR 
        = PrefuseConfig.getDouble("size.scale2D");
    /**
     * Prefix for visualization-specific data fields, such as those used by
     * VisualItem; the default is a single underscore (_).
     */
    public static final String FIELD_PREFIX
        = PrefuseConfig.get("data.visual.fieldPrefix");
    
    // ------------------------------------------------------------------------
    
    private PrefuseLib() {
        // prevent instantiation
    }
    
    // ------------------------------------------------------------------------
    // Memory Usage / Debugging Output
    
    /**
     * Get a String showing current JVM memory usage in kilobytes.
     * @return the memory usage String in KB
     */
    public static String getMemoryUsageInKB() {
        long total = Runtime.getRuntime().totalMemory() / (2<<10);
        long free  = Runtime.getRuntime().freeMemory() / (2<<10);
        long max   = Runtime.getRuntime().maxMemory() / (2<<10);
        return "Memory: "+(total-free)+"k / "+total+"k / "+max+"k";
    }

    /**
     * Get a String showing current JVM memory usage in megabytes.
     * @return the memory usage String in MB
     */
    public static String getMemoryUsageInMB() {
        long total = Runtime.getRuntime().totalMemory() / (2<<20);
        long free  = Runtime.getRuntime().freeMemory() / (2<<20);
        long max   = Runtime.getRuntime().maxMemory() / (2<<20);
        return "Memory: "+(total-free)+"M / "+total+"M / "+max+"M";
    }
    
    /**
     * Returns a string showing debugging info such as number of visualized
     * items and the current frame rate.
     * @return the debug string
     */
    public static String getDisplayStats(Display d) {
        float fr = Math.round(d.getFrameRate()*100f)/100f;
        
        Runtime rt = Runtime.getRuntime();
        long tm = rt.totalMemory() / (2<<20);
        long fm = rt.freeMemory() / (2<<20);
        long mm = rt.maxMemory() / (2<<20);
        
        StringBuffer sb = new StringBuffer();
        sb.append("frame rate: ").append(fr).append("fps - ");
        sb.append(d.getVisibleItemCount()).append(" items - ");
        sb.append("fonts(").append(FontLib.getCacheMissCount());
        sb.append(") colors(");
        sb.append(ColorLib.getCacheMissCount()).append(')');
        sb.append(" mem(");
        sb.append(tm-fm).append("M / ");
        sb.append(mm).append("M)");
        sb.append(" (x:");
        sb.append(StringLib.formatNumber(d.getDisplayX(),2));
        sb.append(", y:");
        sb.append(StringLib.formatNumber(d.getDisplayY(),2));
        sb.append(", z:");
        sb.append(StringLib.formatNumber(d.getScale(),5)).append(")");
        return sb.toString();
    }
    
    // ------------------------------------------------------------------------
    // VisualItem Methods
    
    /**
     * Returns a scale factor by which to scale a 2D shape to grow 
     * the area by the desired input size value. This is used to scale shapes
     * by total pixel area, rather than scaling each dimension by the
     * size value itself, which grows the pixel area quadratically rather
     * than linearly.
     */
    public static double getSize2D(double size) {
        return Math.pow(size, SIZE_SCALE_FACTOR);
    }
    
    /**
     * Get the distance between the x,y points of two VisualItems.
     * @param vi1 the first VisualItem
     * @param vi2 the second VisualItem
     * @return the distance between the items' x,y coordinates
     */
    public static double distance(VisualItem vi1, VisualItem vi2) {
        double dx = vi1.getX() - vi2.getX();
        double dy = vi1.getY() - vi2.getY();
        return Math.sqrt(dx*dx + dy*dy);
    }
    
    /**
     * Update the values in an interpolated column (a set of three columns
     * representing a current value along with starting and ending values).
     * The current value will become the new starting value, while the given
     * value will become the new current and ending values.
     * @param item the VisualItem to update
     * @param field the base field to update, start and ending fields will
     * also be updated, too.
     * @param val the value to set
     */
    public static void update(VisualItem item, String field, Object val) {
        item.set(getStartField(field), item.get(field));
        item.set(field, val);
        item.set(getEndField(field), val);
    }
    
    /**
     * Update the values in an interpolated column (a set of three columns
     * representing a current value along with starting and ending values).
     * The current value will become the new starting value, while the given
     * value will become the new current and ending values.
     * @param item the VisualItem to update
     * @param field the base field to update, start and ending fields will
     * also be updated, too.
     * @param val the value to set
     */
    public static void updateInt(VisualItem item, String field, int val) {
        item.setInt(getStartField(field), item.getInt(field));
        item.setInt(field, val);
        item.setInt(getEndField(field), val);
    }
    
    /**
     * Update the values in an interpolated column (a set of three columns
     * representing a current value along with starting and ending values).
     * The current value will become the new starting value, while the given
     * value will become the new current and ending values.
     * @param item the VisualItem to update
     * @param field the base field to update, start and ending fields will
     * also be updated, too.
     * @param val the value to set
     */
    public static void updateLong(VisualItem item, String field, long val) {
        item.setLong(getStartField(field), item.getLong(field));
        item.setLong(field, val);
        item.setLong(getEndField(field), val);
    }
    
    /**
     * Update the values in an interpolated column (a set of three columns
     * representing a current value along with starting and ending values).
     * The current value will become the new starting value, while the given
     * value will become the new current and ending values.
     * @param item the VisualItem to update
     * @param field the base field to update, start and ending fields will
     * also be updated, too.
     * @param val the value to set
     */
    public static void updateFloat(VisualItem item, String field, float val)
    {
        item.setFloat(getStartField(field), item.getFloat(field));
        item.setFloat(field, val);
        item.setFloat(getEndField(field), val);
    }
    
    /**
     * Update the values in an interpolated column (a set of three columns
     * representing a current value along with starting and ending values).
     * The current value will become the new starting value, while the given
     * value will become the new current and ending values.
     * @param item the VisualItem to update
     * @param field the base field to update, start and ending fields will
     * also be updated, too.
     * @param val the value to set
     */
    public static void updateDouble(VisualItem item, String field, double val)
    {
        item.setDouble(getStartField(field), item.getDouble(field));
        item.setDouble(field, val);
        item.setDouble(getEndField(field), val);
    }
    
    /**
     * Update the values in an interpolated column (a set of three columns
     * representing a current value along with starting and ending values).
     * The current value will become the new starting value, while the given
     * value will become the new current and ending values.
     * @param item the VisualItem to update
     * @param field the base field to update, start and ending fields will
     * also be updated, too.
     * @param b the value to set
     */
    public static void updateBoolean(VisualItem item, String field, boolean b)
    {
        item.setBoolean(getStartField(field), item.getBoolean(field));
        item.setBoolean(field, b);
        item.setBoolean(getEndField(field), b);
    }
    
    /**
     * Update the visibility of an item. The current visibility will become the
     * new starting visibility, while the given visibility value will become
     * the new current and ending visibility.

⌨️ 快捷键说明

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