📄 gstyle.java
字号:
package no.geosoft.cc.graphics;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;import java.lang.ref.WeakReference;import java.awt.BasicStroke;import java.awt.Stroke;import java.awt.Color;import java.awt.Font;import java.awt.Paint;import java.awt.GradientPaint;import java.awt.TexturePaint;import java.awt.Rectangle;import java.awt.image.BufferedImage;import java.io.InputStream;import java.io.BufferedInputStream;import java.io.FileInputStream;import javax.imageio.ImageIO;/** * Graphics object apparence properties. * <p> * GStyle can be set on GObject, GSegment and GPositionals (GText, GImage, * GComponent). It is optional, and when unset the style of the parent * component is applied. This is also the case for the individual properties * of the GStyle. * <p> * Example: * * <pre> * // Define a style with foreground and background color set * GStyle style1 = new GStyle(); * style1.setForegroundColor (Color.RED); * style1.setBackgroundColor (Color.BLUE); * * // Create an object and apply style1. * // It will have foreground color RED and background color BLUE * GObject object1 = new GObject(); * object1.setStyle (style1); * * // Create a sub object without style. style1 is inherited from parent * // It will have foreground color RED and background color BLUE * GObject object2 = new GObject(); * object1.add (object2); * * // Define new style with foreground color set * GStyle style2 = new GStyle() * style2.setForegroundColor (Color.YELLOW); * * // Create a sub object and apply style2. * // It will have foreground color YELLOW and background color BLUE * GObject object3 = new GObject(); * object3.setStyle (style2); * object1.add (object3); * </pre> * * * @author <a href="mailto:jacob.dreyer@geosoft.no">Jacob Dreyer</a> */ public class GStyle implements Cloneable{ private static final int MASK_FOREGROUNDCOLOR = 1 << 0; private static final int MASK_BACKGROUNDCOLOR = 1 << 1; private static final int MASK_LINEWIDTH = 1 << 2; private static final int MASK_FONT = 1 << 3; private static final int MASK_FILLPATTERN = 1 << 4; private static final int MASK_LINESTYLE = 1 << 5; private static final int MASK_CAPSTYLE = 1 << 6; private static final int MASK_JOINSTYLE = 1 << 7; private static final int MASK_ANTIALIAS = 1 << 8; private static final int MASK_GRADIENT = 1 << 9; public static final int LINESTYLE_SOLID = 1; public static final int LINESTYLE_DASHED = 2; public static final int LINESTYLE_DOTTED = 3; public static final int LINESTYLE_DASHDOT = 4; public static final int LINESTYLE_INVISIBLE = 5; public static final int FILL_NONE = 0; public static final int FILL_SOLID = 1; public static final int FILL_10 = 2; public static final int FILL_25 = 3; public static final int FILL_50 = 4; public static final int FILL_75 = 5; public static final int FILL_HORIZONTAL = 6; public static final int FILL_VERTICAL = 7; public static final int FILL_DIAGONAL = 8; private final Collection listeners_; private int validMask_; private Color foregroundColor_; private Color backgroundColor_; private int lineWidth_; private float dashPattern_[]; private float dashOffset_; private boolean isLineVisible_; private Font font_; private BufferedImage fillPattern_; private int fillWidth_; private int fillHeight_; private int fillData_[]; private Stroke stroke_; // Java2D repr of line width and style private Paint paint_; // Java2D repr of fill pattern private int capStyle_; private int joinStyle_; private float miterLimit_; private boolean isAntialiased_; private Color gradientColor1_; private Color gradientColor2_; /** * Create a new style object. The style is initially empty, individual * settings are only valid if explicitly set. */ public GStyle() { listeners_ = new ArrayList(); // Flag everything setting as invalid validMask_ = 0; // Default settings for all styles foregroundColor_ = Color.black; backgroundColor_ = null; lineWidth_ = 1; font_ = Font.decode ("dialog"); dashPattern_ = null; fillPattern_ = null; fillData_ = null; paint_ = null; capStyle_ = BasicStroke.CAP_ROUND; joinStyle_ = BasicStroke.JOIN_ROUND; miterLimit_ = (float) 0.0; dashOffset_ = (float) 0.0; isLineVisible_ = true; isAntialiased_ = true; gradientColor1_ = null; gradientColor2_ = null; } /** * Create a new style object based on this. * * @return Clone of this style object. */ public Object clone() { GStyle style = new GStyle(); style.validMask_ = validMask_; style.foregroundColor_ = null; if (foregroundColor_ != null) { style.foregroundColor_ = new Color (foregroundColor_.getRed(), foregroundColor_.getGreen(), foregroundColor_.getBlue(), foregroundColor_.getAlpha()); } style.backgroundColor_ = null; if (backgroundColor_ != null) { style.backgroundColor_ = new Color (backgroundColor_.getRed(), backgroundColor_.getGreen(), backgroundColor_.getBlue(), backgroundColor_.getAlpha()); } style.lineWidth_ = lineWidth_; style.font_ = null; if (font_ != null) { style.font_ = new Font (font_.getName(), font_.getStyle(), font_.getSize()); } style.dashPattern_ = null; if (dashPattern_ != null) { style.dashPattern_ = new float[dashPattern_.length]; for (int i = 0; i < dashPattern_.length; i++) style.dashPattern_[i] = dashPattern_[i]; } style.fillPattern_ = null; // Lazy initialized style.fillData_ = null; if (fillData_ != null) { style.fillData_ = new int[fillData_.length]; for (int i = 0; i < fillData_.length; i++) style.fillData_[i] = fillData_[i]; } style.capStyle_ = capStyle_; style.joinStyle_ = joinStyle_; style.miterLimit_ = miterLimit_; style.dashOffset_ = dashOffset_; style.isLineVisible_ = isLineVisible_; style.isAntialiased_ = isAntialiased_; style.gradientColor1_ = gradientColor1_; style.gradientColor2_ = gradientColor2_; return style; } /** * Update this style based on specified style. Style elements * explicitly set in the specified style is copied to this style, * other elements are ignored. * * @param style Style to update with. */ void update (GStyle style) { if (style.isValid (MASK_FOREGROUNDCOLOR)) setForegroundColor (style.foregroundColor_); if (style.isValid (MASK_BACKGROUNDCOLOR)) setBackgroundColor (style.backgroundColor_); if (style.isValid (MASK_LINEWIDTH)) setLineWidth (style.lineWidth_); if (style.isValid (MASK_FONT)) setFont (style.font_); if (style.isValid (MASK_FILLPATTERN)) { if (style.fillData_ != null) setFillPattern (style.fillWidth_, style.fillHeight_, style.fillData_); else setFillPattern (style.fillPattern_); } if (style.isValid (MASK_LINESTYLE)) { setLineStyle (style.dashPattern_); isLineVisible_ = style.isLineVisible_; } if (style.isValid (MASK_CAPSTYLE)) setCapStyle (style.capStyle_); if (style.isValid (MASK_JOINSTYLE)) setJoinStyle (style.joinStyle_); if (style.isValid (MASK_ANTIALIAS)) setAntialiased (style.isAntialiased_); if (style.isValid (MASK_GRADIENT)) setGradient (style.gradientColor1_, style.gradientColor2_); } /** * Set the specified style element to valid. * * @param validMask Identifier of style element to set (MASK_...) */ private void setValid (int validMask) { validMask_ |= validMask; } /** * Invalidate the specified style element. * * @param validMask Identifier of style element to invalidate (MASK_...) */ private void setInvalid (int validMask) { validMask_ &= ~validMask; } /** * Check if a specified style element is valid. * * @param validMask Identifier of style element to check (MASK_...) * @return True if it is valid, false otherwise. */ private boolean isValid (int validMask) { return (validMask_ & validMask) != 0; } /** * Set foreground color of this style. * * @param foregroundColor New foreground color. */ public void setForegroundColor (Color foregroundColor) { if (foregroundColor == null) foregroundColor = Color.black; foregroundColor_ = foregroundColor; setValid (MASK_FOREGROUNDCOLOR); // The fill pattern might have become invalid if (fillData_ != null) fillPattern_ = null; // Notify all owners notifyListeners(); } /** * Unset foreground color of this style. */ public void unsetForegroundColor() { foregroundColor_ = null; setInvalid (MASK_FOREGROUNDCOLOR); // The fill pattern might hav become invalid if (fillData_ != null) fillPattern_ = null; // Notify all owners notifyListeners(); } /** * Return the current foreground color of this style. * The element is not in use if it is invalid. * * @return Current foreground color. */ public Color getForegroundColor() { return foregroundColor_; } /** * Set background color. * * @param backgroundColor New background color. */ public void setBackgroundColor (Color backgroundColor) { backgroundColor_ = backgroundColor; setValid (MASK_BACKGROUNDCOLOR); // The fill pattern might hav become invalid if (fillData_ != null) fillPattern_ = null; // Notify all owners notifyListeners(); } /** * Unset background color. */ public void unsetBackgroundColor() { backgroundColor_ = null; setInvalid (MASK_BACKGROUNDCOLOR); // The fill pattern might hav become invalid if (fillData_ != null) fillPattern_ = null; // Notify all owners notifyListeners(); } /** * Return current background color of this style. * The element is not in use if it is invalid. * * @return Current background color of this style. */ public Color getBackgroundColor() { return backgroundColor_; } /** * Set line width. * * @param lineWidth New line width. */ public void setLineWidth (int lineWidth) { if (lineWidth < 1) lineWidth = 1; lineWidth_ = lineWidth; setValid (MASK_LINEWIDTH); stroke_ = null; // Notify all owners notifyListeners(); } /** * Unset line width. */ public void unsetLineWidth() { setInvalid (MASK_LINEWIDTH); stroke_ = null; // Notify all owners notifyListeners(); } /** * Return current line width of this style. * The element is not in use if it is invalid. * * @return Current line width of this style. */ public int getLineWidth() { return lineWidth_; } /** * Set font of this style. * * @param font New font. */ public void setFont (Font font) { if (font == null) font = Font.decode ("dialog"); font_ = font; setValid (MASK_FONT); // Notify all owners notifyListeners(); } /** * Unset font of this style. */ public void unsetFont() { setInvalid (MASK_FONT); // Notify all owners notifyListeners(); } /** * Return current font of this style. * The element is not in use if it is invalid. * * @return Current font of this style. */ public Font getFont() { return font_; } /** * Set line end cap style of this style. One of BasicStroke.CAP_ROUND * (default), BasicStroke.CAP_BUTT, or BasicStroke.CAP_SQUARE. * * @param capStyle New line end cap style. */ public void setCapStyle (int capStyle) { capStyle_ = capStyle; setValid (MASK_CAPSTYLE); stroke_ = null; // Notify all owners notifyListeners(); } /** * Unset cap style of this style. */ public void unsetCapStyle() { setInvalid (MASK_CAPSTYLE); // Notify all owners notifyListeners(); } /** * Return current line end cap style of this style. * The element is not in use if it is invalid. * * @return Current line end cap style of this style. */ public int getCapStyle() { return capStyle_; } /** * Set line end join style of this style. * One of BasicStroke.JOIN_BEVEL, BasicStroke.JOIN_MITTER or * BasicStroke.JOIN_ROUND (default). * * @param joinStyle New join style. */ public void setJoinStyle (int joinStyle) { joinStyle_ = joinStyle;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -