public boolean contains(int x, int y) { // If the button has changed size, // make a new shape object. if (shape == null || !shape.getBounds().equals(getBounds())) { shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight()); } return shape.contains(x, y); }
/** * Constructs a ShapeIcon. * * @param shape the shape to draw * @param decoration a decorating shape to draw * @param width width of the icon * @param height height of the icon */ public ShapeIcon(Shape shape, Shape decoration, int width, int height) { w = width; h = height; this.shape = shape; this.decoration = decoration; Rectangle rect = shape == null ? new Rectangle() : shape.getBounds(); if (decoration != null) rect = rect.union(decoration.getBounds()); offsetX = w / 2 - rect.width / 2 - rect.x; offsetY = h / 2 - rect.height / 2 - rect.y; }
/** * Paints a highlight. * * @param g the graphics context * @param offs0 the starting model offset >= 0 * @param offs1 the ending model offset >= offs1 * @param bounds the bounding box for the highlight * @param c the editor */ public void paint(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c) { Rectangle alloc = bounds.getBounds(); try { // --- determine locations --- TextUI mapper = c.getUI(); Rectangle p0 = mapper.modelToView(c, offs0); Rectangle p1 = mapper.modelToView(c, offs1); // --- render --- Color color = getColor(); if (color == null) { g.setColor(c.getSelectionColor()); } else { g.setColor(color); } if (p0.y == p1.y) { // same line, render a rectangle Rectangle r = p0.union(p1); g.fillRect(r.x, r.y, r.width, r.height); } else { // different lines int p0ToMarginWidth = alloc.x + alloc.width - p0.x; g.fillRect(p0.x, p0.y, p0ToMarginWidth, p0.height); if ((p0.y + p0.height) != p1.y) { g.fillRect(alloc.x, p0.y + p0.height, alloc.width, p1.y - (p0.y + p0.height)); } g.fillRect(alloc.x, p1.y, (p1.x - alloc.x), p1.height); } } catch (BadLocationException e) { // can't render } }
/** * Paints a portion of a highlight. * * @param g the graphics context * @param offs0 the starting model offset >= 0 * @param offs1 the ending model offset >= offs1 * @param bounds the bounding box of the view, which is not necessarily the region to paint. * @param c the editor * @param view View painting for * @return region drawing occurred in */ public Shape paintLayer( Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c, View view) { Color color = getColor(); if (color == null) { g.setColor(c.getSelectionColor()); } else { g.setColor(color); } Rectangle r; if (offs0 == view.getStartOffset() && offs1 == view.getEndOffset()) { // Contained in view, can just use bounds. if (bounds instanceof Rectangle) { r = (Rectangle) bounds; } else { r = bounds.getBounds(); } } else { // Should only render part of View. try { // --- determine locations --- Shape shape = view.modelToView(offs0, Position.Bias.Forward, offs1, Position.Bias.Backward, bounds); r = (shape instanceof Rectangle) ? (Rectangle) shape : shape.getBounds(); } catch (BadLocationException e) { // can't render r = null; } } if (r != null) { // If we are asked to highlight, we should draw something even // if the model-to-view projection is of zero width (6340106). r.width = Math.max(r.width, 1); g.fillRect(r.x, r.y, r.width, r.height); } return r; }
public void draw(Graphics2D graphic) { setForegroundColor(colorString); graphic.fill(shape); if (text != null) { setForegroundColor("black"); java.awt.Rectangle bounds = shape.getBounds(); int x = bounds.x + bounds.width / 2; int y = bounds.y + bounds.height / 2; graphic.drawString(text, x, y); } }
public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException { int p0 = getStartOffset(); int p1 = getEndOffset(); if ((pos >= p0) && (pos <= p1)) { Rectangle r = a.getBounds(); if (pos == p1) { r.x += r.width; } r.width = 0; return r; } throw new BadLocationException(pos + " not in range " + p0 + "," + p1, pos); }
private static BufferedImage createImage(String s, boolean valid) { FontRenderContext frc = new FontRenderContext(null, true, true); Font font = new Font("dialog", Font.BOLD, 12); GlyphVector glyphs = font.createGlyphVector(frc, s); Shape shape = glyphs.getOutline(); Rectangle bounds = shape.getBounds(); int imageW = bounds.width; int imageH = bounds.height; BufferedImage image = new BufferedImage(imageW, imageH, BufferedImage.TYPE_INT_ARGB); Graphics2D g = (Graphics2D) image.getGraphics(); g.setColor(valid ? Color.blue : Color.red); g.translate(bounds.x, -bounds.y); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.fill(shape); return image; }
void union(Shape bounds) { if (bounds == null) return; Rectangle alloc; if (bounds instanceof Rectangle) { alloc = (Rectangle) bounds; } else { alloc = bounds.getBounds(); } if (width == 0 || height == 0) { x = alloc.x; y = alloc.y; width = alloc.width; height = alloc.height; } else { width = Math.max(x + width, alloc.x + alloc.width); height = Math.max(y + height, alloc.y + alloc.height); x = Math.min(x, alloc.x); width -= x; y = Math.min(y, alloc.y); height -= y; } }
@Override public void paintIcon(Component c, Graphics g, int x, int y) { Graphics2D g2 = (Graphics2D) g.create(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.translate(x, y); g2.setStroke(new BasicStroke(BORDER_WIDTH)); g2.setPaint(LINE_COLOR); g2.draw(BORDER); g2.setStroke(new BasicStroke(SLIT_WIDTH)); g2.setColor(UIManager.getColor("Panel.background")); int n = SLIT_NUM + 1; int v = ICON_SIZE / n; int m = n * v; for (int i = 1; i < n; i++) { int a = i * v; g2.drawLine(a, 0, a, m); g2.drawLine(0, a, m, a); } // g2.drawLine(1 * v, 0 * v, 1 * v, 4 * v); // g2.drawLine(2 * v, 0 * v, 2 * v, 4 * v); // g2.drawLine(3 * v, 0 * v, 3 * v, 4 * v); // g2.drawLine(0 * v, 1 * v, 4 * v, 1 * v); // g2.drawLine(0 * v, 2 * v, 4 * v, 2 * v); // g2.drawLine(0 * v, 3 * v, 4 * v, 3 * v); g2.setPaint(LINE_COLOR); Rectangle2D b = ARROW.getBounds(); Point2D p = new Point2D.Double(b.getX() + b.getWidth() / 2d, b.getY() + b.getHeight() / 2d); AffineTransform toCenterAT = AffineTransform.getTranslateInstance(ICON_SIZE / 2d - p.getX(), ICON_SIZE / 2d - p.getY()); g2.fill(toCenterAT.createTransformedShape(ARROW)); g2.dispose(); }
/** * Adjusts the allocation given to the view to be a suitable allocation for a text field. If the * view has been allocated more than the preferred span vertically, the allocation is changed to * be centered vertically. Horizontally the view is adjusted according to the horizontal alignment * property set on the associated JTextField (if that is the type of the hosting component). * * @param a the allocation given to the view, which may need to be adjusted. * @return the allocation that the superclass should use. */ protected Shape adjustAllocation(Shape a) { if (a != null) { Rectangle bounds = a.getBounds(); int vspan = (int) getPreferredSpan(Y_AXIS); int hspan = (int) getPreferredSpan(X_AXIS); if (bounds.height != vspan) { int slop = bounds.height - vspan; bounds.y += slop / 2; bounds.height -= slop; } // horizontal adjustments Component c = getContainer(); if (c instanceof JTextField) { JTextField field = (JTextField) c; BoundedRangeModel vis = field.getHorizontalVisibility(); int max = Math.max(hspan, bounds.width); int value = vis.getValue(); int extent = Math.min(max, bounds.width - 1); if ((value + extent) > max) { value = max - extent; } vis.setRangeProperties(value, extent, vis.getMinimum(), max, false); if (hspan < bounds.width) { // horizontally align the interior int slop = bounds.width - 1 - hspan; int align = ((JTextField) c).getHorizontalAlignment(); if (Utilities.isLeftToRight(c)) { if (align == LEADING) { align = LEFT; } else if (align == TRAILING) { align = RIGHT; } } else { if (align == LEADING) { align = RIGHT; } else if (align == TRAILING) { align = LEFT; } } switch (align) { case SwingConstants.CENTER: bounds.x += slop / 2; bounds.width -= slop; break; case SwingConstants.RIGHT: bounds.x += slop; bounds.width -= slop; break; } } else { // adjust the allocation to match the bounded range. bounds.width = hspan; bounds.x -= vis.getValue(); } } return bounds; } return null; }
@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(); }
/** * Paints the image. * * @param g the rendering surface to use * @param a the allocated region to render into * @see View#paint */ public void paint(Graphics g, Shape a) { Color oldColor = g.getColor(); fBounds = a.getBounds(); int border = getBorder(); int x = fBounds.x + border + getSpace(X_AXIS); int y = fBounds.y + border + getSpace(Y_AXIS); int width = fWidth; int height = fHeight; int sel = getSelectionState(); // Make sure my Component is in the right place: /* if( fComponent == null ) { fComponent = new Component() { }; fComponent.addMouseListener(this); fComponent.addMouseMotionListener(this); fComponent.setCursor(Cursor.getDefaultCursor()); // use arrow cursor fContainer.add(fComponent); } fComponent.setBounds(x,y,width,height); */ // If no pixels yet, draw gray outline and icon: if (!hasPixels(this)) { g.setColor(Color.lightGray); g.drawRect(x, y, width - 1, height - 1); g.setColor(oldColor); loadIcons(); Icon icon = fImage == null ? sMissingImageIcon : sPendingImageIcon; if (icon != null) icon.paintIcon(getContainer(), g, x, y); } // Draw image: if (fImage != null) { g.drawImage(fImage, x, y, width, height, this); // Use the following instead of g.drawImage when // BufferedImageGraphics2D.setXORMode is fixed (4158822). // Use Xor mode when selected/highlighted. // ! Could darken image instead, but it would be more expensive. /* if( sel > 0 ) g.setXORMode(Color.white); g.drawImage(fImage,x, y, width,height,this); if( sel > 0 ) g.setPaintMode(); */ } // If selected exactly, we need a black border & grow-box: Color bc = getBorderColor(); if (sel == 2) { // Make sure there's room for a border: int delta = 2 - border; if (delta > 0) { x += delta; y += delta; width -= delta << 1; height -= delta << 1; border = 2; } bc = null; g.setColor(Color.black); // Draw grow box: g.fillRect(x + width - 5, y + height - 5, 5, 5); } // Draw border: if (border > 0) { if (bc != null) g.setColor(bc); // Draw a thick rectangle: for (int i = 1; i <= border; i++) g.drawRect(x - i, y - i, width - 1 + i + i, height - 1 + i + i); g.setColor(oldColor); } }