private static BufferedImage createImage (int width, int height, Color color) { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR); Graphics2D g = image.createGraphics(); g.setColor(color); g.fillRect(0, 0, width, height); g.dispose(); return image; } }
/** Returns an image that can be used by effects as a temp image. */ static public BufferedImage getScratchImage () { Graphics2D g = (Graphics2D)scratchImage.getGraphics(); g.setComposite(AlphaComposite.Clear); g.fillRect(0, 0, GlyphPage.MAX_GLYPH_SIZE, GlyphPage.MAX_GLYPH_SIZE); g.setComposite(AlphaComposite.SrcOver); g.setColor(java.awt.Color.white); return scratchImage; }
protected void paintComponent (Graphics graphics) { Graphics2D g = (Graphics2D)graphics; int width = getWidth() - 1; int height = getHeight() - 1; for (int i = 0, n = paletteColors.length - 1; i < n; i++) { Color color1 = paletteColors[i]; Color color2 = paletteColors[i + 1]; float point1 = i / (float)n * width; float point2 = (i + 1) / (float)n * width; g.setPaint(new GradientPaint(point1, 0, color1, point2, 0, color2, false)); g.fillRect((int)point1, 0, (int)Math.ceil(point2 - point1), height); } g.setPaint(null); g.setColor(Color.black); g.drawRect(0, 0, width, height); } }
private void redrawGradient() { Color primaryRight = Color.getHSBColor(1f - this.selectedY / (float) (size - 1), 1, 1); Graphics2D g = image.createGraphics(); GradientPaint primary = new GradientPaint( 0f, 0f, Color.WHITE, size - 1, 0f, primaryRight); GradientPaint shade = new GradientPaint( 0f, 0f, new Color(0, 0, 0, 0), 0f, size - 1, Color.BLACK); g.setPaint(primary); g.fillRect(0, 0, size, size); g.setPaint(shade); g.fillRect(0, 0, size, size); g.dispose(); forceRedraw = true; }
private void drawRect(Graphics2D g2, int w, int h) { int rx = (int) (w * Math.random() / 1.5); int ry = (int) (h * Math.random() / 1.5); int rw = (int) (w * Math.random() / 1.5); int rh = (int) (w * Math.random() / 1.5); g2.setColor(new Color((int) (Integer.MAX_VALUE * Math.random()))); g2.fillRect(rx, ry, rw, rh); }
Graphics2D g2 = (Graphics2D)g; g2.setColor( black ); g2.drawRoundRect( 1, 1, getWidth()-2 - 1, getHeight()-2 - 1, 2,2 ); g2.setColor( white ); g2.drawRoundRect( 1 + 1, 1 + 1, getWidth()-2 - 3, getHeight()-2 - 3, 2,2 ); int h = thumbBounds.height; g2.setPaint( new GradientPaint(x, (int)(y-0.1*h), colorDark , x, (int)(y+1.2*h), light) ); g2.fillRect( x, y, w, h ); g2.setPaint( new GradientPaint(x, (int)(y+.65*h), light , x, (int)(y+1.3*h), colorDark) ); g2.fillRect( x, (int)(y+.65*h), w, (int)(h-.65*h) ); g2.setColor( colorBright ); g2.fillRect(x+w/2-size/2,y+h/2-size/2, size, size); g2.setColor( new Color(120,120,120)); g2.fillRect(x+w/2-4,h/2-4, 2, 2); g2.fillRect(x+w/2-1,h/2-4, 2, 2); g2.fillRect(x+w/2+2,h/2-4, 2, 2); g2.setColor( colorDark ); g2.fillRect(x+w/2-4,h/2-2, 2, 6); g2.fillRect(x+w/2-1,h/2-2, 2, 6); g2.fillRect(x+w/2+2,h/2-2, 2, 6); g2.setColor( new Color(170,170,170)); g2.fillRect(x+w/2-4,h/2+2, 2, 2); g2.fillRect(x+w/2-1,h/2+2, 2, 2); g2.fillRect(x+w/2+2,h/2+2, 2, 2);
/** * Deletes all pixels of image and sets them to previously defined * background color. */ public final void clear() { // fill grid with background color final int bgR = (int) (this.backgroundCol >> 16); final int bgG = (int) ((this.backgroundCol >> 8) & 0xff); final int bgB = (int) (this.backgroundCol & 0xff); if (this.frame == null) { final Graphics2D gr = this.image.createGraphics(); Color c = new Color(bgR, bgG, bgB); gr.setBackground(c); gr.clearRect(0, 0, this.width, this.height); gr.setColor(c); gr.fillRect(0, 0, this.width, this.height); } else { int p = 0; for (int i = 0; i < width; i++) { this.frame[p++] = (byte) bgR; this.frame[p++] = (byte) bgG; this.frame[p++] = (byte) bgB; } final int rw = width * 3; for (int i = 1; i < height; i++) { System.arraycopy(this.frame, 0, this.frame, i * rw, rw); } } }
public void loadImage(String imageFile) throws IOException { BufferedImage wpImage = ImageIO.read(new File(imageFile)); TexturePaint paint = new TexturePaint(wpImage, new Rectangle(0, 0, wpImage.getWidth(), wpImage.getHeight())); gd.setPaint(paint); gd.fillRect(0, 0, wpImage.getWidth(), wpImage.getHeight()); } }
private void paintBackground(Graphics2D graphics, AbstractButton b) { Graphics2D g = (Graphics2D) graphics.create(); try { g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f)); Color startingColor = look.colors().shadow().brighter().brighter(); Color endingColor = look.colors().shadow().darker(); g.setPaint(new GradientPaint(0, 0, startingColor, 0, b.getHeight(), endingColor)); g.fillRect(1, 1, b.getWidth()-2, b.getHeight()-2); } finally { g.dispose(); } }
private static BufferedImage createImage (int width, int height, Color color) { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR); Graphics2D g = image.createGraphics(); g.setColor(color); g.fillRect(0, 0, width, height); g.dispose(); return image; } }
public Image(Vertx vertx, String name) { try { final BufferedImage raster = ImageIO.read(((VertxInternal) vertx).resolveFile(name)); width = raster.getWidth(); height = raster.getHeight(); data = raster.getRGB(0, 0, width, height, null, 0, width); for (int pixel : data) if (!colorMap.containsKey(pixel)) { BufferedImage offlineImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = offlineImage.createGraphics(); g2.setPaint(new Color(pixel, true)); g2.fillRect(0, 0, 1, 1); ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(offlineImage, "PNG", out); colorMap.put(pixel, Buffer.buffer().appendBytes(out.toByteArray())); out.close(); g2.dispose(); } } catch (IOException e) { throw new RuntimeException(e); } }
public void makeTransparent(Graphics2D g2) { Color col = g2.getColor(); Composite comp = null; // force transparence of this layer only. If no buffering we would clear layers below if (buffering) { comp = g2.getComposite(); g2.setComposite(AlphaComposite.Clear); } g2.setColor(new Color(0, 0, 0, 0)); g2.fillRect(0, 0, bounds.width, bounds.height); g2.setColor(col); if (comp != null) { g2.setComposite(comp); } }
protected void paintComponent (Graphics graphics) { Graphics2D g = (Graphics2D)graphics; int width = getWidth() - 1; int height = getHeight() - 1; for (int i = 0, n = paletteColors.length - 1; i < n; i++) { Color color1 = paletteColors[i]; Color color2 = paletteColors[i + 1]; float point1 = i / (float)n * width; float point2 = (i + 1) / (float)n * width; g.setPaint(new GradientPaint(point1, 0, color1, point2, 0, color2, false)); g.fillRect((int)point1, 0, (int)Math.ceil(point2 - point1), height); } g.setPaint(null); g.setColor(Color.black); g.drawRect(0, 0, width, height); } }
public static void drawRoundedBox(Graphics2D g, int x, int y, int w, int h, int bevel, Paint border, Paint fill) { g.setColor(new Color(0, 0, 0, 150)); g.drawOval(x - 1, y - 1, bevel * 2, h); g.setPaint(border); g.drawOval(x, y, bevel * 2 - 1, h - 1); g.drawOval(x + w - bevel * 2, y, bevel * 2 - 1, h - 1); g.drawOval(x + 1, y + 1, bevel * 2 - 3, h - 3); g.drawOval(x + 1 + w - bevel * 2, y + 1, bevel * 2 - 3, h - 3); g.drawRect(x + bevel, y, w - 2 * bevel, h - 1); g.drawRect(x + 1 + bevel, y + 1, w - 2 * bevel - 2, h - 3); g.setPaint(fill); g.fillOval(x + 2, y + 2, bevel * 2 - 4, h - 4); g.fillOval(x + 2 + w - bevel * 2, y + 2, bevel * 2 - 4, h - 4); g.fillRect(x + bevel, y + 2, w - 2 * bevel, h - 4); g.setPaint(fill); g.setColor(abitbrighter(g.getColor())); g.drawLine(x + 1 + bevel, y + 1, x + 1 + bevel + w - 2 * bevel - 2, y + 1); g.setPaint(fill); g.setColor(abitdarker(g.getColor())); g.drawLine(x + 1 + bevel, y + h - 2, x + 1 + bevel + w - 2 * bevel - 2, y + h - 2); }
/** Returns an image that can be used by effects as a temp image. */ static public BufferedImage getScratchImage () { Graphics2D g = (Graphics2D)scratchImage.getGraphics(); g.setComposite(AlphaComposite.Clear); g.fillRect(0, 0, GlyphPage.MAX_GLYPH_SIZE, GlyphPage.MAX_GLYPH_SIZE); g.setComposite(AlphaComposite.SrcOver); g.setColor(java.awt.Color.white); return scratchImage; }
/** * 创建{@link Graphics2D} * * @param image {@link BufferedImage} * @param color {@link Color}背景颜色以及当前画笔颜色 * @return {@link Graphics2D} * @since 3.2.3 */ public static Graphics2D createGraphics(BufferedImage image, Color color) { final Graphics2D g = image.createGraphics(); // 填充背景 g.setColor(color); g.fillRect(0, 0, image.getWidth(), image.getHeight()); return g; }
private void paintGradient(Color start, Color end, int y1, int y2) { Graphics2D graphics = image.createGraphics(); try { graphics.setPaint(new GradientPaint(0, y1, start, 0, y2, end)); graphics.fillRect(0, y1, width, y2); } finally { graphics.dispose(); } }
protected void drawSpectrum(Graphics2D g, double[] spectrum, int image_X, int image_width, int image_refY, int image_height) { double yScaleFactor = (double) image_height / spectra_indexmax; if (image_width < 2) image_width = 2; int rect_height = (int) Math.ceil(yScaleFactor); if (rect_height < 2) rect_height = 2; for (int i = 0; i < spectra_indexmax; i++) { int color; if (Double.isNaN(spectrum[i]) || spectrum[i] < spectra_max - DYNAMIC_RANGE) { color = 255; // white } else { color = (int) (255 * (spectra_max - spectrum[i]) / DYNAMIC_RANGE); } g.setColor(new Color(color, color, color)); g.fillRect(image_X, image_refY - (int) (i * yScaleFactor), image_width, rect_height); } }