void drawLine(Graphics g, DrawObject L) { if (L == null) { return; } if ((sequencingOn) && (L.sequenceNum != currentSequenceNumDisplay)) { return; } Graphics2D g2 = (Graphics2D) g; g2.setStroke(L.drawStroke); int x1 = (int) ((L.x - minX) / (maxX - minX) * (D.width - 2 * inset)); int y1 = (int) ((L.y - minY) / (maxY - minY) * (D.height - 2.0 * inset)); int x2 = (int) ((L.x2 - minX) / (maxX - minX) * (D.width - 2 * inset)); int y2 = (int) ((L.y2 - minY) / (maxY - minY) * (D.height - 2.0 * inset)); if (L.isArrow) { drawArrow( (Graphics2D) g, inset + x1, D.height - y1 - inset, inset + x2, D.height - y2 - inset, 1.0f, L.drawStroke); } else { g.drawLine(inset + x1, D.height - y1 - inset, inset + x2, D.height - y2 - inset); } }
public static void saveJPG(Image img, String s) { BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g2 = bi.createGraphics(); g2.drawImage(img, null, null); FileOutputStream out = null; try { out = new FileOutputStream(s); } catch (java.io.FileNotFoundException io) { System.out.println("File Not Found"); } JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi); param.setQuality(0.5f, false); encoder.setJPEGEncodeParam(param); try { encoder.encode(bi); out.close(); } catch (java.io.IOException io) { System.out.println("IOException"); } }
/** * Draws a double headed arrow with arrow heads of a given width and height. * * @param aG the canvas to draw on; * @param aX1 the starting X position of the arrow; * @param aY1 the starting Y position of the arrow; * @param aX2 the ending X position of the arrow; * @param aY2 the ending Y position of the arrow; * @param aArrowWidth the total width of the arrow head; * @param aArrowHeight the total height of the arrow head. */ public static final void drawDoubleHeadedArrow( final Graphics aG, final int aX1, final int aY1, final int aX2, final int aY2, final int aArrowWidth, final int aArrowHeight) { final Graphics2D g2d = (Graphics2D) aG.create(); final int lineWidth = Math.abs(aX2 - aX1); final int threshold = (2 * aArrowWidth) + 2; try { int x1 = aX1; int x2 = aX2; if (lineWidth > threshold) { drawArrowHead(g2d, aX1, aY1, LEFT_FACING, aArrowWidth, aArrowHeight); // why x2 needs to be shifted by one pixel is beyond me... drawArrowHead(g2d, aX2 + 1, aY2, RIGHT_FACING, aArrowWidth, aArrowHeight); x1 += aArrowWidth - 1; x2 -= aArrowWidth + 1; } g2d.drawLine(x1, aY1, x2, aY2); } finally { g2d.dispose(); } }
public void draw(node leaf, Graphics2D g, int px, int py) { int lvl = leaf.getLevel(); double l = lvl; counts[lvl]++; double xfraq = counts[lvl] / (spacing[lvl] + 1); double yfraq = l / depth; int x = new Double(1600 * xfraq).intValue(); int y = new Double(1200 * yfraq).intValue() + 10; if (leaf.getAttr() != null) { g.drawString(leaf.getAttr(), x - 20, y); } if (leaf.getCrit() != null) { g.drawString(leaf.getCrit(), x - 20, y + 10); } if (leaf.getResult() != null) { g.drawString(leaf.getResult(), x - 20, y + 10); } g.drawLine(x, y, px, py); // g.fillRect(x,y,20,20); ArrayList children = leaf.getChildren(); while (!children.isEmpty()) { draw((node) children.remove(0), g, x, y); } }
// From: http://forum.java.sun.com/thread.jspa?threadID=378460&tstart=135 void drawArrow( Graphics2D g2d, int xCenter, int yCenter, int x, int y, float stroke, BasicStroke drawStroke) { double aDir = Math.atan2(xCenter - x, yCenter - y); // Line can be dashed. g2d.setStroke(drawStroke); g2d.drawLine(x, y, xCenter, yCenter); // make the arrow head solid even if dash pattern has been specified g2d.setStroke(lineStroke); Polygon tmpPoly = new Polygon(); int i1 = 12 + (int) (stroke * 2); // make the arrow head the same size regardless of the length length int i2 = 6 + (int) stroke; tmpPoly.addPoint(x, y); tmpPoly.addPoint(x + xCor(i1, aDir + .5), y + yCor(i1, aDir + .5)); tmpPoly.addPoint(x + xCor(i2, aDir), y + yCor(i2, aDir)); tmpPoly.addPoint(x + xCor(i1, aDir - .5), y + yCor(i1, aDir - .5)); tmpPoly.addPoint(x, y); // arrow tip g2d.drawPolygon(tmpPoly); // Remove this line to leave arrow head unpainted: g2d.fillPolygon(tmpPoly); }
protected void drawImageMosaic(Graphics2D g2) { // Break the image up into tiles. Draw each // tile with its own transparency, allowing // the background to show through to varying // degrees. int side = 36; int width = mImage.getWidth(); int height = mImage.getHeight(); for (int y = 0; y < height; y += side) { for (int x = 0; x < width; x += side) { // Calculate an appropriate transparency value. float xBias = (float) x / (float) width; float yBias = (float) y / (float) height; float alpha = 1.0f - Math.abs(xBias - yBias); g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)); // Draw the subimage. int w = Math.min(side, width - x); int h = Math.min(side, height - y); BufferedImage tile = mImage.getSubimage(x, y, w, h); g2.drawImage(tile, x, y, null); } } // Reset the composite. g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); }
/** * Appends a frame to the current video. * * @param image the image to append * @return true if image successfully appended */ protected boolean append(Image image) { BufferedImage bi; if (image instanceof BufferedImage) { bi = (BufferedImage) image; } else { bi = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_RGB); Graphics2D g = bi.createGraphics(); g.drawImage(image, 0, 0, null); } ByteArrayOutputStream out = new ByteArrayOutputStream(); try { if (!editing) { videoMedia.beginEdits(); editing = true; } ImageIO.write(bi, "png", out); // $NON-NLS-1$ QTHandle handle = new QTHandle(out.toByteArray()); DataRef dataRef = new DataRef(handle, kDataRefFileExtensionTag, "png"); // $NON-NLS-1$ GraphicsImporter importer = new GraphicsImporter(dataRef); ImageDescription description = importer.getImageDescription(); int duration = (int) (frameDuration * 0.6); videoMedia.addSample( handle, 0, // data offset handle.getSize(), duration, description, 1, // number of samples 0); // key frame?? } catch (Exception ex) { ex.printStackTrace(); return false; } return true; }
/** * Returns the specified image as icon. * * @param name name of icon * @return icon */ public static ImageIcon icon(final String name) { ImageIcon ii = ICONS.get(name); if (ii != null) return ii; Image img; if (GUIConstants.scale > 1) { // choose large image or none final URL url = GUIConstants.large() ? BaseXImages.class.getResource("/img/" + name + "_32.png") : null; if (url == null) { // resize low-res image if no hi-res image exists img = get(url(name)); final int w = (int) (img.getWidth(null) * GUIConstants.scale); final int h = (int) (img.getHeight(null) * GUIConstants.scale); final BufferedImage tmp = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); final Graphics2D g2 = tmp.createGraphics(); g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.drawImage(img, 0, 0, w, h, null); g2.dispose(); img = tmp; } else { img = get(url); } } else { img = get(name); } ii = new ImageIcon(img); ICONS.put(name, ii); return ii; }
@Override public int print(Graphics g, PageFormat pf, int page) throws PrinterException { if (page > 0) { return NO_SUCH_PAGE; } int i = pf.getOrientation(); // get the size of the page double pageWidth = pf.getImageableWidth(); double pageHeight = pf.getImageableHeight(); double myWidth = this.getWidth(); // - borderWidth * 2; double myHeight = this.getHeight(); // - borderWidth * 2; double scaleX = pageWidth / myWidth; double scaleY = pageHeight / myHeight; double minScale = Math.min(scaleX, scaleY); Graphics2D g2d = (Graphics2D) g; g2d.translate(pf.getImageableX(), pf.getImageableY()); g2d.scale(minScale, minScale); drawPlot(g); return PAGE_EXISTS; }
/** paint the canvas into a image file of given width and height */ public void writeToImage(String s, int w, int h) { String ext; File f; try { ext = s.substring(s.lastIndexOf(".") + 1); f = new File(s); } catch (Exception e) { System.out.println(e); return; } if (!ext.equals("jpg") && !ext.equals("png")) { System.out.println("Cannot write to file: Illegal extension " + ext); return; } boolean opq = true; if (theOpaque != null) opq = theOpaque; BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); g2.setBackground(Color.white); g2.setPaint(Color.black); g2.setStroke(new BasicStroke(1)); g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); doBuffer(g2, true, new Rectangle(0, 0, w, h)); try { ImageIO.write(image, ext, f); } catch (Exception e) { System.out.println(e); } }
/** [Internal] */ private void paintBackground(Graphics2D g2, Color theBackground) { Color color1 = g2.getColor(); if (theBackground == null) theBackground = Color.white; g2.setColor(theBackground); g2.fillRect(0, 0, 30000, 30000); g2.setColor(color1); }
private <T> T scaleImageUsingAffineTransformation(final BufferedImage bufferedImage, T target) { BufferedImage destinationImage = generateDestinationImage(); Graphics2D graphics2D = destinationImage.createGraphics(); AffineTransform transformation = AffineTransform.getScaleInstance( ((double) getQualifiedWidth() / bufferedImage.getWidth()), ((double) getQualifiedHeight() / bufferedImage.getHeight())); graphics2D.drawRenderedImage(bufferedImage, transformation); graphics2D.addRenderingHints(retrieveRenderingHints()); try { if (target instanceof File) { LOGGER.info(String.format(M_TARGET_TYPE_OF, "File")); ImageIO.write(destinationImage, imageType.toString(), (File) target); } else if (target instanceof ImageOutputStream) { LOGGER.info(String.format(M_TARGET_TYPE_OF, "ImageOutputStream")); ImageIO.write(destinationImage, imageType.toString(), (ImageOutputStream) target); } else if (target instanceof OutputStream) { LOGGER.info(String.format(M_TARGET_TYPE_OF, "OutputStream")); ImageIO.write(destinationImage, imageType.toString(), (OutputStream) target); } else { target = null; } } catch (IOException e) { e.printStackTrace(); } return target; }
// public void savePDF(){ // Rectangle suggestedPageSize = getITextPageSize(page1.getPageSize()); // Rectangle pageSize = rotatePageIfNecessary(suggestedPageSize); // //rotate if we need landscape // Document document = new Document(pageSize); // // PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile)); // document.open(); // Graphics2D graphics = cb.createGraphics(pageSize.getWidth(), pageSize.getHeight()); // // // call your GTRenderer here // GTRenderer draw = new StreamingRenderer(); // draw.setMapContent(mapContent); // // draw.paint(graphics, outputArea, mapContent.getLayerBounds() ); // // // cleanup // graphics.dispose(); // // //cleanup // document.close(); // writer.close(); // } public void saveImage(final MapContent map, final String file, final int imageWidth) { GTRenderer renderer = new StreamingRenderer(); renderer.setMapContent(map); Rectangle imageBounds = null; ReferencedEnvelope mapBounds = null; try { mapBounds = map.getMaxBounds(); double heightToWidth = mapBounds.getSpan(1) / mapBounds.getSpan(0); imageBounds = new Rectangle(0, 0, imageWidth, (int) Math.round(imageWidth * heightToWidth)); } catch (Exception e) { // failed to access mapContent layers throw new RuntimeException(e); } BufferedImage image = new BufferedImage(imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_RGB); Graphics2D gr = image.createGraphics(); gr.setPaint(Color.WHITE); gr.fill(imageBounds); try { renderer.paint(gr, imageBounds, mapBounds); File fileToSave = new File(file); ImageIO.write(image, "jpeg", fileToSave); } catch (Exception e) { e.printStackTrace(); } }
/** * Write the given text string in the current font, centered on (x, y) and rotated by the * specified number of degrees * * @param x the center x-coordinate of the text * @param y the center y-coordinate of the text * @param s the text * @param degrees is the number of degrees to rotate counterclockwise */ public static void text(double x, double y, String s, double degrees) { double xs = scaleX(x); double ys = scaleY(y); offscreen.rotate(Math.toRadians(-degrees), xs, ys); text(x, y, s); offscreen.rotate(Math.toRadians(+degrees), xs, ys); }
public ArrayList<BufferedImage> Split(BufferedImage orgBilde) throws IOException { ArrayList<BufferedImage> bilder = new ArrayList<>(); int rader = 3; int kolonner = 2; int bildeBit = rader * kolonner; int splittetBredde = orgBilde.getWidth() / kolonner; int splittetHøyde = orgBilde.getHeight() / rader; int count = 0; BufferedImage imgs[] = new BufferedImage[bildeBit]; for (int x = 0; x < rader; x++) { for (int y = 0; y < kolonner; y++) { imgs[count] = new BufferedImage(splittetBredde, splittetHøyde, orgBilde.getType()); Graphics2D gr = imgs[count++].createGraphics(); gr.drawImage( orgBilde, 0, 0, splittetBredde, splittetHøyde, splittetBredde * y, splittetHøyde * x, splittetBredde * y + splittetBredde, splittetHøyde * x + splittetHøyde, null); gr.dispose(); } } bilder.addAll(Arrays.asList(imgs)); return bilder; }
// buffered images are just better. protected static BufferedImage imageToBufferedImage(Image img) { BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g2 = bi.createGraphics(); g2.drawImage(img, null, null); return bi; }
public void paint(Graphics g) { if (loadflag && (!runflag)) { g.clearRect(0, 0, 530, 330); Graphics2D g2D = (Graphics2D) g; g2D.translate(0, 50); // 设置图像左上角为当前点 g.drawImage(iImage, 0, 0, null); // 画输入图 } }
private void shadeExt(Graphics2D g2, int r, int g, int b, int a) { g2.setPaint(new Color(r, g, b, a)); g2.fillRect(0, 0, iw, rect.y); /* _N_ */ g2.fillRect( rect.x + rect.width + 1, rect.y, iw - rect.x - rect.width - 1, rect.height + 1); /* E */ g2.fillRect(0, rect.y, rect.x, rect.height + 1); /* W */ g2.fillRect(0, rect.y + rect.height + 1, iw, ih - rect.y - rect.height - 1); /* _S_ */ }
public static BufferedImage convertToARGB(BufferedImage image) { BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = newImage.createGraphics(); g.drawImage(image, 0, 0, null); g.dispose(); return newImage; }
public static BufferedImage resize(BufferedImage image, int width, int height) { BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT); Graphics2D g2d = (Graphics2D) bi.createGraphics(); g2d.addRenderingHints( new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)); g2d.drawImage(image, 0, 0, width, height, null); g2d.dispose(); return bi; }
public void plot(Color c, int x0, int y0, int z0) { Graphics2D g = bi.createGraphics(); g.setColor(c); if (x0 >= XRES || y0 >= YRES || x0 < 0 || y0 < 0) return; if (z0 > zbuffer[x0][y0]) { g.drawLine(x0, y0, x0, y0); zbuffer[x0][y0] = z0; } }
/** * Write the given text string in the current font, left-aligned at (x, y). * * @param x the x-coordinate of the text * @param y the y-coordinate of the text * @param s the text */ public static void textLeft(double x, double y, String s) { offscreen.setFont(font); FontMetrics metrics = offscreen.getFontMetrics(); double xs = scaleX(x); double ys = scaleY(y); int hs = metrics.getDescent(); offscreen.drawString(s, (float) (xs), (float) (ys + hs)); draw(); }
// initialize default image private BufferedImage getDefaultImage(int w, int h) { BufferedImage defaultImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics2D = defaultImage.createGraphics(); graphics2D.setColor(new Color(200, 200, 200)); graphics2D.fillRect(0, 0, w, h); graphics2D.setColor(new Color(130, 130, 130)); graphics2D.drawRect(0, 0, w - 1, h - 1); return defaultImage; }
@SuppressWarnings("unchecked") private <T> T scaleImageUsingGraphics2D(final BufferedImage bufferedImage, T target) { BufferedImage destinationImage = generateDestinationImage(); Graphics2D graphics2D = destinationImage.createGraphics(); graphics2D.addRenderingHints(retrieveRenderingHints()); graphics2D.drawImage(bufferedImage, O_X, O_Y, getQualifiedWidth(), getQualifiedHeight(), null); graphics2D.dispose(); target = (T) destinationImage; return target; }
private void drawSnake(Graphics2D g) { g.setColor(panel.getColor()); for (BodyPart bodyPart : snake.getBody()) { g.fillRect( bodyPart.getX() * CELL_SIZE - CELL_SIZE, game.getScreenSize().height - (bodyPart.getY() * CELL_SIZE), CELL_SIZE, CELL_SIZE); } }
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; // Turn on antialiasing. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); drawBackground(g2); drawImageMosaic(g2); drawText(g2); }
/** * Method to load the buffered image with the passed image * * @param image the image to use */ public void load(Image image) { // get a graphics context to use to draw on the buffered image Graphics2D graphics2d = bufferedImage.createGraphics(); // draw the image on the buffered image starting at 0,0 graphics2d.drawImage(image, 0, 0, null); // show the new image show(); }
/** * Render the View to the given graphic context. This implementation render the interior first, * then the outline. */ public void paint(Graphics2D g, Rectangle2D a) { if (!a.intersects(getBounds())) return; if (image != null) { // paint bitmap g.drawImage(image, text2ModelTr, null); // debug: g.setPaint(Color.red); g.draw(this.bounds); super.paint(g, a); // possibly paint framebox if non-null } else { // paint textlayout super.paint(g, a); // possibly paint framebox if non-null AffineTransform oldAT = g.getTransform(); // paint text in black g.setPaint(Color.black); // from now on, we work in Y-direct (<0) coordinates to avoid inextricable problems with font // being mirrored... g.transform(text2ModelTr); // also include rotation textLayout.draw(g, 0.0f, 0.0f); // [pending] ajouter un cadre si areDimensionsComputed (wysiwyg du pauvre) // get back to previous transform g.setTransform(oldAT); if (DEBUG) { g.setPaint(Color.red); g.draw(bounds); } } }
public void paint(Graphics g) { gRef = (Graphics2D) g; // change size of font gRef.setFont(gRef.getFont().deriveFont(9.0f)); fmRef = g.getFontMetrics(); // Clear background if (Preferences.monochrome) { gRef.setColor(Preferences.whiteColor); } else { gRef.setColor(Preferences.backgroundColor); } gRef.fillRect(0, 0, getWidth(), getHeight()); // set colour to correct drawing colour if (Preferences.monochrome) { gRef.setColor(Preferences.blackColor); } else { gRef.setColor(Preferences.penColor); } gRef.translate(0, margin); // Call c code to draw tree gRef.scale(scale, scale); nativeDrawTree(); }
@Override public BufferedImage makeIcon( final int width, final int height, final Path2D iconShape, final boolean allowAlpha) { final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); final Graphics2D g = result.createGraphics(); g.setStroke(new BasicStroke(0.05f)); final Color c; if (allowAlpha) { c = new Color( this.color.getRed(), this.color.getGreen(), this.color.getBlue(), this.color.getAlpha()); } else { c = new Color(this.color.getRGB()); } if (iconShape != null) { g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint( RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); g.setColor(c); final Shape path = makeTransformedPathForSize(width, height, iconShape); g.fill(path); } else { g.setColor(c); g.fillRect(0, 0, width, height); } g.dispose(); return result; }