/** * This function is used to draw very thin white borders over the outer edge of the raster map. * It's necessary because the map edge "bleeds" into the adjacent pixels, and we need to cover * that. * * <p>I think this quirky behaviour is possibly an iText bug */ private void addWhiteMapBorder(Image img, Document doc) { try { Color color = Color.white; int borderWidth = 1; BufferedImage bufferedTop = new BufferedImage((int) img.getScaledWidth(), borderWidth, BufferedImage.TYPE_INT_RGB); Graphics2D g1 = bufferedTop.createGraphics(); g1.setBackground(color); g1.clearRect(0, 0, bufferedTop.getWidth(), bufferedTop.getHeight()); Image top = Image.getInstance(bufferedImage2ByteArray(bufferedTop)); top.setAbsolutePosition( img.getAbsoluteX(), img.getAbsoluteY() + img.getScaledHeight() - bufferedTop.getHeight() / 2); BufferedImage bufferedBottom = new BufferedImage((int) img.getScaledWidth(), borderWidth, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = bufferedBottom.createGraphics(); g2.setBackground(color); g2.clearRect(0, 0, bufferedBottom.getWidth(), bufferedBottom.getHeight()); Image bottom = Image.getInstance(bufferedImage2ByteArray(bufferedBottom)); bottom.setAbsolutePosition( img.getAbsoluteX(), img.getAbsoluteY() - bufferedTop.getHeight() / 2); BufferedImage bufferedLeft = new BufferedImage(borderWidth, (int) img.getScaledHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D g3 = bufferedLeft.createGraphics(); g3.setBackground(color); g3.clearRect(0, 0, bufferedLeft.getWidth(), bufferedLeft.getHeight()); Image left = Image.getInstance(bufferedImage2ByteArray(bufferedLeft)); left.setAbsolutePosition( img.getAbsoluteX() - bufferedLeft.getWidth() / 2, img.getAbsoluteY()); BufferedImage bufferedRight = new BufferedImage(borderWidth, (int) img.getScaledHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D g4 = bufferedRight.createGraphics(); g4.setBackground(color); g4.clearRect(0, 0, bufferedRight.getWidth(), bufferedRight.getHeight()); Image right = Image.getInstance(bufferedImage2ByteArray(bufferedRight)); right.setAbsolutePosition( img.getAbsoluteX() + img.getScaledWidth() - bufferedRight.getWidth() / 2, img.getAbsoluteY()); doc.add(top); doc.add(bottom); doc.add(left); doc.add(right); } catch (Exception e) { e.printStackTrace(); } }
@Override void myRendering(Graphics2D display, AffineTransform boxTransform) { Rectangle visRect = getComponent().getVisibleRect(); display.setBackground(Color.WHITE); display.clearRect(visRect.x, visRect.y, visRect.width, visRect.height); final String logo = getVisConfig().getLogo(); if (logo != null && logo.length() > 0) { int targetLogoHeight = visRect.height * 8 / 10; int targetLogoWidth = visRect.width * 9 / 10; display.setFont(new Font(display.getFont().getName(), Font.BOLD, targetLogoHeight)); int trueLogoWidth = display.getFontMetrics().stringWidth(logo); double scaling = Math.min((double) targetLogoWidth / trueLogoWidth, 1); int scaledLogoHeight = (int) Math.round(scaling * targetLogoHeight); display.setFont(new Font(display.getFont().getName(), Font.PLAIN, scaledLogoHeight)); int scaledLogoWidth = display.getFontMetrics().stringWidth(logo); display.setColor(Color.LIGHT_GRAY); int xCoord = visRect.x + (visRect.width - scaledLogoWidth) / 2; int yCoord = visRect.y + (visRect.height + scaledLogoHeight) / 2; display.drawString(logo, xCoord, yCoord); } }
@Override public void paint(final Graphics g) { // Double-Buffering if (buffer == null) { buffer = createImage(this.getSize().width, this.getSize().height); gBuffer = (Graphics2D) buffer.getGraphics(); } gBuffer.clearRect(0, 0, this.getSize().width, this.getSize().height); // Antialiasing gBuffer.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int worldXoffset = controller.worldXoffsetModel.getNumber().intValue(); int worldYOffset = controller.worldYoffsetModel.getNumber().intValue(); if (cube != null) { renderGeometry(worldXoffset, worldYOffset); } g.drawImage(buffer, 0, 0, this); // Verzögerung / Delay try { Thread.sleep(controller.delayModel.getNumber().intValue()); } catch (InterruptedException e) { } rotate(); // repaint(); }
@Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setBackground(Color.LIGHT_GRAY); g2d.clearRect(0, 0, getParent().getWidth(), getParent().getHeight()); // Red line g2d.setPaint(Color.RED); // 5 visible, 5 invisible final float dash[] = {5, 5}; // 10 thick, end lines with no cap, // any joins make round, miter limit, // dash array, dash phase final BasicStroke dashed = new BasicStroke(10, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 0, dash, 0); g2d.setStroke(dashed); g2d.drawLine(100, 10, 10, 110); // White line g2d.setPaint(Color.WHITE); g2d.setStroke(new BasicStroke(10.0f)); g2d.draw(new Line2D.Float(150, 10, 10, 160)); // Blue line g2d.setPaint(Color.BLUE); Stroke solidStroke = new BasicStroke(10, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); g2d.setStroke(solidStroke); g2d.draw(new Line2D.Float(new Point2D.Float(200, 10), new Point2D.Double(10, 210))); }
public void update(URL svg) { g.setColor(new Color(0, 0, 0, 255)); g.setColor(new Color(20, 20, 20, 200)); g.setBackground(new Color(0, 0, 0, 0)); g.clearRect(0, 0, OSDParams.W, OSDParams.H); Transcoder t = new ImageTranscoder() { @Override public BufferedImage createImage(int arg0, int arg1) { return bi; } @Override public void writeImage(BufferedImage arg0, TranscoderOutput arg1) throws TranscoderException { // TODO Auto-generated method stub } }; try { TranscoderInput ti = new TranscoderInput(new FileReader(svg.getFile())); ti.setURI(svg.toURI().toString()); TranscoderOutput to = new TranscoderOutput(); t.transcode(ti, to); } catch (Exception e) { System.out.println("Failed to transcode"); e.printStackTrace(); throw new RuntimeException(e); } }
private void createImage(OutputStream out) { int width = 100; int height = 100; BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g = bi.createGraphics(); // set background color g.setBackground(Color.BLUE); g.clearRect(0, 0, width, height); // set color g.setColor(Color.RED); g.drawLine(0, 0, 99, 99); g.dispose(); bi.flush(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi); param.setQuality(1.0f, false); encoder.setJPEGEncodeParam(param); try { encoder.encode(bi); } catch (IOException e) { e.printStackTrace(); } }
public void drawBackground() { int i, x, y, width = getWidth(), height = getHeight(); imBackBuff = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D gib = (Graphics2D) imBackBuff.getGraphics(); gib.setBackground(Color.WHITE); gib.clearRect(0, 0, width, height); // draw gravity field float randShift = rand.nextFloat(); float randMult = 1.0f; // 0.6f*rand.nextFloat()+0.2f; for (y = 0; y < height; y += 4) { for (x = 0; x < width; x += 4) { float fieldStrength = 0; for (i = 0; i < planets.length; i++) { Planet p = planets[i]; double rSquared = Math.pow(p.x - x, 2) + Math.pow(p.y - y, 2); fieldStrength += p.mass / rSquared; int rgb = Color.HSBtoRGB(randMult * fieldStrength + randShift, 1.0f, 1.0f); gib.setColor(new Color(rgb)); gib.drawRect(x, y, 2, 2); } } } }
public void paint(Graphics g) { if (big == null) { return; } big.setBackground(getBackground()); big.clearRect(0, 0, w, h); h = h / ((numPools + numPools % 2) / 2); w = w / 2; int k = 0; // index of memory pool. for (int i = 0; i < 2; i++) { for (int j = 0; j < (numPools + numPools % 2) / 2; j++) { plotMemoryUsage(w * i, h * j, w, h, k); if (++k >= numPools) { i = 3; j = (numPools + numPools % 2) / 2; break; } } } g.drawImage(bimg, 0, 0, this); }
public static void saveImageResultsToPng(String prefix, ImageSearchHits hits, String queryImage) throws IOException { LinkedList<BufferedImage> results = new LinkedList<BufferedImage>(); int width = 0; for (int i = 0; i < hits.length(); i++) { // hits.score(i) // hits.doc(i).get("descriptorImageIdentifier") BufferedImage tmp = ImageIO.read(new FileInputStream(hits.doc(i).get("descriptorImageIdentifier"))); // if (tmp.getHeight() > 200) { double factor = 200d / ((double) tmp.getHeight()); tmp = ImageUtils.scaleImage(tmp, (int) (tmp.getWidth() * factor), 200); // } width += tmp.getWidth() + 5; results.add(tmp); } BufferedImage result = new BufferedImage(width, 220, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = (Graphics2D) result.getGraphics(); g2.setColor(Color.white); g2.setBackground(Color.white); g2.clearRect(0, 0, result.getWidth(), result.getHeight()); g2.setColor(Color.black); g2.setFont(Font.decode("\"Arial\", Font.BOLD, 12")); int offset = 0; int count = 0; for (Iterator<BufferedImage> iterator = results.iterator(); iterator.hasNext(); ) { BufferedImage next = iterator.next(); g2.drawImage(next, offset, 20, null); g2.drawString(hits.score(count) + "", offset + 5, 12); offset += next.getWidth() + 5; count++; } ImageIO.write( result, "PNG", new File(prefix + "_" + (System.currentTimeMillis() / 1000) + ".png")); }
public byte[] resize(byte[] img, int hauteur, int largeur) { InputStream is = new ByteArrayInputStream(img); BufferedImage in = null; try { in = ImageIO.read(is); } catch (IOException ex) { Logger.getLogger(ServletControle.class.getName()).log(Level.SEVERE, null, ex); } BufferedImage newImage = new BufferedImage(largeur, hauteur, BufferedImage.TYPE_INT_RGB); Graphics2D g = newImage.createGraphics(); try { g.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g.clearRect(0, 0, largeur, hauteur); g.drawImage(in, 0, 0, largeur, hauteur, null); } finally { g.dispose(); } byte[] bytesOut = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ImageIO.write(newImage, "jpg", baos); bytesOut = baos.toByteArray(); } catch (IOException ex) { Logger.getLogger(ServletControle.class.getName()).log(Level.SEVERE, null, ex); } return bytesOut; }
public void render(GCModel model, String chartFilePath) throws IOException { GCPreferences gcPreferences = new GCPreferences(); gcPreferences.load(); final ModelChartImpl pane = new ModelChartImpl(); pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); pane.setModel(model, gcPreferences); pane.setFootprint(model.getFootprint()); pane.setMaxPause(model.getPause().getMax()); pane.setRunningTime(model.getRunningTime()); Dimension d = new Dimension(gcPreferences.getWindowWidth(), gcPreferences.getWindowHeight()); pane.setSize(d); pane.addNotify(); pane.validate(); pane.autoSetScaleFactor(); final BufferedImage image = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_RGB); final Graphics2D graphics = image.createGraphics(); graphics.setBackground(Color.WHITE); graphics.clearRect(0, 0, image.getWidth(), image.getHeight()); pane.paint(graphics); ImageIO.write(image, "png", new File(chartFilePath)); }
public QRCodeEncoderTest() throws Exception { Qrcode qrcode = new Qrcode(); qrcode.setQrcodeErrorCorrect('M'); qrcode.setQrcodeEncodeMode('B'); qrcode.setQrcodeVersion(7); String encodeddata = "{id:10022,name:wandern}"; byte[] d = encodeddata.getBytes("GBK"); BufferedImage bi = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB); // createGraphics Graphics2D g = bi.createGraphics(); // set background g.setBackground(Color.WHITE); g.clearRect(0, 0, 139, 139); g.setColor(Color.BLACK); if (d.length > 0 && d.length < 123) { boolean[][] b = qrcode.calQrcode(d); for (int i = 0; i < b.length; i++) { for (int j = 0; j < b.length; j++) { if (b[j][i]) { g.fillRect(j * 3 + 2, i * 3 + 2, 3, 3); } } } } g.dispose(); bi.flush(); String FilePath = "C:\\QRCode.png"; File f = new File(FilePath); ImageIO.write(bi, "png", f); System.out.println("Input Encoded data is" + encodeddata); }
@Override public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; Dimension d = this.getSize(); int width = (int) d.getWidth(); int height = (int) d.getHeight(); g2.clearRect(0, 0, width, height); AffineTransform at = null; if (cachedImage == null) { renderOffscreen(); } if (getOrientation() == SwingConstants.VERTICAL) { at = AffineTransform.getScaleInstance((double) width / 10f, (double) height / DEFAULT_HEIGHT); } if (getOrientation() == SwingConstants.HORIZONTAL) { at = AffineTransform.getScaleInstance( (double) width / DEFAULT_WIDTH, (double) height / (double) 10); } drawBackground(g2); g2.drawRenderedImage(cachedImage, at); }
/** * 等比例图片缩放. * * @param srcFile 源文件 * @param destFile 目标文件 * @param destWidth 目标宽度(>0) * @param destHeight 目标高度(>0) */ public static void zoom(File srcFile, File destFile, int destWidth, int destHeight) { if (type == Type.jdk) { try { BufferedImage srcBufferedImage = ImageIO.read(srcFile); int srcWidth = srcBufferedImage.getWidth(); int srcHeight = srcBufferedImage.getHeight(); int width = destWidth; int height = destHeight; if (srcHeight >= srcWidth) { width = (int) Math.round(((destHeight * 1.0 / srcHeight) * srcWidth)); } else { height = (int) Math.round(((destWidth * 1.0 / srcWidth) * srcHeight)); } BufferedImage destBufferedImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = destBufferedImage.createGraphics(); graphics2D.setBackground(BACKGROUND_COLOR); graphics2D.clearRect(0, 0, destWidth, destHeight); graphics2D.drawImage( srcBufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), (destWidth / 2) - (width / 2), (destHeight / 2) - (height / 2), null); graphics2D.dispose(); ImageIO.write(destBufferedImage, FileUtil.getExtension(destFile), destFile); } catch (IOException e) { throw new RuntimeException(e); } } else { IMOperation operation = new IMOperation(); operation.thumbnail(destWidth, destHeight); operation.gravity("center"); operation.background(toHexEncoding(BACKGROUND_COLOR)); operation.extent(destWidth, destHeight); operation.quality((double) DEST_QUALITY); operation.addImage(srcFile.getPath()); operation.addImage(destFile.getPath()); if (type == Type.graphicsMagick) { ConvertCmd convertCmd = new ConvertCmd(true); if (graphicsMagickPath != null) { convertCmd.setSearchPath(graphicsMagickPath); } try { convertCmd.run(operation); } catch (IOException | InterruptedException | IM4JavaException e) { throw new RuntimeException(e); } } else { ConvertCmd convertCmd = new ConvertCmd(false); if (imageMagickPath != null) { convertCmd.setSearchPath(imageMagickPath); } try { convertCmd.run(operation); } catch (IOException | InterruptedException | IM4JavaException e) { throw new RuntimeException(e); } } } }
@Override public PadHardVersion createBlankVersion(Pad pad) { // TODO refactoring? // create image String filename = "0.png"; String path = getPadDirectory(pad.getId()); String wholePath = path + filename; BufferedImage image = new BufferedImage(DEFAULT_WIDTH, DEFAULT_HEIGHT, BufferedImage.TYPE_INT_BGR); // draw image Graphics2D g2 = image.createGraphics(); g2.setBackground(Color.WHITE); g2.clearRect(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT); // save image try { File file = new File(wholePath); ImageIO.write(image, "png", file); } catch (FileNotFoundException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } // save hard version record PadHardVersion version = new PadHardVersion(); version.setPad(pad); version.setVersion(0); version.setPath(wholePath); persist(version); return version; }
private void render() { Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics(); g.clearRect(0, 0, mapSizex * gridSize, mapSizey * gridSize); render(g); g.dispose(); bufferStrategy.show(); }
public static void EncoderQrCode(String content) throws IOException { Qrcode qrcode = new Qrcode(); qrcode.setQrcodeErrorCorrect('M'); qrcode.setQrcodeEncodeMode('B'); qrcode.setQrcodeVersion(7); // if(content == null || !content.equals("")){ // content = "DHCC"; // } String servicePassword = PropertiesBean.getInstance().getProperty("confg.serviceKey"); content = AESCoder.aesCbcEncrypt(content, servicePassword); byte[] bstr = content.getBytes("UTF-8"); BufferedImage bi = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB); Graphics2D g = bi.createGraphics(); g.setBackground(Color.WHITE); // g.clearRect(0, 0, 139, 139); g.setColor(Color.BLACK); // if (bstr.length > 0 && bstr.length < 123) { boolean[][] b = qrcode.calQrcode(bstr); for (int i = 0; i < b.length; i++) { for (int j = 0; j < b.length; j++) { if (b[j][i]) { g.fillRect(j * 3 + 2, i * 3 + 2, 3, 3); } } } } g.dispose(); bi.flush(); WebContextHolder.getContext().getResponse().setContentType("image/png"); ImageIO.write(bi, "png", WebContextHolder.getContext().getResponse().getOutputStream()); }
// **************************************** public void paint(Graphics g) // **************************************** { Graphics2D g2 = (Graphics2D) g; g2.setBackground(the_color); g2.clearRect(0, 0, getWidth(), getHeight()); }
public PaintJob(Box clip, Color background) { bufferCache = Context.instance().bufferedImageCache; bufferedImagePool = Context.instance().bufferedImagePool; this.clip = clip; buffer = bufferedImagePool.acquire(clip.getSize()); rootGraphics = buffer.createGraphics(); rootGraphics.setBackground(background); rootGraphics.clearRect(0, 0, clip.width, clip.height); composite = rootGraphics.getComposite(); }
private void generateGrid() { BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR_PRE); Graphics2D img = bi.createGraphics(); img.setBackground(Color.WHITE); img.clearRect(0, 0, width, height); gridImg = bi; }
public void paint(Graphics g) { System.out.println("paint"); Graphics2D g2d = (Graphics2D) g; Point1 p1, p2; n = paintInfo.size(); if (toolFlag == 2) g2d.clearRect(0, 0, getSize().width - 100, getSize().height - 100); // 清除 for (int i = 0; i < n - 1; i++) { p1 = (Point1) paintInfo.elementAt(i); p2 = (Point1) paintInfo.elementAt(i + 1); size = new BasicStroke(p1.boarder, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); g2d.setColor(p1.col); g2d.setStroke(size); if (p1.tool == p2.tool) { switch (p1.tool) { case 0: // 画笔 Line2D line1 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y); g2d.draw(line1); break; case 1: // 橡皮 g.clearRect(p1.x, p1.y, p1.boarder, p1.boarder); break; case 3: // 画直线 Line2D line2 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y); g2d.draw(line2); break; case 4: // 画圆 Ellipse2D ellipse = new Ellipse2D.Double(p1.x, p1.y, Math.abs(p2.x - p1.x), Math.abs(p2.y - p1.y)); g2d.draw(ellipse); break; case 5: // 画矩形 Rectangle2D rect = new Rectangle2D.Double(p1.x, p1.y, Math.abs(p2.x - p1.x), Math.abs(p2.y - p1.y)); g2d.draw(rect); break; case 6: // 截断,跳过 i = i + 1; break; default: } // end switch } // end if } // end for }
public static RenderedImage createRenderedImage(RenderedImage image, Color bkg) { if (bkg == null) return image; BufferedImage bufferedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D g = (Graphics2D) bufferedImage.getGraphics(); g.setBackground(bkg); g.clearRect(0, 0, image.getWidth(), image.getHeight()); g.drawRenderedImage(image, new AffineTransform()); return bufferedImage; }
public BufferedImage buildBufferFor(Panel panel) { Dimension dimension = new Dimension(panel.getWidth(), panel.getHeight()); BufferedImage buffer = bufferedImagePool.acquire(dimension); Graphics2D graphics = buffer.createGraphics(); graphics.setBackground(Colors.TRANSPARENT); graphics.clearRect(0, 0, panel.getWidth(), panel.getHeight()); panel.paintOn(graphics); graphics.dispose(); bufferCache.cache(panel, buffer); return buffer; }
public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setBackground(Color.white); g2d.clearRect(0, 0, Data.screenWidth, Data.screenHeight); synchronized (RenderableHolder.getInstance()) { for (IRenderable entity : RenderableHolder.getInstance().getRenderableList()) { if (entity.isVisible()) entity.render(g2d); } } }
@Override public void applyEffect(BufferedImage img) { filter.setRadius(radius); filter.setAngle(angle); Graphics2D buffer = img.createGraphics(); BufferedImage temp = filter.filter(img, null); buffer.setBackground(new Color(0, 0, 0, 0)); buffer.clearRect(0, 0, img.getWidth(), img.getHeight()); buffer.drawImage( temp, 0, 0, img.getWidth(), img.getHeight(), 0, 0, temp.getWidth(), temp.getHeight(), null); buffer.dispose(); }
@Override public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.clearRect(-100, -100, getWidth() + 100, getHeight() + 100); if (board == null) return; for (Bot bot : board.getBots()) { g2d.drawString( board.getSymbol(bot), bot.getPosition().x * 38, -bot.getPosition().y * 38 + 400); g2d.drawString("G" + board.getSymbol(bot), bot.getGoal().x * 38, -bot.getGoal().y * 38 + 400); } }
private static void testWriting(File file, Format format) throws IOException { System.out.println("Writing " + file); // Make the format more specific format = format.prepend( MediaTypeKey, MediaType.VIDEO, // FrameRateKey, new Rational(30, 1), // WidthKey, 320, // HeightKey, 160); // Create a buffered image for this format BufferedImage img = createImage(format); Graphics2D g = img.createGraphics(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); QuickTimeWriter out = null; try { // Create the writer out = new QuickTimeWriter(file); // Add a track to the writer out.addTrack(format); out.setVideoColorTable(0, img.getColorModel()); // initialize the animation Random rnd = new Random(0); // use seed 0 to get reproducable output g.setBackground(Color.WHITE); g.clearRect(0, 0, img.getWidth(), img.getHeight()); for (int i = 0; i < 100; i++) { // Create an animation frame g.setColor(new Color(rnd.nextInt())); g.fillOval(rnd.nextInt(img.getWidth() - 30), rnd.nextInt(img.getHeight() - 30), 30, 30); // write it to the writer out.write(0, img, 1); } } finally { // Close the writer if (out != null) { out.close(); } // Dispose the graphics object g.dispose(); } }
static { BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB); Graphics2D gi = bi.createGraphics(); gi.setBackground(Color.white); gi.clearRect(0, 0, 10, 10); GeneralPath p1 = new GeneralPath(); p1.moveTo(0, 0); p1.lineTo(5, 10); p1.lineTo(10, 0); p1.closePath(); gi.setColor(Color.lightGray); gi.fill(p1); triangles = new TexturePaint(bi, new Rectangle(0, 0, 10, 10)); bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB); gi = bi.createGraphics(); gi.setColor(Color.black); gi.fillRect(0, 0, 5, 5); gi.setColor(Color.gray); gi.fillRect(1, 1, 4, 4); blacklines = new TexturePaint(bi, new Rectangle(0, 0, 5, 5)); int w = 30; int h = 30; bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); gi = bi.createGraphics(); Color oc = Color.white; Color ic = Color.lightGray; gi.setPaint(new GradientPaint(0, 0, oc, w * .35f, h * .35f, ic)); gi.fillRect(0, 0, w / 2, h / 2); gi.setPaint(new GradientPaint(w, 0, oc, w * .65f, h * .35f, ic)); gi.fillRect(w / 2, 0, w / 2, h / 2); gi.setPaint(new GradientPaint(0, h, oc, w * .35f, h * .65f, ic)); gi.fillRect(0, h / 2, w / 2, h / 2); gi.setPaint(new GradientPaint(w, h, oc, w * .65f, h * .65f, ic)); gi.fillRect(w / 2, h / 2, w / 2, h / 2); gradient = new TexturePaint(bi, new Rectangle(0, 0, w, h)); bi = new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB); bi.setRGB(0, 0, 0xffffffff); bi.setRGB(1, 0, 0xffffffff); bi.setRGB(0, 1, 0xffffffff); bi.setRGB(1, 1, 0xff0000ff); bluedots = new TexturePaint(bi, new Rectangle(0, 0, 2, 2)); bi = new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB); bi.setRGB(0, 0, 0xffffffff); bi.setRGB(1, 0, 0xffffffff); bi.setRGB(0, 1, 0xffffffff); bi.setRGB(1, 1, 0xff00ff00); greendots = new TexturePaint(bi, new Rectangle(0, 0, 2, 2)); }
protected BufferedImage scaleImage(BufferedImage img, int width, int height, Color background) { BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = newImage.createGraphics(); try { g.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g.setBackground(background); g.clearRect(0, 0, width, height); g.drawImage(img, 0, 0, width, height, null); } finally { g.dispose(); } return newImage; }
// renders a page to the given graphics private void renderPage( PDPage page, Graphics2D graphics, int width, int height, float scaleX, float scaleY) throws IOException { graphics.clearRect(0, 0, width, height); graphics.scale(scaleX, scaleY); // TODO should we be passing the scale to PageDrawer rather than messing with Graphics? PDRectangle cropBox = page.getCropBox(); int rotationAngle = page.getRotation(); if (rotationAngle != 0) { float translateX = 0; float translateY = 0; switch (rotationAngle) { case 90: translateX = cropBox.getHeight(); break; case 270: translateY = cropBox.getWidth(); break; case 180: translateX = cropBox.getWidth(); translateY = cropBox.getHeight(); break; } graphics.translate(translateX, translateY); graphics.rotate((float) Math.toRadians(rotationAngle)); } // the end-user may provide a custom PageDrawer PageDrawerParameters parameters = new PageDrawerParameters(this, page); PageDrawer drawer = createPageDrawer(parameters); drawer.drawPage(graphics, cropBox); }