Java Code Examples for javax.swing.JComponent#print()

The following examples show how to use javax.swing.JComponent#print() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public static void writeToSVG(JComponent panel, File fileName) throws IOException {
  // print the panel to pdf
  int width = panel.getWidth();
  int height = panel.getWidth();
  logger.info(
      () -> MessageFormat.format("Exporting panel to SVG file (width x height; {0} x {1}): {2}",
          width, height, fileName.getAbsolutePath()));

  // Get a DOMImplementation
  DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
  org.w3c.dom.Document document = domImpl.createDocument(null, "svg", null);
  SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
  svgGenerator.setSVGCanvasSize(new Dimension(width, height));
  panel.print(svgGenerator);

  boolean useCSS = true; // we want to use CSS style attribute

  try (Writer out = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8")) {
    svgGenerator.stream(out, useCSS);
  }
}
 
Example 2
public static void writeToSVG(JComponent panel, File fileName) throws IOException {
  // print the panel to pdf
  int width = panel.getWidth();
  int height = panel.getWidth();
  logger.info(
      () -> MessageFormat.format("Exporting panel to SVG file (width x height; {0} x {1}): {2}",
          width, height, fileName.getAbsolutePath()));

  // Get a DOMImplementation
  DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
  org.w3c.dom.Document document = domImpl.createDocument(null, "svg", null);
  SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
  svgGenerator.setSVGCanvasSize(new Dimension(width, height));
  panel.print(svgGenerator);

  boolean useCSS = true; // we want to use CSS style attribute

  try (Writer out = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8")) {
    svgGenerator.stream(out, useCSS);
  }
}
 
Example 3
/**
 * Writes swing to pdf
 * 
 * @param panel
 * @param fileName
 * @throws DocumentException
 * @throws Exception
 */
public static void writeToPDF(JComponent panel, File fileName)
    throws IOException, DocumentException {
  // print the panel to pdf
  int width = panel.getWidth();
  int height = panel.getHeight();
  logger.info(
      () -> MessageFormat.format("Exporting panel to PDF file (width x height; {0} x {1}): {2}",
          width, height, fileName.getAbsolutePath()));
  Document document = new Document(new Rectangle(width, height));
  PdfWriter writer = null;
  try {
    writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
    document.open();
    PdfContentByte contentByte = writer.getDirectContent();
    PdfTemplate template = contentByte.createTemplate(width, height);
    Graphics2D g2 = new PdfGraphics2D(contentByte, width, height, new DefaultFontMapper());
    panel.print(g2);
    g2.dispose();
    contentByte.addTemplate(template, 0, 0);
    document.close();
    writer.close();
  } finally {
    if (document.isOpen()) {
      document.close();
    }
  }
}
 
Example 4
/**
 * Writes swing to EMF
 * 
 * @param panel
 * @param fileName
 * @throws Exception
 */
public static void writeToEMF(JComponent panel, File fileName) throws IOException {
  // print the panel to pdf
  int width = panel.getWidth();
  int height = panel.getWidth();
  logger.info(
      () -> MessageFormat.format("Exporting panel to EMF file (width x height; {0} x {1}): {2}",
          width, height, fileName.getAbsolutePath()));

  VectorGraphics g = new EMFGraphics2D(fileName, new Dimension(width, height));
  g.startExport();
  panel.print(g);
  g.endExport();
}
 
Example 5
Source Project: netbeans   File: ComponentPanel.java    License: Apache License 2.0 5 votes vote down vote up
@Override
    public void print(Graphics g) {
        for (JComponent component : myComponents) {
            component.print(g);
//          g.setColor(java.awt.Color.green);
//          g.drawRect(0, 0, getWidth(component), getHeight(component));
            g.translate(getWidth(component), 0);
        }
    }
 
Example 6
/**
 * Writes swing to pdf
 * 
 * @param panel
 * @param fileName
 * @throws DocumentException
 * @throws Exception
 */
public static void writeToPDF(JComponent panel, File fileName)
    throws IOException, DocumentException {
  // print the panel to pdf
  int width = panel.getWidth();
  int height = panel.getHeight();
  logger.info(
      () -> MessageFormat.format("Exporting panel to PDF file (width x height; {0} x {1}): {2}",
          width, height, fileName.getAbsolutePath()));
  Document document = new Document(new Rectangle(width, height));
  PdfWriter writer = null;
  try {
    writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
    document.open();
    PdfContentByte contentByte = writer.getDirectContent();
    PdfTemplate template = contentByte.createTemplate(width, height);
    Graphics2D g2 = new PdfGraphics2D(contentByte, width, height, new DefaultFontMapper());
    panel.print(g2);
    g2.dispose();
    contentByte.addTemplate(template, 0, 0);
    document.close();
    writer.close();
  } finally {
    if (document.isOpen()) {
      document.close();
    }
  }
}
 
Example 7
/**
 * Writes swing to EMF
 * 
 * @param panel
 * @param fileName
 * @throws Exception
 */
public static void writeToEMF(JComponent panel, File fileName) throws IOException {
  // print the panel to pdf
  int width = panel.getWidth();
  int height = panel.getWidth();
  logger.info(
      () -> MessageFormat.format("Exporting panel to EMF file (width x height; {0} x {1}): {2}",
          width, height, fileName.getAbsolutePath()));

  VectorGraphics g = new EMFGraphics2D(fileName, new Dimension(width, height));
  g.startExport();
  panel.print(g);
  g.endExport();
}
 
Example 8
void printComponent(JComponent c, TestBufferedImage i) {
    Graphics g = i.getGraphics();
    g.setColor(c.getBackground());
    g.fillRect(0, 0, i.getWidth(), i.getHeight());
    c.print(g);
}
 
Example 9
void printComponent(JComponent c, TestBufferedImage i) {
    Graphics g = i.getGraphics();
    g.setColor(c.getBackground());
    g.fillRect(0, 0, i.getWidth(), i.getHeight());
    c.print(g);
}
 
Example 10
Source Project: jdk8u60   File: bug4337267.java    License: GNU General Public License v2.0 4 votes vote down vote up
void printComponent(JComponent c, TestBufferedImage i) {
    Graphics g = i.getGraphics();
    g.setColor(c.getBackground());
    g.fillRect(0, 0, i.getWidth(), i.getHeight());
    c.print(g);
}
 
Example 11
void printComponent(JComponent c, TestBufferedImage i) {
    Graphics g = i.getGraphics();
    g.setColor(c.getBackground());
    g.fillRect(0, 0, i.getWidth(), i.getHeight());
    c.print(g);
}
 
Example 12
void printComponent(JComponent c, TestBufferedImage i) {
    Graphics g = i.getGraphics();
    g.setColor(c.getBackground());
    g.fillRect(0, 0, i.getWidth(), i.getHeight());
    c.print(g);
}
 
Example 13
void printComponent(JComponent c, TestBufferedImage i) {
    Graphics g = i.getGraphics();
    g.setColor(c.getBackground());
    g.fillRect(0, 0, i.getWidth(), i.getHeight());
    c.print(g);
}
 
Example 14
void printComponent(JComponent c, TestBufferedImage i) {
    Graphics g = i.getGraphics();
    g.setColor(c.getBackground());
    g.fillRect(0, 0, i.getWidth(), i.getHeight());
    c.print(g);
}
 
Example 15
Source Project: hottub   File: bug4337267.java    License: GNU General Public License v2.0 4 votes vote down vote up
void printComponent(JComponent c, TestBufferedImage i) {
    Graphics g = i.getGraphics();
    g.setColor(c.getBackground());
    g.fillRect(0, 0, i.getWidth(), i.getHeight());
    c.print(g);
}
 
Example 16
void printComponent(JComponent c, TestBufferedImage i) {
    Graphics g = i.getGraphics();
    g.setColor(c.getBackground());
    g.fillRect(0, 0, i.getWidth(), i.getHeight());
    c.print(g);
}
 
Example 17
void printComponent(JComponent c, TestBufferedImage i) {
    Graphics g = i.getGraphics();
    g.setColor(c.getBackground());
    g.fillRect(0, 0, i.getWidth(), i.getHeight());
    c.print(g);
}
 
Example 18
void printComponent(JComponent c, TestBufferedImage i) {
    Graphics g = i.getGraphics();
    g.setColor(c.getBackground());
    g.fillRect(0, 0, i.getWidth(), i.getHeight());
    c.print(g);
}
 
Example 19
void printComponent(JComponent c, TestBufferedImage i) {
    Graphics g = i.getGraphics();
    g.setColor(c.getBackground());
    g.fillRect(0, 0, i.getWidth(), i.getHeight());
    c.print(g);
}
 
Example 20
void printComponent(JComponent c, TestBufferedImage i) {
    Graphics g = i.getGraphics();
    g.setColor(c.getBackground());
    g.fillRect(0, 0, i.getWidth(), i.getHeight());
    c.print(g);
}