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

The following examples show how to use javax.swing.JComponent#print() . 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
Source File: SwingExportUtil.java    From mzmine3 with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: SwingExportUtil.java    From mzmine3 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 3
Source File: SwingExportUtil.java    From mzmine2 with GNU General Public License v2.0 6 votes vote down vote up
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 4
Source File: SwingExportUtil.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 File: SwingExportUtil.java    From mzmine2 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 6
Source File: ComponentPanel.java    From netbeans with 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 7
Source File: SwingExportUtil.java    From mzmine2 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 8
Source File: bug4337267.java    From jdk8u-dev-jdk with 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 9
Source File: bug4337267.java    From jdk8u-jdk with 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 10
Source File: bug4337267.java    From jdk8u_jdk with 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
Source File: bug4337267.java    From openjdk-8 with 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 12
Source File: bug4337267.java    From openjdk-8-source with 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 13
Source File: bug4337267.java    From hottub with 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 14
Source File: bug4337267.java    From jdk8u-jdk with 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 15
Source File: bug4337267.java    From openjdk-jdk9 with 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
Source File: bug4337267.java    From openjdk-jdk8u-backup with 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 17
Source File: bug4337267.java    From openjdk-jdk8u with 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 18
Source File: bug4337267.java    From jdk8u60 with 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 19
Source File: bug4337267.java    From TencentKona-8 with 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 20
Source File: bug4337267.java    From dragonwell8_jdk with 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);
}