📄 gstyle.java
字号:
setValid (MASK_JOINSTYLE); stroke_ = null; // Notify all owners notifyListeners(); } /** * Unset join style of this style. */ public void unsetJoinStyle() { setInvalid (MASK_JOINSTYLE); // Notify all owners notifyListeners(); } /** * Return current join style of this style. * The element is not in use if it is invalid. * * @return Current join style of this style. */ public int getJoinStyle() { return joinStyle_; } /** * Set antialising flag of this style. * * @param isAntialiased Antialiasing on (true) or off (false) (default). */ public void setAntialiased (boolean isAntialiased) { isAntialiased_ = isAntialiased; setValid (MASK_ANTIALIAS); // Notify all owners notifyListeners(); } /** * Unset antialias flag. */ public void unsetAntialias() { setInvalid (MASK_ANTIALIAS); // Notify all owners notifyListeners(); } /** * Return current antialiasing setting of this style. * The element is not in use if it is invalid. * * @return True if antialiasing on, false otherwise. */ public boolean isAntialiased() { return isAntialiased_; } /** * TODO: This code is experimental and should not yet be used. * * @param color1 * @param color2 */ public void setGradient (Color color1, Color color2) { gradientColor1_ = color1; gradientColor2_ = color2; setValid (MASK_GRADIENT); paint_ = null; // Notify all owners notifyListeners(); } public void unsetGradient() { setInvalid (MASK_GRADIENT); // Notify all owners notifyListeners(); } /** * Set predefined fill pattern of this style. * * @param fillType New fill pattern. */ public void setFillPattern (int fillType) { int width; int height; switch (fillType) { case FILL_NONE : width = 0; height = 0; break; case FILL_SOLID : width = 1; height = 1; break; case FILL_10 : width = 3; height = 3; break; case FILL_25 : width = 2; height = 2; break; case FILL_50 : width = 2; height = 2; break; case FILL_75 : width = 2; height = 2; break; case FILL_HORIZONTAL : width = 1; height = 2; break; case FILL_VERTICAL : width = 2; height = 1; break; case FILL_DIAGONAL : width = 3; height = 3; break; default : return; // Unknown fill type } int data[] = new int [width * height]; // Set bits specified switch (fillType) { case FILL_SOLID : case FILL_10 : case FILL_25 : case FILL_HORIZONTAL : case FILL_VERTICAL : data[0] = 1; break; case FILL_50 : data[0] = 1; data[3] = 1; break; case FILL_75 : data[1] = 1; data[2] = 1; data[3] = 1; break; case FILL_DIAGONAL : data[0] = 1; data[4] = 1; data[8] = 1; break; } setFillPattern (width, height, data); } /** * Set custom fill pattern of this style. * * @param width Tile width. * @param height Tile height. * @param data Pattern data (0s and 1s indicating set/unset). */ public void setFillPattern (int width, int height, int data[]) { fillWidth_ = width; fillHeight_ = height; int size = width * height; fillData_ = size > 0 ? new int[size] : null; for (int i = 0; i < size; i++) fillData_[i] = data != null && data.length > i ? data[i] : 0; // Lazily created when needed fillPattern_ = null; paint_ = null; setValid (MASK_FILLPATTERN); // Notify all owners notifyListeners(); } /** * Set image as fill pattern. * * @param image Image to use as fill pattern. */ public void setFillPattern (BufferedImage image) { fillData_ = null; fillPattern_ = image; setValid (MASK_FILLPATTERN); paint_ = null; // Notify all owners notifyListeners(); } /** * Set image as fill pattern. * TODO: Cache the file name and create the image lazy * * @param fileName File name of image. */ public void setFillPattern (String fileName) { try { InputStream stream = new BufferedInputStream (new FileInputStream (fileName)); fillPattern_ = ImageIO.read (stream); paint_ = null; setValid (MASK_FILLPATTERN); // Notify all owners notifyListeners(); } catch (Exception exception) { exception.printStackTrace(); } } /** * Unset fill pattern. */ public void unsetFillPattern() { setInvalid (MASK_FILLPATTERN); // Notify all owners notifyListeners(); } /** * Return current fill pattern. * * @return Current fill pattern. */ public BufferedImage getFillPattern() { return fillPattern_; } /** * Set custom line style. Dash pattern consists of and array of leg * lengths of line on and line off respectively. * * @param dashPattern New dash pattern. */ public void setLineStyle (float dashPattern[]) { // Copy dash pattern locally if (dashPattern != null) { dashPattern_ = new float[dashPattern.length]; System.arraycopy (dashPattern, 0, dashPattern_, 0, dashPattern.length); isLineVisible_ = true; } else dashPattern_ = null; setValid (MASK_LINESTYLE); stroke_ = null; // Create this lazily when needed // Notify all owners notifyListeners(); } /** * Set predefined line style of this style. * * @param lineStyle New line style. */ public void setLineStyle (int lineStyle) { float dashPattern[] = null; switch (lineStyle) { case LINESTYLE_SOLID : isLineVisible_ = true; break; case LINESTYLE_DASHED : dashPattern = new float[2]; dashPattern[0] = (float) 5.0; dashPattern[1] = (float) 5.0; break; case LINESTYLE_DOTTED : dashPattern = new float[2]; dashPattern[0] = (float) 2.0; dashPattern[1] = (float) 5.0; break; case LINESTYLE_DASHDOT : dashPattern = new float[4]; dashPattern[0] = (float) 8.0; dashPattern[1] = (float) 3.0; dashPattern[2] = (float) 2.0; dashPattern[3] = (float) 3.0; break; case LINESTYLE_INVISIBLE : isLineVisible_ = false; break; } setLineStyle (dashPattern); } /** * Unset line style. */ public void unsetLineStyle() { setInvalid (MASK_LINESTYLE); stroke_ = null; // Create this lazily when needed // Notify all owners notifyListeners(); } /** * Return current line style of this style. * The element is not in use if it is invalid. * * @return Current line style of this style. */ public float[] getLineStyle() { return dashPattern_; } /** * Create a Stroke object based on the line width, cap style, join style, * miter limit (0.0), dash pattern and dash offset (0.0) of this style. * * @return Stroke for this style. */ Stroke getStroke() { // Lazy creation if (stroke_ == null) { // Dash pattern is created based on a line width = 1. For wider // lines we increase the dash accordingly float[] dashPattern = null; if (dashPattern_ != null) { dashPattern = new float[dashPattern_.length]; for (int i = 0; i < dashPattern_.length; i++) dashPattern[i] = dashPattern_[i] * lineWidth_; } stroke_ = new BasicStroke (lineWidth_, capStyle_, joinStyle_, miterLimit_, dashPattern, dashOffset_); } return stroke_; } /** * Create a paint object based on the fill pattern of this style. * * @return Paint object for this style. */ Paint getPaint() { // Generate the image if (fillPattern_ == null && fillData_ != null) { // Create the image that represent the pattern fillPattern_ = new BufferedImage (fillWidth_, fillHeight_, BufferedImage.TYPE_INT_ARGB); // Put the data into the image int pointNo = 0; for (int i = 0; i < fillHeight_; i++) { for (int j = 0; j < fillWidth_; j++) { if (fillData_[pointNo] == 0 && backgroundColor_ != null) fillPattern_.setRGB (i, j, backgroundColor_.getRGB()); else if (fillData_[pointNo] != 0) fillPattern_.setRGB (i, j, foregroundColor_.getRGB()); pointNo++; } } paint_ = null; } // Generate the paint if (paint_ == null && fillPattern_ != null) { paint_ = new TexturePaint (fillPattern_, new Rectangle (0, 0, fillWidth_, fillHeight_)); } // TODO: Gradient color is not currently in use else if (paint_ == null && gradientColor1_ != null) { paint_ = new GradientPaint ((float) -100.0, (float) 0.0, gradientColor1_, (float) 100.0, (float) 0.0, gradientColor2_, true); } else if (backgroundColor_ != null) { paint_ = backgroundColor_; } return paint_; } float getMiterLimit() { return miterLimit_; } /** * Check if line is visible. Line is invisible if line style is set * to invisible. * * @return True if line is visible, false otherwise. */ boolean isLineVisible() { return isLineVisible_; } /** * Check if objects using this style appaer as filled? * * @return Return true if objects using this style appear as filled, * false otherwise. */ boolean isDefiningFill() { return fillPattern_ != null || fillData_ != null || backgroundColor_ != null; } /** * Notify all listeners about change in specified style. * * @param style Style that has changed. */ private void notifyListeners() { for (Iterator i = listeners_.iterator(); i.hasNext(); ) { WeakReference reference = (WeakReference) i.next(); GStyleListener listener = (GStyleListener) reference.get(); // If the listener has been GC'd, remove it from the list if (listener == null) i.remove(); else listener.styleChanged (this); } } /** * Add a listener to this style. When the style is changed, a * styleChanged() signal is sent to the listener. * * @param listener Style listener to add. */ void addListener (GStyleListener listener) { // Check if the listener is there already for (Iterator i = listeners_.iterator(); i.hasNext(); ) { WeakReference reference = (WeakReference) i.next(); if (reference.get() == listener) return; } // Add the listener listeners_.add (new WeakReference (listener)); } /** * Remove specified listener from this style. * * @param listener Style listener to remove. */ void removeListener (GStyleListener listener) { for (Iterator i = listeners_.iterator(); i.hasNext(); ) { WeakReference reference = (WeakReference) i.next(); if (reference.get() == listener) { i.remove(); break; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -