List of usage examples for java.awt Graphics2D fillRect
public abstract void fillRect(int x, int y, int width, int height);
From source file:se.vgregion.webbisar.svc.impl.ImageUtil.java
/** * Resizes and overwrites the image. Will keep the original pictures dimensions. * /* w w w .ja v a2s . c o m*/ * @param sourceFile * the file to rezise * @param newLongSideSize * the new size, in pixels. * @param quality * a number between 0 and 100 where 100 gives the best quality * @throws IOException */ public synchronized static void scaleImage(File sourceFile, ImageSize imageSize, float quality) throws IOException { // System.gc(); BufferedImage sourceImage = ImageIO.read(sourceFile); int srcWidth = sourceImage.getWidth(); int srcHeight = sourceImage.getHeight(); double longSideForSource = Math.max(srcWidth, srcHeight); double longSideForDest = srcWidth > srcHeight ? imageSize.getWidth() : imageSize.getHeight(); double multiplier = longSideForDest / longSideForSource; int destWidth = (int) (srcWidth * multiplier); int destHeight = (int) (srcHeight * multiplier); BufferedImage destImage = new BufferedImage((int) imageSize.getWidth(), (int) imageSize.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D graphics = destImage.createGraphics(); graphics.setPaint(Color.WHITE); graphics.fillRect(0, 0, destImage.getWidth(), destImage.getHeight()); AffineTransform affineTransform = AffineTransform.getScaleInstance(multiplier, multiplier); AffineTransform trans = new AffineTransform(); trans.setToTranslation((imageSize.getWidth() - destWidth) / 2, (imageSize.getHeight() - destHeight) / 2); graphics.transform(trans); graphics.drawRenderedImage(sourceImage, affineTransform); saveImageAsJPEG(sourceFile.getAbsolutePath(), destImage, quality); }
From source file:SplashScreenTest.java
private static void drawOnSplash(int percent) { Rectangle bounds = splash.getBounds(); Graphics2D g = splash.createGraphics(); int height = 20; int x = 2;//from w w w. j a v a 2 s . co m int y = bounds.height - height - 2; int width = bounds.width - 4; Color brightPurple = new Color(76, 36, 121); g.setColor(brightPurple); g.fillRect(x, y, width * percent / 100, height); splash.update(); }
From source file:org.gitools.ui.app.heatmap.drawer.AbstractHeatmapDrawer.java
/** * Paint background./*from w ww .j a va 2s. c o m*/ * * @param backgroundColor the background color * @param g the g * @param box the box */ protected static void paintBackground(Color backgroundColor, Graphics2D g, Rectangle box) { g.setColor(backgroundColor); g.fillRect(box.x, box.y, box.width, box.height); }
From source file:D20140128.ApacheXMLGraphicsTest.EPSExample1.java
/** * Creates an EPS file. The contents are painted using a Graphics2D * implementation that generates an EPS file. * * @param outputFile the target file/*ww w . j av a 2 s .c om*/ * @throws IOException In case of an I/O error */ public static void generateEPSusingJava2D(File outputFile) throws IOException { OutputStream out = new java.io.FileOutputStream(outputFile); out = new java.io.BufferedOutputStream(out); try { //Instantiate the EPSDocumentGraphics2D instance EPSDocumentGraphics2D g2d = new EPSDocumentGraphics2D(false); g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext()); //Set up the document size g2d.setupDocument(out, 400, 200); //400pt x 200pt //Paint a bounding box g2d.drawRect(0, 0, 400, 200); //A few rectangles rotated and with different color Graphics2D copy = (Graphics2D) g2d.create(); int c = 12; for (int i = 0; i < c; i++) { float f = ((i + 1) / (float) c); Color col = new Color(0.0f, 1 - f, 0.0f); copy.setColor(col); copy.fillRect(70, 90, 50, 50); copy.rotate(-2 * Math.PI / (double) c, 70, 90); } copy.dispose(); //Some text g2d.rotate(-0.25); g2d.setColor(Color.RED); g2d.setFont(new Font("sans-serif", Font.PLAIN, 36)); g2d.drawString("Hello world!", 140, 140); g2d.setColor(Color.RED.darker()); g2d.setFont(new Font("serif", Font.PLAIN, 36)); g2d.drawString("Hello world!", 140, 180); //Cleanup g2d.finish(); } finally { IOUtils.closeQuietly(out); } }
From source file:de.laures.cewolf.util.Renderer.java
/** * Renders a legend//from w w w . j ava2 s .c om * @param cd the chart iamge to be rendred * @return the rendered image * @throws CewolfException */ private static RenderedImage renderLegend(ChartImage cd, Object c) throws CewolfException { try { JFreeChart chart = (JFreeChart) c; final int width = cd.getWidth(); final int height = cd.getHeight(); LegendTitle legend = getLegend(chart); boolean haslegend = true; // with JFreeChart v0.9.20, the only way to get a valid legend, // is either to retrieve it from the chart or to assign a new // one to the chart. In the case where the chart has no legend, // a new one must be assigned, but just for rendering. After, we // have to reset the legend to null in the chart. if (null == legend) { haslegend = false; legend = new LegendTitle(chart.getPlot()); } legend.setPosition(RectangleEdge.BOTTOM); BufferedImage bimage = ImageHelper.createImage(width, height); Graphics2D g = bimage.createGraphics(); g.setColor(Color.white); g.fillRect(0, 0, width, height); legend.arrange(g, new RectangleConstraint(width, height)); legend.draw(g, new Rectangle(width, height)); ByteArrayOutputStream out = new ByteArrayOutputStream(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage); param.setQuality(1.0f, true); encoder.encode(bimage, param); out.close(); // if the chart had no legend, reset it to null in order to give back the // chart in the state we received it. if (!haslegend) { removeLegend(chart); } return new RenderedImage(out.toByteArray(), "image/jpeg", new ChartRenderingInfo(new StandardEntityCollection())); } catch (IOException ioex) { log.error(ioex); throw new ChartRenderingException(ioex.getMessage(), ioex); } }
From source file:net.sqs2.omr.session.logic.PageImageRenderer.java
private static void drawPageState(PageTask pageTask, SourceConfig sourceConfig, //SourceDirectoryConfiguration configHandler, int pageIndex, float densityThreshold, FormMaster master, BufferedImage image, int focusedColumnIndex, Rectangle scope) throws IOException { PageTaskResult pageTaskResult = pageTask.getPageTaskResult(); PageTaskException pageTaskException = pageTask.getPageTaskException(); FrameConfig frameConfig = sourceConfig.getFrameConfig(); DeskewGuideAreaConfig deskewGuideAreaConfig = frameConfig.getDeskewGuideAreaConfig(); MarkRecognitionConfig markRecognizationConfig = sourceConfig.getMarkRecognitionConfig(); float headerVerticalMargin = deskewGuideAreaConfig.getHeaderVerticalMargin(); float footerVerticalMargin = deskewGuideAreaConfig.getFooterVerticalMargin(); float deskewGuideAreaHeight = deskewGuideAreaConfig.getHeight(); float deskewGuideAreaHorizontalMargin = deskewGuideAreaConfig.getHeaderVerticalMargin(); int w = image.getWidth(); int h = image.getHeight(); Graphics2D g = (Graphics2D) image.getGraphics(); g.setColor(HEADER_FOOTER_COLOR);/* w w w .jav a 2 s . co m*/ g.fillRect((int) (w * deskewGuideAreaHorizontalMargin), (int) (h * headerVerticalMargin), (int) (w - 2 * w * deskewGuideAreaHorizontalMargin), (int) (h * deskewGuideAreaHeight)); g.fillRect((int) (w * deskewGuideAreaHorizontalMargin), (int) (h - h * (footerVerticalMargin + deskewGuideAreaHeight) - 1), (int) (w - 2 * w * deskewGuideAreaHorizontalMargin), (int) (h * deskewGuideAreaHeight)); if (pageTaskResult != null) { Point2D[] corners = pageTaskResult.getDeskewGuideCenterPoints(); DeskewedImageSource pageSource = drawCorners(master, corners, image, g); drawFormAreas(pageIndex, densityThreshold, (FormMaster) master, pageTaskResult, g, markRecognizationConfig, pageSource, focusedColumnIndex, scope); } if (pageTaskException != null) { PageTaskExceptionModel model = pageTaskException.getExceptionModel(); if (model instanceof PageFrameExceptionModel) { PageFrameExceptionModel m = (PageFrameExceptionModel) model; drawCorners(master, m.getCorners(), image, g); } } }
From source file:net.sf.mcf2pdf.mcfelements.util.ImageUtil.java
/** * Loads the given CLP or SVG file and creates a BufferedImage with the given dimensions. As CLP files contain Vector images, * they can be scaled to every size needed. The contents are scaled to the given width and height, <b>not</b> preserving any * ratio of the image./*www . j a v a2 s .c o m*/ * * @param clpFile CLP or SVG file. * @param widthPixel The width, in pixels, the resulting image shall have. * @param heightPixel The height, in pixels, the resulting image shall have. * * @return An image displaying the contents of the loaded CLP file. * * @throws IOException If any I/O related problem occurs reading the file. */ public static BufferedImage loadClpFile(File clpFile, int widthPixel, int heightPixel) throws IOException { FileInputStream fis = new FileInputStream(clpFile); ClpInputStream cis = null; InputStream in = clpFile.getName().toLowerCase().endsWith(".clp") ? (cis = new ClpInputStream(fis)) : fis; UserAgentAdapter userAgentAdapter = new UserAgentAdapter(); BridgeContext bridgeContext = new BridgeContext(userAgentAdapter); SVGDocument svgDocument; GraphicsNode rootSvgNode; try { String parser = XMLResourceDescriptor.getXMLParserClassName(); SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser); svgDocument = (SVGDocument) factory.createDocument(clpFile.toURI().toString(), new InputStreamReader(in, "ISO-8859-1")); rootSvgNode = getRootNode(svgDocument, bridgeContext); } finally { IOUtils.closeQuietly(cis); IOUtils.closeQuietly(fis); } float[] vb = ViewBox.parseViewBoxAttribute(svgDocument.getRootElement(), svgDocument.getRootElement().getAttribute("viewBox"), bridgeContext); AffineTransform usr2dev = ViewBox.getPreserveAspectRatioTransform(vb, SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_NONE, true, widthPixel, heightPixel); BufferedImage img = new BufferedImage(widthPixel, heightPixel, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = img.createGraphics(); g2d.setColor(new Color(0.0f, 0.0f, 0.0f, 0.0f)); g2d.fillRect(0, 0, widthPixel, heightPixel); g2d.transform(usr2dev); // fixes "Graphics2D from BufferedImage lacks BUFFERED_IMAGE hint" - part 1 final Object oldBufferedImage = g2d.getRenderingHint(RenderingHintsKeyExt.KEY_BUFFERED_IMAGE); g2d.setRenderingHint(RenderingHintsKeyExt.KEY_BUFFERED_IMAGE, new WeakReference<BufferedImage>(img)); rootSvgNode.paint(g2d); // fixes "Graphics2D from BufferedImage lacks BUFFERED_IMAGE hint" - part 2 if (oldBufferedImage != null) g2d.setRenderingHint(RenderingHintsKeyExt.KEY_BUFFERED_IMAGE, oldBufferedImage); else g2d.getRenderingHints().remove(RenderingHintsKeyExt.KEY_BUFFERED_IMAGE); g2d.dispose(); return img; }
From source file:com.sketchy.utils.image.SketchyImage.java
public static BufferedImage toByteImage(BufferedImage image) { BufferedImage newImage = createByteImage(image.getWidth(), image.getHeight()); Graphics2D graphics = newImage.createGraphics(); graphics.setBackground(Color.white); graphics.fillRect(0, 0, image.getWidth(), image.getHeight()); graphics.drawImage(image, 0, 0, null); return newImage; }
From source file:PaintUtils.java
/** * Paints a top to bottom gradient fill over the component bounds * from color1 to color2.//from w w w .j av a 2 s . c o m */ public static void paintGradient(Graphics g, JComponent comp, Color color1, Color color2) { GradientPaint paint = new GradientPaint(0, 0, color1, 0, comp.getHeight(), color2, true); Graphics2D g2 = (Graphics2D) g; Paint oldPaint = g2.getPaint(); g2.setPaint(paint); g2.fillRect(0, 0, comp.getWidth(), comp.getHeight()); g2.setPaint(oldPaint); }
From source file:juicebox.tools.utils.juicer.apa.APAPlotter.java
/** * Method for plotting apa data/*from w w w . j a va 2 s . co m*/ * * @param data for heat map * @param axesRange initial values and increments to annotate axes [x0, dx, y0, dy] * @param outputFile where image will saved */ public static void plot(RealMatrix data, int[] axesRange, File outputFile, String title) { APARegionStatistics apaStats = new APARegionStatistics(data); DecimalFormat df = new DecimalFormat("0.000"); title += ", P2LL = " + df.format(apaStats.getPeak2LL()); // initialize heat map HeatChart map = new HeatChart(data.getData()); map.setLowValueColour(Color.WHITE); map.setHighValueColour(Color.RED); map.setXValues(axesRange[0], axesRange[1]); map.setYValues(axesRange[2], axesRange[3]); map.setTitle(title); try { // calculate dimensions for plot wrapper initializeSizes(map); // create blank white image BufferedImage apaImage = new BufferedImage(fullWidth, fullHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = apaImage.createGraphics(); g2.setBackground(Color.WHITE); g2.fillRect(0, 0, fullWidth, fullHeight); // plot in heat map, color bar, etc g2.drawImage(map.getChartImage(), 0, 0, heatmapWidth, fullHeight, null); drawHeatMapBorder(g2, map); plotColorScaleBar(g2); plotColorScaleValues(g2, map); // top left, top right, bottom left, bottom right values (from apa) drawCornerRegions(g2, map, new Dimension(APA.regionWidth, APA.regionWidth), apaStats.getRegionCornerValues()); // save data ImageIO.write(apaImage, "png", outputFile); } catch (IOException e) { e.printStackTrace(); } }