/** * Writes the <code>shape</code>, <code>coords</code>, <code>href</code>, * <code>nohref</code> Attribute for the specified figure and shape. * * @return Returns true, if the polygon is inside of the image bounds. */ private boolean writePolyAttributes(IXMLElement elem, SVGFigure f, Shape shape) { AffineTransform t = TRANSFORM.getClone(f); if (t == null) { t = drawingTransform; } else { t.preConcatenate(drawingTransform); } StringBuilder buf = new StringBuilder(); float[] coords = new float[6]; GeneralPath path = new GeneralPath(); for (PathIterator i = shape.getPathIterator(t, 1.5f); ! i.isDone(); i.next()) { switch (i.currentSegment(coords)) { case PathIterator.SEG_MOVETO : if (buf.length() != 0) { throw new IllegalArgumentException("Illegal shape "+shape); } if (buf.length() != 0) { buf.append(','); } buf.append((int) coords[0]); buf.append(','); buf.append((int) coords[1]); path.moveTo(coords[0], coords[1]); break; case PathIterator.SEG_LINETO : if (buf.length() != 0) { buf.append(','); } buf.append((int) coords[0]); buf.append(','); buf.append((int) coords[1]); path.lineTo(coords[0], coords[1]); break; case PathIterator.SEG_CLOSE : path.closePath(); break; default : throw new InternalError("Illegal segment type "+i.currentSegment(coords)); } } elem.setAttribute("shape", "poly"); elem.setAttribute("coords", buf.toString()); writeHrefAttribute(elem, f); return path.intersects(new Rectangle2D.Float(bounds.x, bounds.y, bounds.width, bounds.height)); }
/** * This implementation calls <code>super.hitTest</code> and returns the result if non-null (this * should be a HitInfo.Point), then returns a HitInfo.Interior if the mouse-click occured inside * the text bound (as defined by text layout) * * @return a HitInfo corresponding to the given mouse-event */ public HitInfo hitTest(PEMouseEvent e) { // from Bitmap: if (image != null) { if (getBounds().contains(e.getPicPoint())) { return new HitInfo.Interior((PicText) element, e); } return null; } // from TextLayout: if (!getBounds().contains(e.getPicPoint())) return null; PicText te = (PicText) element; // recompute textlayout b-box, but store it in a temporary field ! Rectangle2D tb = textLayout.getBounds(); Shape text_bounds = text2ModelTr.createTransformedShape(tb); if (text_bounds.contains(e.getPicPoint())) { // [SR:pending] for the hitInfo to be reliable, getPicPoint() should first be transformed by // inverse text2ModelTr ! (especially when rotationAngle != 0) TextHitInfo thi = textLayout.hitTestChar( (float) (e.getPicPoint().x - strx), (float) (e.getPicPoint().y - stry)); // guaranteed to return a non-null thi return new HitInfo.Text((PicText) element, thi, e); } // test hit on textlayout's bounding rectangle : // else if (bounds.contains(e.getPicPoint())) return new HitInfo.Interior(element,e); return null; }
private Paint decodeGradient6(Shape s) { Rectangle2D bounds = s.getBounds2D(); float x = (float) bounds.getX(); float y = (float) bounds.getY(); float w = (float) bounds.getWidth(); float h = (float) bounds.getHeight(); return decodeGradient( (0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y, new float[] {0.0f, 0.03f, 0.06f, 0.33f, 0.6f, 0.65f, 0.7f, 0.825f, 0.95f, 0.975f, 1.0f}, new Color[] { color28, decodeColor(color28, color29, 0.5f), color29, decodeColor(color29, color30, 0.5f), color30, decodeColor(color30, color30, 0.5f), color30, decodeColor(color30, color31, 0.5f), color31, decodeColor(color31, color32, 0.5f), color32 }); }
private Paint decodeGradient10(Shape s) { Rectangle2D bounds = s.getBounds2D(); float x = (float) bounds.getX(); float y = (float) bounds.getY(); float w = (float) bounds.getWidth(); float h = (float) bounds.getHeight(); return decodeGradient( (0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y, new float[] {0.0f, 0.024f, 0.06f, 0.276f, 0.6f, 0.65f, 0.7f, 0.856f, 0.96f, 0.98f, 1.0f}, new Color[] { (Color) componentColors[0], decodeColor((Color) componentColors[0], (Color) componentColors[1], 0.5f), (Color) componentColors[1], decodeColor((Color) componentColors[1], (Color) componentColors[2], 0.5f), (Color) componentColors[2], decodeColor((Color) componentColors[2], (Color) componentColors[2], 0.5f), (Color) componentColors[2], decodeColor((Color) componentColors[2], (Color) componentColors[3], 0.5f), (Color) componentColors[3], decodeColor((Color) componentColors[3], (Color) componentColors[3], 0.5f), (Color) componentColors[3] }); }
private Paint decodeGradient10(Shape s) { Rectangle2D bounds = s.getBounds2D(); float x = (float) bounds.getX(); float y = (float) bounds.getY(); float w = (float) bounds.getWidth(); float h = (float) bounds.getHeight(); return decodeGradient( (0.24868421f * w) + x, (0.0014705883f * h) + y, (0.24868421f * w) + x, (1.0f * h) + y, new float[] {0.0f, 0.5f, 1.0f}, new Color[] {color48, decodeColor(color48, color49, 0.5f), color49}); }
private Paint decodeGradient2(Shape s) { Rectangle2D bounds = s.getBounds2D(); float x = (float) bounds.getX(); float y = (float) bounds.getY(); float w = (float) bounds.getWidth(); float h = (float) bounds.getHeight(); return decodeGradient( (0.25f * w) + x, (0.0f * h) + y, (0.25441176f * w) + x, (1.0016667f * h) + y, new float[] {0.0f, 0.5f, 1.0f}, new Color[] {color3, decodeColor(color3, color2, 0.5f), color2}); }
private Paint decodeGradient13(Shape s) { Rectangle2D bounds = s.getBounds2D(); float x = (float) bounds.getX(); float y = (float) bounds.getY(); float w = (float) bounds.getWidth(); float h = (float) bounds.getHeight(); return decodeGradient( (0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y, new float[] {0.26047903f, 0.6302395f, 1.0f}, new Color[] {color62, decodeColor(color62, color63, 0.5f), color63}); }
private Paint decodeGradient4(Shape s) { Rectangle2D bounds = s.getBounds2D(); float x = (float) bounds.getX(); float y = (float) bounds.getY(); float w = (float) bounds.getWidth(); float h = (float) bounds.getHeight(); return decodeGradient( (0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y, new float[] {0.0f, 0.1684492f, 1.0f}, new Color[] {color10, decodeColor(color10, color11, 0.5f), color11}); }
private Paint decodeGradient3(Shape s) { Rectangle2D bounds = s.getBounds2D(); float x = (float) bounds.getX(); float y = (float) bounds.getY(); float w = (float) bounds.getWidth(); float h = (float) bounds.getHeight(); return decodeGradient( (0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y, new float[] {0.0f, 0.49573863f, 0.99147725f}, new Color[] {color8, decodeColor(color8, color9, 0.5f), color9}); }
private Paint decodeGradient9(Shape s) { Rectangle2D bounds = s.getBounds2D(); float x = (float) bounds.getX(); float y = (float) bounds.getY(); float w = (float) bounds.getWidth(); float h = (float) bounds.getHeight(); return decodeGradient( (0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y, new float[] {0.09f, 0.52f, 0.95f}, new Color[] {color39, decodeColor(color39, color40, 0.5f), color40}); }
private Paint decodeGradient5(Shape s) { Rectangle2D bounds = s.getBounds2D(); float x = (float) bounds.getX(); float y = (float) bounds.getY(); float w = (float) bounds.getWidth(); float h = (float) bounds.getHeight(); return decodeGradient( (0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y, new float[] {0.1f, 0.49999997f, 0.9f}, new Color[] {color7, decodeColor(color7, color8, 0.5f), color8}); }
private Paint decodeGradient4(Shape s) { Rectangle2D bounds = s.getBounds2D(); float x = (float) bounds.getX(); float y = (float) bounds.getY(); float w = (float) bounds.getWidth(); float h = (float) bounds.getHeight(); return decodeGradient( (0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y, new float[] {0.1f, 0.49999997f, 0.9f}, new Color[] { (Color) componentColors[0], decodeColor((Color) componentColors[0], (Color) componentColors[1], 0.5f), (Color) componentColors[1] }); }
private Paint decodeGradient9(Shape s) { Rectangle2D bounds = s.getBounds2D(); float x = (float) bounds.getX(); float y = (float) bounds.getY(); float w = (float) bounds.getWidth(); float h = (float) bounds.getHeight(); return decodeGradient( (0.25f * w) + x, (0.0f * h) + y, (0.25441176f * w) + x, (1.0016667f * h) + y, new float[] {0.0f, 0.26988637f, 0.53977275f, 0.5951705f, 0.6505682f, 0.8252841f, 1.0f}, new Color[] { color37, decodeColor(color37, color38, 0.5f), color38, decodeColor(color38, color39, 0.5f), color39, decodeColor(color39, color18, 0.5f), color18 }); }
private Paint decodeGradient15(Shape s) { Rectangle2D bounds = s.getBounds2D(); float x = (float) bounds.getX(); float y = (float) bounds.getY(); float w = (float) bounds.getWidth(); float h = (float) bounds.getHeight(); return decodeGradient( (0.25f * w) + x, (0.0f * h) + y, (0.25441176f * w) + x, (1.0016667f * h) + y, new float[] {0.0f, 0.26988637f, 0.53977275f, 0.66659296f, 0.79341316f, 0.8967066f, 1.0f}, new Color[] { color37, decodeColor(color37, color38, 0.5f), color38, decodeColor(color38, color39, 0.5f), color39, decodeColor(color39, color70, 0.5f), color70 }); }
private Paint decodeGradient12(Shape s) { Rectangle2D bounds = s.getBounds2D(); float x = (float) bounds.getX(); float y = (float) bounds.getY(); float w = (float) bounds.getWidth(); float h = (float) bounds.getHeight(); return decodeGradient( (0.25f * w) + x, (0.0f * h) + y, (0.25441176f * w) + x, (1.0016667f * h) + y, new float[] {0.0f, 0.26988637f, 0.53977275f, 0.6082097f, 0.6766467f, 0.83832335f, 1.0f}, new Color[] { color3, decodeColor(color3, color59, 0.5f), color59, decodeColor(color59, color60, 0.5f), color60, decodeColor(color60, color2, 0.5f), color2 }); }
private Paint decodeGradient16(Shape s) { Rectangle2D bounds = s.getBounds2D(); float x = (float) bounds.getX(); float y = (float) bounds.getY(); float w = (float) bounds.getWidth(); float h = (float) bounds.getHeight(); return decodeGradient( (0.25f * w) + x, (0.0f * h) + y, (0.25441176f * w) + x, (1.0016667f * h) + y, new float[] {0.0f, 0.26988637f, 0.53977275f, 0.6291678f, 0.7185629f, 0.8592814f, 1.0f}, new Color[] { color50, decodeColor(color50, color52, 0.5f), color52, decodeColor(color52, color52, 0.5f), color52, decodeColor(color52, color53, 0.5f), color53 }); }
/** * Rebuilds the template from an input image. The input image dimensions must match the original. * The input and original are overlaid onto the existing template, if any. Pixels that fall * outside the mask are ignored in the final template. * * @param image the input image * @param alphaInput the opacity with which the input image is overlaid * @param alphaOriginal the opacity with which the original image is overlaid */ public void rebuildTemplate(BufferedImage image, int alphaInput, int alphaOriginal) { int w = image.getWidth(); int h = image.getHeight(); // return if image dimensions do not match original image if (original.getWidth() != w || original.getHeight() != h) return; // return if both alphas are zero if (alphaInput == 0 && alphaOriginal == 0) return; // draw image onto argb input BufferedImage input = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); input.createGraphics().drawImage(image, 0, 0, null); // create working image if needed if (working == null) { working = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); } // reset template dimensions and create new template if needed if (template == null || w != wTemplate || h != hTemplate) { wTemplate = w; hTemplate = h; int len = w * h; template = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); pixels = new int[len]; templateR = new int[len]; templateG = new int[len]; templateB = new int[len]; isPixelTransparent = new boolean[len]; } // set alpha of input and draw onto working Graphics2D gWorking = working.createGraphics(); alphaInput = Math.max(0, Math.min(255, alphaInput)); if (alphaInput > 0) { // overlay only if not transparent gWorking.setComposite(getComposite(alphaInput)); input.getRaster().getDataElements(0, 0, w, h, pixels); gWorking.drawImage(input, 0, 0, null); } // set alpha of original and draw onto working alphaOriginal = Math.max(0, Math.min(255, alphaOriginal)); if (alphaOriginal > 0) { // overlay only if not transparent gWorking.setComposite(getComposite(alphaOriginal)); original.getRaster().getDataElements(0, 0, w, h, pixels); gWorking.drawImage(original, 0, 0, null); } // read pixels from working raster working.getRaster().getDataElements(0, 0, wTemplate, hTemplate, pixels); if (mask != null) { // set pixels outside mask to transparent for (int i = 0; i < pixels.length; i++) { boolean inside = true; // pixel is inside only if all corners are inside int x = i % wTemplate, y = i / wTemplate; for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { p.setLocation(x + j, y + k); inside = inside && mask.contains(p); } } if (!inside) pixels[i] = pixels[i] & (0 << 24); // set alpha to zero (transparent) } } // write pixels to template raster template.getRaster().setDataElements(0, 0, wTemplate, hTemplate, pixels); // trim transparent edges from template int trimRight = 0, trimBottom = 0; trimLeft = trimTop = 0; // left edge boolean transparentEdge = true; while (transparentEdge && trimLeft < wTemplate) { for (int line = 0; line < hTemplate; line++) { int i = line * wTemplate + trimLeft; transparentEdge = transparentEdge && getAlpha(pixels[i]) == 0; } if (transparentEdge) trimLeft++; } // right edge transparentEdge = true; while (transparentEdge && (trimLeft + trimRight) < wTemplate) { for (int line = 0; line < hTemplate; line++) { int i = (line + 1) * wTemplate - 1 - trimRight; transparentEdge = transparentEdge && getAlpha(pixels[i]) == 0; } if (transparentEdge) trimRight++; } // top edge transparentEdge = true; while (transparentEdge && trimTop < hTemplate) { for (int col = 0; col < wTemplate; col++) { int i = trimTop * wTemplate + col; transparentEdge = transparentEdge && getAlpha(pixels[i]) == 0; } if (transparentEdge) trimTop++; } // bottom edge transparentEdge = true; while (transparentEdge && (trimTop + trimBottom) < hTemplate) { for (int col = 0; col < wTemplate; col++) { int i = (hTemplate - 1 - trimBottom) * wTemplate + col; transparentEdge = transparentEdge && getAlpha(pixels[i]) == 0; } if (transparentEdge) trimBottom++; } // reduce size of template if needed if (trimLeft + trimRight + trimTop + trimBottom > 0) { wTemplate -= (trimLeft + trimRight); hTemplate -= (trimTop + trimBottom); pixels = new int[wTemplate * hTemplate]; templateR = new int[wTemplate * hTemplate]; templateG = new int[wTemplate * hTemplate]; templateB = new int[wTemplate * hTemplate]; isPixelTransparent = new boolean[wTemplate * hTemplate]; BufferedImage bi = new BufferedImage(wTemplate, hTemplate, BufferedImage.TYPE_INT_ARGB); bi.createGraphics().drawImage(template, -trimLeft, -trimTop, null); template = bi; template.getRaster().getDataElements(0, 0, wTemplate, hTemplate, pixels); } // set up rgb and transparency arrays for faster matching for (int i = 0; i < pixels.length; i++) { int val = pixels[i]; templateR[i] = getRed(val); // red templateG[i] = getGreen(val); // green templateB[i] = getBlue(val); // blue isPixelTransparent[i] = getAlpha(val) == 0; // alpha } }
@Override public int getIconHeight() { return star.getBounds().height; }
@Override public int getIconWidth() { return star.getBounds().width; }
// Graphics2D g=getG();return g==null?null:g.getClip();} public Rectangle getClipBounds() { Shape s = bufferClip(); return s == null ? null : s.getBounds(); }