blueprintengine.java
来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,139 行 · 第 1/3 页
JAVA
1,139 行
color = RGBtoHLS(red, green, blue); red = hue; green = color.getGreen(); blue = sat; color = HLStoRGB(red, green, blue); pixels[index] = color.getRed(); pixels[index + 1] = color.getGreen(); pixels[index + 2] = color.getBlue(); pixels[index + 3] = Math.min( pixels[index + 3], alpha); } // Set the pixel data for the image. i.getRaster().setPixels(0, 0, iw, ih, pixels); image = i; } if (stretch) {// themeBlueprintComputeHints(); } if (iw <= 0 || ih <= 0) { return; } Object lastHint; Object renderingHint = RENDERING_HINT; if (renderingHint != null && stretch) { Graphics2D g2 = (Graphics2D)g; lastHint = g2.getRenderingHint(RenderingHints.KEY_INTERPOLATION); if (lastHint == null) { lastHint = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR; } g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, renderingHint); } else { lastHint = null; } if (!stretch) { if (center) { /* Center the image. */ blueprintRender(image, g, 0, 0, iw, ih, x + (w / 2) - (iw / 2), y + (h / 2) - (ih / 2), iw, ih); } else { /* Tile the image. */ int lastIY = 0; for (int yCounter = y, maxY = y + h; yCounter < maxY; yCounter += (ih - lastIY), lastIY = 0) { int lastIX = 0; for (int xCounter = x, maxX = x + w; xCounter < maxX; xCounter += (iw - lastIX), lastIX = 0) { int dx2 = Math.min(maxX, xCounter + iw - lastIX); int dy2 = Math.min(maxY, yCounter + ih - lastIY); if (intersectsClip(xCounter, yCounter, dx2, dy2)) { g.drawImage(image, xCounter, yCounter, dx2, dy2, lastIX, lastIY, lastIX + dx2 -xCounter, lastIY + dy2 - yCounter, null); } } } } } else { int srcX[] = new int[4]; int srcY[] = new int[4]; int destX[] = new int[4]; int destY[] = new int[4]; srcX[0] = 0; srcX[1] = insets.left; srcX[2] = iw - insets.right; srcX[3] = iw; srcY[0] = 0; srcY[1] = insets.top; srcY[2] = ih - insets.bottom; srcY[3] = ih; destX[0] = x; destX[1] = x + insets.left; destX[2] = x + w - insets.right; destX[3] = x + w; destY[0] = y; destY[1] = y + insets.top; destY[2] = y + h - insets.bottom; destY[3] = y + h; /* Scale the image. */ if ((componentMask & COMPONENT_ALL) != 0) { componentMask = (COMPONENT_ALL - 1) & ~componentMask; } // top left if ((componentMask & COMPONENT_NORTH_WEST) != 0) { blueprintRender(image, g, srcX[0], srcY[0], srcX[1] - srcX[0], srcY[1] - srcY[0], destX[0], destY[0], destX[1] - destX[0], destY[1] - destY[0]); } // top if ((componentMask & COMPONENT_NORTH) != 0) { blueprintRender(image, g, srcX[1], srcY[0], srcX[2] - srcX[1], srcY[1] - srcY[0], destX[1], destY[0], destX[2] - destX[1], destY[1] - destY[0]); } // top right if ((componentMask & COMPONENT_NORTH_EAST) != 0) { blueprintRender(image, g, srcX[2], srcY[0], srcX[3] - srcX[2], srcY[1] - srcY[0], destX[2], destY[0], destX[3] - destX[2], destY[1] - destY[0]); } // left if ((componentMask & COMPONENT_WEST) != 0) { blueprintRender(image, g, srcX[0], srcY[1], srcX[1] - srcX[0], srcY[2] - srcY[1], destX[0], destY[1], destX[1] - destX[0], destY[2] - destY[1]); } // center if ((componentMask & COMPONENT_CENTER) != 0) { blueprintRender(image, g, srcX[1], srcY[1], srcX[2] - srcX[1], srcY[2] - srcY[1], destX[1], destY[1], destX[2] - destX[1], destY[2] - destY[1]); } // right if ((componentMask & COMPONENT_EAST) != 0) { blueprintRender(image, g, srcX[2], srcY[1], srcX[3] - srcX[2], srcY[2] - srcY[1], destX[2], destY[1], destX[3] - destX[2], destY[2] - destY[1]); } // bottom left if ((componentMask & COMPONENT_SOUTH_WEST) != 0) { blueprintRender(image, g, srcX[0], srcY[2], srcX[1] - srcX[0], srcY[3] - srcY[2], destX[0], destY[2], destX[1] - destX[0], destY[3] - destY[2]); } // bottom if ((componentMask & COMPONENT_SOUTH) != 0) { blueprintRender(image, g, srcX[1], srcY[2], srcX[2] - srcX[1], srcY[3] - srcY[2], destX[1], destY[2], destX[2] - destX[1], destY[3] - destY[2]); } // bottom right if ((componentMask & COMPONENT_SOUTH_EAST) != 0) { blueprintRender(image, g, srcX[2], srcY[2], srcX[3] - srcX[2], srcY[3] - srcY[2], destX[2], destY[2], destX[3] - destX[2], destY[3] - destY[2]); } } if (lastHint != null) { ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_INTERPOLATION, lastHint); } } /** * Draws a portion of an image stretched. * * @param image Image to render. * @param g Graphics to render to * @param srcX X origin to draw from * @param srxY Y origin to draw from * @param srcWidth Width of source * @param srcHeight Height of source * @param destX X origin to draw to * @param destY Y origin to draw to * @param destWidth Width of destination * @param destHeight Height of destination */ private void blueprintRender(Image image, Graphics g, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight) { if (destWidth <= 0 || destHeight <= 0 || !intersectsClip(destX, destY, destX + destWidth, destY + destHeight)) { // Bogus location, nothing to paint return; } if (srcWidth == 0 && srcHeight == 0) { // Paint bilinear gradient. } else if (srcHeight == 0 && destHeight == srcHeight) { // Paint horizontal gradient. } else if (srcHeight == 0 && destWidth == srcWidth) { // Paint vertical gradient. } g.drawImage(image, destX, destY, destX + destWidth, destY + destHeight, srcX, srcY, srcX + srcWidth, srcY + srcHeight, null); } private boolean hasAncestorOfTypeFromList(JComponent c, ArrayList list) { if (list == null) { return false; } Iterator itr = list.iterator(); while (itr.hasNext()) { if (hasAncestorOfType(c, (String)itr.next())) { return true; } } return false; } private boolean hasAncestorOfType(JComponent c, String parentType) { String type = null; while (c != null) { type = getComponentType(c); if (type == parentType) { return true; } if (c.getParent() instanceof JComponent) { c = (JComponent)c.getParent(); } else { c = null; } } return false; } private String getComponentType(JComponent c) { return GTKStyleFactory.gtkClassFor(SynthLookAndFeel.getRegion(c)); } private SynthStyle getStyle(JComponent c, ComponentUI ui) { if (ui instanceof SynthUI) { SynthContext parentContext = ((SynthUI)ui).getContext(c); // Note that we don't dispose of the context here, while this // isn't good, it just means we won't be recycling as often as // we can. return parentContext.getStyle(); } return null; } /** * Returns true if the passed in region intersects the clip. */ private boolean intersectsClip(int x1, int y1, int x2, int y2) { return ((x2 < x1 || x2 > _clipX1) && (y2 < y1 || y2 > _clipY1) && (_clipX2 < _clipX1 || _clipX2 > x1) && (_clipY2 < _clipY1 || _clipY2 > y1)); } /** * Convert RGB to HLS. * * @param r Red * @param g Green * @param b Blue * @return Color Where red = hue, green = lightness and blue = saturation. */ private Color RGBtoHLS(int r, int g, int b) { int h, l, s; int min, max; int delta; if (r > g) { max = Math.max(r, b); min = Math.min(g, b); } else { max = Math.max(g, b); min = Math.min(r, b); } l = (max + min) / 2; if (max == min) { s = 0; h = 0; } else { delta = (max - min); if (l < 128) { s = 255 * delta / (max + min); } else { s = 255 * delta / (511 - max - min); } if (r == max) { h = (g - b) / delta; } else if (g == max) { h = 2 + (b - r) / delta; } else { h = 4 + (r - g) / delta; } h = (int)(h * 42.5); if (h < 0) { h+= 255; } else if (h > 255) { h -= 255; } } return new Color(h, l, s); } /** * Convert HLS to RGB. * * @param hue Hue * @param lightness Lightness * @param saturation Saturation * @return Color Resulting RGB color. */ private Color HLStoRGB(int hue, int lightness, int saturation) { double h = hue; double l = lightness; double s = saturation; double m1, m2; if (s == 0) { hue = lightness; saturation = lightness; } else { if (l < 128) { m2 = (l * (255 + s)) / 65025.0; } else { m2 = (l + s - (l * s) / 255.0) / 255.0; } m1 = (l / 127.5) - m2; hue = HLSvalue(m1, m2, h + 85); lightness = HLSvalue(m1, m2, h); saturation = HLSvalue(m1, m2, h - 85); } return new Color(hue, lightness, saturation); } private int HLSvalue(double n1, double n2, double hue) { double value; if (hue > 255) { hue -= 255; } else if (hue < 0) { hue += 255; } if (hue < 42.5) { value = n1 + (n2 - n1) * (hue / 42.5); } else if (hue < 127.5) { value = n2; } else if (hue < 170) { value = n1 + (n2 - n1) * ((170 - hue) / 42.5); } else { value = n1; } return (int)(value * 255); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?