📄 imageview.java
字号:
fBounds.setBounds(rect); paintHighlights(g, a); paintBorder(g, rect); if (clip != null) { g.clipRect(rect.x + leftInset, rect.y + topInset, rect.width - leftInset - rightInset, rect.height - topInset - bottomInset); } if (image != null) { if (!hasPixels(image)) { // No pixels yet, use the default Icon icon = (image == null) ? getNoImageIcon() : getLoadingImageIcon(); if (icon != null) { icon.paintIcon(getContainer(), g, rect.x + leftInset, rect.y + topInset); } } else { // Draw the image g.drawImage(image, rect.x + leftInset, rect.y + topInset, width, height, imageObserver); } } else { Icon icon = getNoImageIcon(); if (icon != null) { icon.paintIcon(getContainer(), g, rect.x + leftInset, rect.y + topInset); } View view = getAltView(); // Paint the view representing the alt text, if its non-null if (view != null && ((state & WIDTH_FLAG) == 0 || width > DEFAULT_WIDTH)) { // Assume layout along the y direction Rectangle altRect = new Rectangle (rect.x + leftInset + DEFAULT_WIDTH, rect.y + topInset, rect.width - leftInset - rightInset - DEFAULT_WIDTH, rect.height - topInset - bottomInset); view.paint(g, altRect); } } if (clip != null) { // Reset clip. g.setClip(clip.x, clip.y, clip.width, clip.height); } } private void paintHighlights(Graphics g, Shape shape) { if (container instanceof JTextComponent) { JTextComponent tc = (JTextComponent)container; Highlighter h = tc.getHighlighter(); if (h instanceof LayeredHighlighter) { ((LayeredHighlighter)h).paintLayeredHighlights (g, getStartOffset(), getEndOffset(), shape, tc, this); } } } private void paintBorder(Graphics g, Rectangle rect) { Color color = borderColor; if ((borderSize > 0 || image == null) && color != null) { int xOffset = leftInset - borderSize; int yOffset = topInset - borderSize; g.setColor(color); int n = (image == null) ? 1 : borderSize; for (int counter = 0; counter < n; counter++) { g.drawRect(rect.x + xOffset + counter, rect.y + yOffset + counter, rect.width - counter - counter - xOffset -xOffset-1, rect.height - counter - counter -yOffset-yOffset-1); } } } /** * Determines the preferred span for this view along an * axis. * * @param axis may be either X_AXIS or Y_AXIS * @return the span the view would like to be rendered into; * typically the view is told to render into the span * that is returned, although there is no guarantee; * the parent may choose to resize or break the view */ public float getPreferredSpan(int axis) { sync(); // If the attributes specified a width/height, always use it! if (axis == View.X_AXIS && (state & WIDTH_FLAG) == WIDTH_FLAG) { getPreferredSpanFromAltView(axis); return width + leftInset + rightInset; } if (axis == View.Y_AXIS && (state & HEIGHT_FLAG) == HEIGHT_FLAG) { getPreferredSpanFromAltView(axis); return height + topInset + bottomInset; } Image image = getImage(); if (image != null) { switch (axis) { case View.X_AXIS: return width + leftInset + rightInset; case View.Y_AXIS: return height + topInset + bottomInset; default: throw new IllegalArgumentException("Invalid axis: " + axis); } } else { View view = getAltView(); float retValue = 0f; if (view != null) { retValue = view.getPreferredSpan(axis); } switch (axis) { case View.X_AXIS: return retValue + (float)(width + leftInset + rightInset); case View.Y_AXIS: return retValue + (float)(height + topInset + bottomInset); default: throw new IllegalArgumentException("Invalid axis: " + axis); } } } /** * Determines the desired alignment for this view along an * axis. This is implemented to give the alignment to the * bottom of the icon along the y axis, and the default * along the x axis. * * @param axis may be either X_AXIS or Y_AXIS * @return the desired alignment; this should be a value * between 0.0 and 1.0 where 0 indicates alignment at the * origin and 1.0 indicates alignment to the full span * away from the origin; an alignment of 0.5 would be the * center of the view */ public float getAlignment(int axis) { switch (axis) { case View.Y_AXIS: return vAlign; default: return super.getAlignment(axis); } } /** * Provides a mapping from the document model coordinate space * to the coordinate space of the view mapped to it. * * @param pos the position to convert * @param a the allocated region to render into * @return the bounding box of the given position * @exception BadLocationException if the given position does not represent a * valid location in the associated document * @see View#modelToView */ 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; } return null; } /** * Provides a mapping from the view coordinate space to the logical * coordinate space of the model. * * @param x the X coordinate * @param y the Y coordinate * @param a the allocated region to render into * @return the location within the model that best represents the * given point of view * @see View#viewToModel */ public int viewToModel(float x, float y, Shape a, Position.Bias[] bias) { Rectangle alloc = (Rectangle) a; if (x < alloc.x + alloc.width) { bias[0] = Position.Bias.Forward; return getStartOffset(); } bias[0] = Position.Bias.Backward; return getEndOffset(); } /** * Sets the size of the view. This should cause * layout of the view if it has any layout duties. * * @param width the width >= 0 * @param height the height >= 0 */ public void setSize(float width, float height) { sync(); if (getImage() == null) { View view = getAltView(); if (view != null) { view.setSize(Math.max(0f, width - (float)(DEFAULT_WIDTH + leftInset + rightInset)), Math.max(0f, height - (float)(topInset + bottomInset))); } } } /** * Returns true if this image within a link? */ private boolean isLink() { return ((state & LINK_FLAG) == LINK_FLAG); } /** * Returns true if the passed in image has a non-zero width and height. */ private boolean hasPixels(Image image) { return image != null && (image.getHeight(imageObserver) > 0) && (image.getWidth(imageObserver) > 0); } /** * Returns the preferred span of the View used to display the alt text, * or 0 if the view does not exist. */ private float getPreferredSpanFromAltView(int axis) { if (getImage() == null) { View view = getAltView(); if (view != null) { return view.getPreferredSpan(axis); } } return 0f; } /** * Request that this view be repainted. * Assumes the view is still at its last-drawn location. */ private void repaint(long delay) { if (container != null && fBounds != null) { container.repaint(delay, fBounds.x, fBounds.y, fBounds.width, fBounds.height); } } /** * Convenience method for getting an integer attribute from the elements * AttributeSet. */ private int getIntAttr(HTML.Attribute name, int deflt) { AttributeSet attr = getElement().getAttributes(); if (attr.isDefined(name)) { // does not check parents! int i; String val = (String)attr.getAttribute(name); if (val == null) { i = deflt; } else { try{ i = Math.max(0, Integer.parseInt(val)); }catch( NumberFormatException x ) { i = deflt; } } return i; } else return deflt; } /** * Makes sure the necessary properties and image is loaded. */ private void sync() { int s = state; if ((s & RELOAD_IMAGE_FLAG) != 0) { refreshImage(); } s = state; if ((s & RELOAD_FLAG) != 0) { synchronized(this) { state = (state | RELOAD_FLAG) ^ RELOAD_FLAG; } setPropertiesFromAttributes(); } } /** * Loads the image and updates the size accordingly. This should be * invoked instead of invoking <code>loadImage</code> or * <code>updateImageSize</code> directly. */ private void refreshImage() { synchronized(this) { // clear out width/height/realoadimage flag and set loading flag state = (state | LOADING_FLAG | RELOAD_IMAGE_FLAG | WIDTH_FLAG | HEIGHT_FLAG) ^ (WIDTH_FLAG | HEIGHT_FLAG | RELOAD_IMAGE_FLAG); image = null; width = height = 0; } try { // Load the image loadImage(); // And update the size params updateImageSize(); } finally { synchronized(this) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -