Java Graphics Draw getTranslatedGraphics(Graphics g, PageFormat pf, int pageIndex, Component component)

Here you can find the source of getTranslatedGraphics(Graphics g, PageFormat pf, int pageIndex, Component component)

Description

get Translated Graphics

License

Open Source License

Declaration

public static Graphics2D getTranslatedGraphics(Graphics g, PageFormat pf, int pageIndex, Component component) 

Method Source Code


//package com.java2s;
/*/*from  ww w. ja v  a2s .  co m*/
    
The Martus(tm) free, social justice documentation and
monitoring software. Copyright (C) 2006-2007, Beneficent
Technology, Inc. (The Benetech Initiative).
    
Martus is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later
version with the additions and exceptions described in the
accompanying Martus license file entitled "license.txt".
    
It is distributed WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, including warranties of fitness of purpose or
merchantability.  See the accompanying Martus License and
GPL license for more details on the required license terms
for this software.
    
You should have received a copy of the GNU General Public
License along with this program; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
    
To the extent this copyrighted software code is used in the 
Miradi project, it is subject to a royalty-free license to 
members of the Conservation Measures Partnership when 
used with the Miradi software as specified in the agreement 
between Benetech and WCS dated 5/1/05.
    
 * This file is based on one downloaded on 2006-07-26 from:
 *   http://www.developerdotstar.com/community/node/124
 * Based on the text on that page, the author (Rob MacGrogan) 
 * clearly expects and permits this code to be reused without any limitations.
 * I found this email address: robertmacgrogan@yahoo.com
 *
 * MacGrogan says the code was based on source from two other sources:
 * 1. The tutorial at www.apl.jhu.edu (still available on 2006-07-26), 
 * which has this copyright:
 *   (C) 1999 Marty Hall. All source code freely available for unrestricted use.
 *
 * 2. An anonymous forum post (the domain he lists is not valid)
 *
 * There is little enough code here that it could be re-invented from scratch
 * pretty easily, if necessary. 
 * 
 * Here is the copyright comment as it appears at developerdotstar:
 * ------------------------------------------------------------------------------------
 * Copied from this tutorial:
 *
 * http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-Printing.html
 *
 * And also from a post on the forums at java.swing.com. My apologies that do not have
 * a link to that post, by my hat goes off to the poster because he/she figured out the
 * sticky problem of paging properly when printing a Swing component.
 */

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;

public class Main {
    public static Graphics2D getTranslatedGraphics(Graphics g, PageFormat pf, int pageIndex, Component component) {
        Graphics2D g2 = (Graphics2D) g;
        // 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 * pf.getImageableHeight());
        // scale the page so the width fits...
        if (needsScaling(pf, component)) {
            double scale = getScale(pf, component);
            g2.scale(scale, scale);
        }
        return g2;
    }

    private static boolean needsScaling(PageFormat pf, Component component) {
        return pf.getImageableWidth() < component.getWidth();
    }

    private static double getScale(PageFormat pf, Component component) {
        if (needsScaling(pf, component))
            return pf.getImageableWidth() / component.getWidth();
        return 1.0;
    }
}

Related

  1. drawWinUpperLeft(Graphics g, int which, Color fill, int x, int y, int fmWidth, int fmHeight)
  2. drawXMajorScaleTick(Graphics g, int x, int y)
  3. fillVisible(final Graphics g, final JComponent c)
  4. fitToWidthAndHeight(Graphics2D g2, JComponent component, int width, int height)
  5. getGraphicsDevice(Component comp)
  6. getUsableScreenBounds(GraphicsConfiguration gconf)
  7. paint(Graphics2D g)
  8. paint(Graphics2D g)
  9. paint(Graphics2D g)