/** 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); } }
@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; }
/** [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); }
public int print(Graphics g, PageFormat pf, int page) throws PrinterException { Graphics2D g2 = (Graphics2D) g; if (page > getPageCount(g2, pf)) return Printable.NO_SUCH_PAGE; g2.translate(pf.getImageableX(), pf.getImageableY()); drawPage(g2, pf, page); return Printable.PAGE_EXISTS; }
public int print(Graphics g, PageFormat pgFmt, int pgIndex) { if (pgIndex > 0) return Printable.NO_SUCH_PAGE; Graphics2D g2d = (Graphics2D) g; g2d.translate(pgFmt.getImageableX(), pgFmt.getImageableY()); doPaint(g2d); return Printable.PAGE_EXISTS; }
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(); }
public static BufferedImage tileImage(BufferedImage im, int width, int height) { GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice() .getDefaultConfiguration(); int transparency = Transparency.OPAQUE; // Transparency.BITMASK; BufferedImage compatible = gc.createCompatibleImage(width, height, transparency); Graphics2D g = (Graphics2D) compatible.getGraphics(); g.setPaint(new TexturePaint(im, new Rectangle(0, 0, im.getWidth(), im.getHeight()))); g.fillRect(0, 0, width, height); return compatible; }
public void setPenColour(int colour) { if (Preferences.monochrome) { gRef.setColor(Preferences.blackColor); } else { if (colour == 0) { gRef.setColor(Preferences.penColor); } if (colour == 1) { gRef.setColor(Preferences.highlightColor); } } }
/** * Draws 1/2" crop marks in the corners of the page. * * @param g2 the graphics context * @param pf the page format */ public void drawCropMarks(Graphics2D g2, PageFormat pf) { final double C = 36; // crop mark length = 1/2 inch double w = pf.getImageableWidth(); double h = pf.getImageableHeight(); g2.draw(new Line2D.Double(0, 0, 0, C)); g2.draw(new Line2D.Double(0, 0, C, 0)); g2.draw(new Line2D.Double(w, 0, w, C)); g2.draw(new Line2D.Double(w, 0, w - C, 0)); g2.draw(new Line2D.Double(0, h, 0, h - C)); g2.draw(new Line2D.Double(0, h, C, h)); g2.draw(new Line2D.Double(w, h, w, h - C)); g2.draw(new Line2D.Double(w, h, w - C, h)); }
public int print(Graphics g,PageFormat pf,int pageIndex) { if (pageIndex == 0) { Graphics2D g2d= (Graphics2D)g; g2d.translate(pf.getImageableX(), pf.getImageableY()); g2d.setColor(Color.black); g2d.drawString("example string", 250, 250); g2d.fillRect(0, 0, 200, 200); return Printable.PAGE_EXISTS; } else { return Printable.NO_SUCH_PAGE; } }
/** [Internal] */ public int print(Graphics g, PageFormat pf, int pi) throws PrinterException { if (pi >= 1) { return Printable.NO_SUCH_PAGE; } RepaintManager currentManager = RepaintManager.currentManager(this); currentManager.setDoubleBufferingEnabled(false); Graphics2D g2 = (Graphics2D) g; initState(g2, true); g2.translate((int) (pf.getImageableX() + 1), (int) (pf.getImageableY() + 1)); g2.scale(printScale, printScale); doBuffer(g2, true, null); currentManager.setDoubleBufferingEnabled(true); return Printable.PAGE_EXISTS; }
private synchronized void doBuffer(Graphics2D g2, boolean opq, Rectangle rt) { origTransform = g2.getTransform(); if (opq && rt != null) g2.clearRect(rt.x, rt.y, rt.width, rt.height); g2.setPaint(origPaint); g2.setFont(origFont); g2.setStroke(origStroke); if (inBuffer) { // System.out.println("upps"); for (int i = 0; i < a1x.size(); i++) doPaint(g2, a1x.get(i), a2x.get(i)); origTransform = null; return; } for (int i = 0; i < a1.size(); i++) doPaint(g2, a1.get(i), a2.get(i)); origTransform = null; }
@Override public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException { System.out.println("PageFormat: width=" + pf.getWidth() + ", height=" + pf.getHeight()); Logger.getGlobal().log(Level.INFO, "PageFormat {0}", pf); System.out.println("pageIndex " + pageIndex); Logger.getGlobal().log(Level.INFO, "pageIndex {0}", pageIndex); if (pageIndex == 0) { Graphics2D g2d = (Graphics2D) g; Font font = g2d.getFont(); g2d.setFont(font.deriveFont((float) fontSize)); g2d.translate(pf.getImageableX(), pf.getImageableY()); g2d.setColor(Color.black); int step = g2d.getFont().getSize(); step += step / 4; double y = paddingTop + g2d.getFont().getSize(); for (String s : printStringList) { Logger.getGlobal().log(Level.INFO, "printStringList: {0}", s); g2d.drawString(s, (float) paddingLeft, (float) y); y += step; } // g2d.fillRect(0, 0, 200, 200); return Printable.PAGE_EXISTS; } else { return Printable.NO_SUCH_PAGE; } }
public void drawPage(Graphics2D g2, PageFormat pf, int page) { if (message.equals("")) return; page--; // account for cover page drawCropMarks(g2, pf); g2.clip(new Rectangle2D.Double(0, 0, pf.getImageableWidth(), pf.getImageableHeight())); g2.translate(-page * pf.getImageableWidth(), 0); g2.scale(scale, scale); FontRenderContext context = g2.getFontRenderContext(); Font f = new Font("Serif", Font.PLAIN, 72); TextLayout layout = new TextLayout(message, f, context); AffineTransform transform = AffineTransform.getTranslateInstance(0, layout.getAscent()); Shape outline = layout.getOutline(transform); g2.draw(outline); }
private void fillPolyline(int x[], int y[], int length, int symbolType) { if (!Preferences.monochrome) { setFillColor(symbolType); gRef.fillPolygon(x, y, length); restoreColor(); } }
private void fillRectangle(int x, int y, int width, int height, int symbolType) { if (!Preferences.monochrome) { setFillColor(symbolType); gRef.fillRect(x, y, width, height); restoreColor(); } }
private void drawString(String str, int x, int y) { // text does not work correctly for very small scales. if (textEnabled && scale > 0.05) { gRef.drawString(str, x, y); } }
private void fillArc( int x, int y, int width, int height, int startAngle, int arcAngle, int symbolType) { if (!Preferences.monochrome) { setFillColor(symbolType); gRef.fillArc(x, y, width, height, startAngle, arcAngle); restoreColor(); } }
/** * Repaints the text. * * @param gfx The graphics context */ public void paint(Graphics gfx) { Graphics2D g2 = (Graphics2D) gfx; g2.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, antialias ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); tabSize = fm.charWidth(' ') * ((Integer) textArea.getDocument().getProperty(PlainDocument.tabSizeAttribute)) .intValue(); Rectangle clipRect = gfx.getClipBounds(); gfx.setColor(getBackground()); gfx.fillRect(clipRect.x, clipRect.y, clipRect.width, clipRect.height); // We don't use yToLine() here because that method doesn't // return lines past the end of the document int height = fm.getHeight(); int firstLine = textArea.getFirstLine(); int firstInvalid = firstLine + clipRect.y / height; // Because the clipRect's height is usually an even multiple // of the font height, we subtract 1 from it, otherwise one // too many lines will always be painted. int lastInvalid = firstLine + (clipRect.y + clipRect.height - 1) / height; try { TokenMarker tokenMarker = textArea.getDocument().getTokenMarker(); int x = textArea.getHorizontalOffset(); for (int line = firstInvalid; line <= lastInvalid; line++) { paintLine(gfx, tokenMarker, line, x); } if (tokenMarker != null && tokenMarker.isNextLineRequested()) { int h = clipRect.y + clipRect.height; repaint(0, h, getWidth(), getHeight() - h); } } catch (Exception e) { System.err.println( "Error repainting line" + " range {" + firstInvalid + "," + lastInvalid + "}:"); e.printStackTrace(); } }
/** This method is required to implement the Printable interface */ public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex >= 1) { return NO_SUCH_PAGE; } Graphics2D g2 = (Graphics2D) g; Dimension cs = printTarget.getSize(); g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); double imageableWidth = pageFormat.getImageableWidth(); double imageableHeight = pageFormat.getImageableHeight(); double scale = 1; if (cs.width >= imageableWidth) { scale = imageableWidth / cs.width; } g2.scale(scale, scale); // g2.translate((imageableWidth - cs.width)*scale/2, // (imageableHeight - cs.height)*scale/2); printTarget.paintAll(g2); return Printable.PAGE_EXISTS; }
/** * Gets the page count of this section. * * @param g2 the graphics context * @param pf the page format * @return the number of pages needed */ public int getPageCount(Graphics2D g2, PageFormat pf) { if (message.equals("")) return 0; FontRenderContext context = g2.getFontRenderContext(); Font f = new Font("Serif", Font.PLAIN, 72); Rectangle2D bounds = f.getStringBounds(message, context); scale = pf.getImageableHeight() / bounds.getHeight(); double width = scale * bounds.getWidth(); int pages = (int) Math.ceil(width / pf.getImageableWidth()); return pages; }
public void doPaint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.black); BufferedImage bimg = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB); Graphics ig = bimg.getGraphics(); Color alphared = new Color(255, 0, 0, 128); Color alphagreen = new Color(0, 255, 0, 128); Color alphablue = new Color(0, 0, 255, 128); ig.setColor(alphared); ig.fillRect(0, 0, 200, 200); ig.setColor(alphagreen); ig.fillRect(25, 25, 150, 150); ig.setColor(alphablue); ig.fillRect(75, 75, 125, 125); g.drawImage(bimg, 10, 25, this); GradientPaint gp = new GradientPaint(10.0f, 10.0f, alphablue, 210.0f, 210.0f, alphared, true); g2.setPaint(gp); g2.fillRect(10, 240, 200, 200); }
private synchronized void toBuffer(int s, Object a) { a1.add(s); a2.add(a); if (s == clear) { clearBuffer(); } if (s == opaque) theOpaque = (Boolean) a; if (s == setBackground) theBackground = (Color) a; if (inBuffer) return; if (isSetter(s)) return; Graphics g = getGraphics(); if (g == null) return; Graphics2D g2 = (Graphics2D) g; g2.setPaint(origPaint); g2.setFont(origFont); g2.setStroke(origStroke); for (int i = 0; i < a1.size() - 1; i++) { int s1 = a1.get(i); Object s2 = a2.get(i); if (isSetter(s1)) doPaint(g2, s1, s2); } doPaint((Graphics2D) g, s, a); }
public int print(Graphics g, PageFormat pf, int pageIndex) { int response = NO_SUCH_PAGE; Graphics2D g2 = (Graphics2D) g; disableDoubleBuffering(componentToBePrinted); Dimension d = componentToBePrinted.getSize(); double panelWidth = d.width; double panelHeight = d.height; double pageHeight = pf.getImageableHeight(); double pageWidth = pf.getImageableWidth(); double scale = pageWidth / panelWidth; int totalNumPages = (int) Math.ceil(scale * panelHeight / pageHeight); if (pageIndex >= totalNumPages) { response = NO_SUCH_PAGE; } else { g2.translate(pf.getImageableX(), pf.getImageableY()); g2.translate(0f, -pageIndex * pageHeight); g2.scale(scale, scale); componentToBePrinted.paint(g2); enableDoubleBuffering(componentToBePrinted); response = Printable.PAGE_EXISTS; } return response; }
public int print(Graphics g, PageFormat pageFormat, int pageIndex) { int lineHeight = fm.getHeight(); int linesPerPage = (int) (pageFormat.getImageableHeight() / lineHeight); int lineCount = textArea.getLineCount(); int lastPage = lineCount / linesPerPage; if (pageIndex > lastPage) { return NO_SUCH_PAGE; } else { Graphics2D g2d = (Graphics2D) g; TokenMarker tokenMarker = textArea.getDocument().getTokenMarker(); int firstLine = pageIndex * linesPerPage; g2d.translate( Math.max(54, pageFormat.getImageableX()), pageFormat.getImageableY() - firstLine * lineHeight); printing = true; for (int line = firstLine; line < firstLine + linesPerPage; line++) { paintLine(g2d, tokenMarker, line, 0); } printing = false; return PAGE_EXISTS; } }
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException { // Component printMe = getPrintComponent(); Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.black); // set default foreground color to black // for faster printing, turn off double buffering // RepaintManager.currentManager(this).setDoubleBufferingEnabled(false); Dimension d = printComponent.getSize(); // get size of document double panelWidth = d.width; // width in pixels double panelHeight = d.height; // height in pixels double pageHeight = pf.getImageableHeight(); // height of printer page double pageWidth = pf.getImageableWidth(); // width of printer page double scale = pageWidth / panelWidth; int totalNumPages = (int) Math.ceil(scale * panelHeight / pageHeight); // make sure we don't print empty pages if (pageIndex >= totalNumPages) { return Printable.NO_SUCH_PAGE; } // shift Graphic to line up with beginning of print-imageable region g2.translate(pf.getImageableX(), pf.getImageableY()); // shift Graphic to line up with beginning of next page to print g2.translate(0f, -pageIndex * pageHeight); // scale the page so the width fits... g2.scale(scale, scale); // PRINT IT! printComponent.paint(g2); return Printable.PAGE_EXISTS; }
public static BufferedImage rotateImage(BufferedImage bi) { int w = bi.getWidth(); int h = bi.getHeight(); BufferedImage image = new BufferedImage(h, w, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.setPaint(Color.white); // getBackground()); g2.fillRect(0, 0, h, w); g2.rotate(90 * Math.PI / 180); g2.drawImage(bi, 0, -h, w, h, null); // this); g2.dispose(); return image; }
public static BufferedImage cropImage(BufferedImage bi, int x, int y, int w, int h) { BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.setPaint(Color.white); g2.fillRect(0, 0, w, h); g2.drawImage(bi, -x, -y, null); // this); g2.dispose(); return image; }
/* (non-Javadoc) * @see java.awt.print.Printable#print(java.awt.Graphics, java.awt.print.PageFormat, int) */ public int print(Graphics g, PageFormat pf, int page) throws PrinterException { int ret = PAGE_EXISTS; String line = null; try { if (fph.knownPage(page)) { in.seek(fph.getFileOffset(page)); line = in.readLine(); } else { long offset = in.getFilePointer(); line = in.readLine(); if (line == null) { ret = NO_SUCH_PAGE; } else { fph.createPage(page); fph.setFileOffset(page, offset); } } if (ret == PAGE_EXISTS) { // Seite ausgeben, Grafikkontext vorbereiten Graphics2D g2 = (Graphics2D) g; g2.scale(1.0 / RESMUL, 1.0 / RESMUL); int ypos = (int) pf.getImageableY() * RESMUL; int xpos = ((int) pf.getImageableX() + 2) * RESMUL; int yd = 12 * RESMUL; int ymax = ypos + (int) pf.getImageableHeight() * RESMUL - yd; // Seitentitel ausgeben ypos += yd; g2.setColor(Color.black); g2.setFont(new Font("Monospaced", Font.BOLD, 10 * RESMUL)); g.drawString(fbname + " Seite " + (page + 1), xpos, ypos); g.drawLine( xpos, ypos + 6 * RESMUL, xpos + (int) pf.getImageableWidth() * RESMUL, ypos + 6 * RESMUL); ypos += 2 * yd; // Zeilen ausgeben g2.setColor(new Color(0, 0, 127)); g2.setFont(new Font("Monospaced", Font.PLAIN, 10 * RESMUL)); while (line != null) { g.drawString(line, xpos, ypos); ypos += yd; if (ypos >= ymax) { break; } line = in.readLine(); } } } catch (IOException e) { throw new PrinterException(e.toString()); } return ret; }
public static BufferedImage scaleImage(BufferedImage bi, double scale) { int w1 = (int) (Math.round(scale * bi.getWidth())); int h1 = (int) (Math.round(scale * bi.getHeight())); BufferedImage image = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.setPaint(Color.white); g2.fillRect(0, 0, w1, h1); g2.drawImage(bi, 0, 0, w1, h1, null); // this); g2.dispose(); return image; }