List of usage examples for java.awt PrintJob getGraphics
public abstract Graphics getGraphics();
From source file:MainClass.java
public static void main(String args[]) { String name = "Test print job"; Properties properties = new Properties(); PrintJob pj = Toolkit.getDefaultToolkit().getPrintJob(new MainClass(), name, properties); if (pj != null) { printDimensions(pj.getGraphics(), pj.getPageDimension()); pj.end();/*from w w w .j av a 2 s . co m*/ } }
From source file:PrintSampleApp.java
public PrintSampleApp() { add("Center", canvas); setSize(500, 500);/*from w w w .j a va 2s .c om*/ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); String name = "Test print job"; Properties properties = new Properties(); PrintJob pj = Toolkit.getDefaultToolkit().getPrintJob(PrintSampleApp.this, name, properties); if (pj != null) { canvas.printAll(pj.getGraphics()); pj.end(); } }
From source file:JuliaSet2.java
public void print() { // Create some attributes objects. This is Java 1.3 stuff. // In Java 1.1, we'd use a java.util.Preferences object instead. JobAttributes jattrs = new JobAttributes(); PageAttributes pattrs = new PageAttributes(); // Set some example attributes: monochrome, landscape mode pattrs.setColor(PageAttributes.ColorType.MONOCHROME); pattrs.setOrientationRequested(PageAttributes.OrientationRequestedType.LANDSCAPE); // Print to file by default jattrs.setDestination(JobAttributes.DestinationType.FILE); jattrs.setFileName("juliaset.ps"); // Look up the Frame that holds this component Component frame = this; while (!(frame instanceof Frame)) frame = frame.getParent();// w w w .j av a 2 s. c o m // Get a PrintJob object to print the Julia set with. // The getPrintJob() method displays a print dialog and allows the user // to override and modify the default JobAttributes and PageAttributes Toolkit toolkit = this.getToolkit(); PrintJob job = toolkit.getPrintJob((Frame) frame, "JuliaSet1", jattrs, pattrs); // We get a null PrintJob if the user clicked cancel if (job == null) return; // Get a Graphics object from the PrintJob. // We print simply by drawing to this Graphics object. Graphics g = job.getGraphics(); // Center the image on the page Dimension pagesize = job.getPageDimension(); // how big is page? Dimension panesize = this.getSize(); // how big is image? g.translate((pagesize.width - panesize.width) / 2, // center it (pagesize.height - panesize.height) / 2); // Draw a box around the Julia Set and label it g.drawRect(-1, -1, panesize.width + 2, panesize.height + 2); g.drawString("Julia Set for c={" + cx + "," + cy + "}", 0, -15); // Set a clipping region g.setClip(0, 0, panesize.width, panesize.height); // Now print the component by calling its paint method this.paint(g); // Finally tell the printer we're done with the page. // No output will be generated if we don't call dispose() here. g.dispose(); }
From source file:PrintTestApp.java
public PrintTestApp() { super("PrintTestApp"); toolkit = getToolkit();/*from w ww .jav a2s. c o m*/ add("Center", textArea); setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); String name = "Test print job"; Properties properties = new Properties(); PrintJob pj = toolkit.getPrintJob(PrintTestApp.this, name, properties); if (pj == null) textArea.setText("A null PrintJob was returned."); else { String output = "Name: " + name + "\nProperties: " + properties.toString(); Dimension pageDim = pj.getPageDimension(); int resolution = pj.getPageResolution(); boolean lastPageFirst = pj.lastPageFirst(); output += "\nPage dimension (in pixels):"; output += "\n height: " + String.valueOf(pageDim.height); output += "\n width: " + String.valueOf(pageDim.width); output += "\nResolution (pixels/inch): " + String.valueOf(resolution); output += "\nLast Page First: " + String.valueOf(lastPageFirst); textArea.setText(output); Graphics g = pj.getGraphics(); g.dispose(); pj.end(); } }
From source file:org.opensha.commons.util.FileUtils.java
/** * Prints a Text file//from w w w. j a v a 2 s . com * @param pjob PrintJob created using getToolkit().getPrintJob(JFrame,String,Properties); * @param pg Graphics * @param textToPrint String */ public static void print(PrintJob pjob, Graphics pg, String textToPrint) { int margin = 60; int pageNum = 1; int linesForThisPage = 0; int linesForThisJob = 0; // Note: String is immutable so won't change while printing. if (!(pg instanceof PrintGraphics)) { throw new IllegalArgumentException("Graphics context not PrintGraphics"); } StringReader sr = new StringReader(textToPrint); LineNumberReader lnr = new LineNumberReader(sr); String nextLine; int pageHeight = pjob.getPageDimension().height - margin; Font helv = new Font("Monaco", Font.PLAIN, 12); //have to set the font to get any output pg.setFont(helv); FontMetrics fm = pg.getFontMetrics(helv); int fontHeight = fm.getHeight(); int fontDescent = fm.getDescent(); int curHeight = margin; try { do { nextLine = lnr.readLine(); if (nextLine != null) { if ((curHeight + fontHeight) > pageHeight) { // New Page if (linesForThisPage == 0) break; pageNum++; linesForThisPage = 0; pg.dispose(); pg = pjob.getGraphics(); if (pg != null) { pg.setFont(helv); } curHeight = 0; } curHeight += fontHeight; if (pg != null) { pg.drawString(nextLine, margin, curHeight - fontDescent); linesForThisPage++; linesForThisJob++; } } } while (nextLine != null); } catch (EOFException eof) { // Fine, ignore } catch (Throwable t) { // Anything else t.printStackTrace(); } }