Java Code Examples for java.awt.Graphics2D#scale()

The following examples show how to use java.awt.Graphics2D#scale() . 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: AwtComponentRenderer.java    From jasperreports with GNU Lesser General Public License v3.0 8 votes vote down vote up
@Override
public void render(JasperReportsContext jasperReportsContext, Graphics2D grx, Rectangle2D rectangle) 
{
	AffineTransform origTransform = grx.getTransform();
	try
	{
		Dimension size = component.getSize();

		grx.translate(rectangle.getX(), rectangle.getY());
		if (rectangle.getWidth() != size.getWidth() 
				|| rectangle.getHeight() != size.getHeight())
		{
			grx.scale(rectangle.getWidth() / size.getWidth(), 
					rectangle.getHeight() / size.getHeight());
		}
		component.paint(grx);
	}
	catch (Exception e)
	{
		throw new JRRuntimeException(e);
	}
	finally
	{
		grx.setTransform(origTransform);
	}
}
 
Example 2
Source File: mxGraphOutline.java    From blog-codes with Apache License 2.0 8 votes vote down vote up
/**
 * Paints the graph outline.
 */
public void paintGraph(Graphics g)
{
	if (graphComponent != null)
	{
		Graphics2D g2 = (Graphics2D) g;
		AffineTransform tx = g2.getTransform();

		try
		{
			Point tr = graphComponent.getGraphControl().getTranslate();
			g2.translate(translate.x + tr.getX() * scale,
					translate.y + tr.getY() * scale);
			g2.scale(scale, scale);

			// Draws the scaled graph
			graphComponent.getGraphControl().drawGraph(g2, drawLabels);
		}
		finally
		{
			g2.setTransform(tx);
		}
	}
}
 
Example 3
Source File: GuiBoard.java    From FancyBing with GNU General Public License v3.0 6 votes vote down vote up
public int print(Graphics g, PageFormat format, int page)
    throws PrinterException
{
    if (page >= 1)
    {
        return Printable.NO_SUCH_PAGE;
    }
    double width = getSize().width;
    double height = getSize().height;
    double pageWidth = format.getImageableWidth();
    double pageHeight = format.getImageableHeight();
    double scale = 1;
    if (width >= pageWidth)
        scale = pageWidth / width;
    double xSpace = (pageWidth - width * scale) / 2;
    double ySpace = (pageHeight - height * scale) / 2;
    Graphics2D g2d = (Graphics2D)g;
    g2d.translate(format.getImageableX() + xSpace,
                  format.getImageableY() + ySpace);
    g2d.scale(scale, scale);
    print(g2d);
    return Printable.PAGE_EXISTS;
}
 
Example 4
Source File: DashScaleMinWidth.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void draw(final Image img) {
    float[] dashes = {200.0f, 200.0f};
    BasicStroke bs = new BasicStroke(20.0f,
                                     BasicStroke.CAP_BUTT,
                                     BasicStroke.JOIN_MITER,
                                     1.0f,
                                     dashes,
                                     0.0f);
    Graphics2D g = (Graphics2D) img.getGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, 200, 40);
    Line2D line = new Line2D.Double(400, 400, 3600, 400);
    g.setColor(Color.BLACK);
    g.scale(0.05, 0.05);
    g.setStroke(bs);
    g.draw(line);
    g.dispose();
}
 
Example 5
Source File: MultiResolutionImageObserverTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        generateImages();
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Image image = Toolkit.getDefaultToolkit().getImage(IMAGE_NAME_1X);

        LoadImageObserver sizeObserver
                = new LoadImageObserver(WIDTH | HEIGHT);
        toolkit.prepareImage(image, -1, -1, sizeObserver);
        waitForImageLoading(sizeObserver, "The first observer is not called");

        LoadImageObserver bitsObserver
                = new LoadImageObserver(SOMEBITS | FRAMEBITS | ALLBITS);

        BufferedImage buffImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = (Graphics2D) buffImage.createGraphics();
        g2d.scale(2, 2);
        g2d.drawImage(image, 0, 0, bitsObserver);
        waitForImageLoading(bitsObserver, "The second observer is not called!");
        g2d.dispose();
    }
 
Example 6
Source File: ImageUtilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected Image createAndPaintImage(
        Component c, ColorModel colorModel, int deviceWidth, int deviceHeight, double scale)
{
    BufferedImage ret = createBufferedImage(colorModel, deviceWidth, deviceHeight);
    Graphics2D g = ret.createGraphics();
    try {
        g.clip(new Rectangle(0, 0, deviceWidth, deviceHeight));
        g.scale(scale, scale);
        icon1.paintIcon(c, g, 0, 0);
        icon2.paintIcon(c, g, x, y);
    } finally {
        g.dispose();
    }
    return ret;
}
 
Example 7
Source File: Test8004859.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static void main(final String[] args)
        throws NoninvertibleTransformException {
    final BufferedImage bi = new BufferedImage(300, 300,
                                               BufferedImage.TYPE_INT_RGB);
    final Graphics2D g = (Graphics2D) bi.getGraphics();
    test(g);
    g.translate(2.0, 2.0);
    test(g);
    g.translate(-4.0, -4.0);
    test(g);
    g.scale(2.0, 2.0);
    test(g);
    g.scale(-4.0, -4.0);
    test(g);
    g.rotate(Math.toRadians(90));
    test(g);
    g.rotate(Math.toRadians(90));
    test(g);
    g.rotate(Math.toRadians(90));
    test(g);
    g.rotate(Math.toRadians(90));
    test(g);
    g.dispose();
    if (!status) {
        throw new RuntimeException("Test failed");
    }
}
 
Example 8
Source File: JLightweightFrame.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Graphics getGraphics() {
    if (bbImage == null) return null;

    Graphics2D g = bbImage.createGraphics();
    g.setBackground(getBackground());
    g.setColor(getForeground());
    g.setFont(getFont());
    g.scale(scaleFactor, scaleFactor);
    return g;
}
 
Example 9
Source File: SEBrowserComponent.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
private void paintOnBufferImpl() {
    final BufferedImage buf = getBuffer();
    Graphics2D g2d = buf.createGraphics();
    g2d.scale(JavaSEPort.retinaScale / instance.zoomLevel, JavaSEPort.retinaScale / instance.zoomLevel);
    cmp.panel.paint(g2d);
    g2d.dispose();
    cmp.putClientProperty("__buffer", buf);
}
 
Example 10
Source File: Test8004859.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(final String[] args)
        throws NoninvertibleTransformException {
    final BufferedImage bi = new BufferedImage(300, 300,
                                               BufferedImage.TYPE_INT_RGB);
    final Graphics2D g = (Graphics2D) bi.getGraphics();
    test(g);
    g.translate(2.0, 2.0);
    test(g);
    g.translate(-4.0, -4.0);
    test(g);
    g.scale(2.0, 2.0);
    test(g);
    g.scale(-4.0, -4.0);
    test(g);
    g.rotate(Math.toRadians(90));
    test(g);
    g.rotate(Math.toRadians(90));
    test(g);
    g.rotate(Math.toRadians(90));
    test(g);
    g.rotate(Math.toRadians(90));
    test(g);
    g.dispose();
    if (!status) {
        throw new RuntimeException("Test failed");
    }
}
 
Example 11
Source File: OverviewPanel.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void paintComponent(Graphics graphics) {
	super.paintComponent(graphics);
	double scaleX = (double) getWidth() / (double) processRenderer.getWidth();
	double scaleY = (double) getHeight() / (double) processRenderer.getHeight();
	scale = Math.min(scaleX, scaleY);
	double scaledW = processRenderer.getWidth() * scale;
	double scaledH = processRenderer.getHeight() * scale;

	Graphics2D g = (Graphics2D) graphics.create();
	g.translate((getWidth() - scaledW) / 2d, (getHeight() - scaledH) / 2d);
	g.scale(scale, scale);

	g.setRenderingHints(ProcessDrawer.LOW_QUALITY_HINTS);
	processRenderer.getOverviewPanelDrawer().draw(g, true);

	g.setStroke(new BasicStroke((int) (1d / scale)));

	Rectangle visibleRect = processRenderer.getVisibleRect();
	Rectangle drawRect = new Rectangle((int) visibleRect.getX(), (int) visibleRect.getY(),
			(int) visibleRect.getWidth() - 1, (int) visibleRect.getHeight() - 1);

	g.setColor(FILL_COLOR);
	g.fill(drawRect);

	g.setColor(DRAW_COLOR);
	g.draw(drawRect);

	g.dispose();
}
 
Example 12
Source File: MisplacedBorder.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Draws standard JMenuBar and border on top of it.
 */
private BufferedImage step2(final JMenuBar menubar) {
    final BufferedImage bi2 = new BufferedImage(W, H, TYPE_INT_ARGB_PRE);
    final Graphics2D g2d2 = bi2.createGraphics();
    g2d2.scale(2, 2);
    g2d2.setColor(Color.RED);
    g2d2.fillRect(0, 0, W, H);
    menubar.paintAll(g2d2);
    menubar.getBorder().paintBorder(menubar, g2d2, menubar.getX(), menubar
            .getX(), menubar.getWidth(), menubar.getHeight());
    g2d2.dispose();
    return bi2;
}
 
Example 13
Source File: Test8004859.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(final String[] args)
        throws NoninvertibleTransformException {
    final BufferedImage bi = new BufferedImage(300, 300,
                                               BufferedImage.TYPE_INT_RGB);
    final Graphics2D g = (Graphics2D) bi.getGraphics();
    test(g);
    g.translate(2.0, 2.0);
    test(g);
    g.translate(-4.0, -4.0);
    test(g);
    g.scale(2.0, 2.0);
    test(g);
    g.scale(-4.0, -4.0);
    test(g);
    g.rotate(Math.toRadians(90));
    test(g);
    g.rotate(Math.toRadians(90));
    test(g);
    g.rotate(Math.toRadians(90));
    test(g);
    g.rotate(Math.toRadians(90));
    test(g);
    g.dispose();
    if (!status) {
        throw new RuntimeException("Test failed");
    }
}
 
Example 14
Source File: DrawCachedImageAndTransform.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    GraphicsEnvironment ge = GraphicsEnvironment
            .getLocalGraphicsEnvironment();
    GraphicsConfiguration gc = ge.getDefaultScreenDevice()
                                 .getDefaultConfiguration();
    VolatileImage vi = gc.createCompatibleVolatileImage(100, 100);

    Graphics2D g2d = vi.createGraphics();
    g2d.scale(2, 2);
    BufferedImage img = new BufferedImage(50, 50,
                                          BufferedImage.TYPE_INT_ARGB);

    g2d.drawImage(img, 10, 25, Color.blue, null);
    g2d.dispose();
}
 
Example 15
Source File: PgramUserBoundsTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
static void testAll(Graphics2D g2d) {
    g2d.setTransform(identity);
    g2d.translate(100, 100);
    testPrimitives(g2d);

    g2d.setTransform(identity);
    g2d.scale(10, 10);
    testPrimitives(g2d);

    g2d.setTransform(identity);
    g2d.rotate(Math.PI/6);
    testPrimitives(g2d);
}
 
Example 16
Source File: MisplacedBorder.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Draws standard JMenuBar.
 */
private BufferedImage step1(final JMenuBar menubar) {
    final BufferedImage bi1 = new BufferedImage(W, H, TYPE_INT_ARGB_PRE);
    final Graphics2D g2d = bi1.createGraphics();
    g2d.scale(2, 2);
    g2d.setColor(Color.RED);
    g2d.fillRect(0, 0, W, H);
    menubar.paintAll(g2d);
    g2d.dispose();
    return bi1;
}
 
Example 17
Source File: IncorrectFractionalClip.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static void drawToImage(int testId, boolean horiz, double scale,
                                BufferedImage image, boolean shape) {
    Graphics2D g = image.createGraphics();
    g.setComposite(AlphaComposite.Src);
    g.setColor(WHITE);
    g.fillRect(0, 0, bi.getWidth(), bi.getHeight());
    g.setComposite(AlphaComposite.SrcOver);
    g.setRenderingHint(KEY_STROKE_CONTROL, VALUE_STROKE_PURE);

    // set the scale in one direction
    if (horiz) {
        g.scale(scale, 1);
    } else {
        g.scale(1, scale);
    }
    // cover all units in the user space to touch all pixels in the
    // image after transform
    final int destSize = (int) Math.ceil(SIZE / scale);
    final int destW;
    final int destH;
    if (horiz) {
        destW = destSize;
        destH = SIZE;
    } else {
        destW = SIZE;
        destH = destSize;
    }
    for (int step = 0; step < destSize; ++step) {
        if (horiz) {
            if (!shape) {
                g.setClip(step, 0, 1, SIZE);
            } else{
                g.setClip(new Area(new Rectangle(step, 0, 1, SIZE)));
            }
        } else {
            if (!shape) {
                g.setClip(0, step, SIZE, 1);
            }else{
                g.setClip(new Area(new Rectangle(0, step, SIZE, 1)));
            }
        }
        switch (testId) {
            case 0:
                g.setColor(step % 2 == 0 ? RED : GREEN);
                g.fillRect(0, 0, destW, destH);
                break;
            case 1:
                g.drawImage(step % 2 == 0 ? redI : greenI, 0, 0,
                            destW, destH, null);
                break;
            case 2:
                g.setColor(step % 2 == 0 ? RED : GREEN);
                g.setStroke(STROKE);
                if (horiz) {
                    g.drawLine(step, 0, step, SIZE);
                } else {
                    g.drawLine(0, step, SIZE, step);
                }
                break;
            default:
                throw new RuntimeException();
        }
    }
    g.dispose();
}
 
Example 18
Source File: MultiResolutionImageTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
void draw(Graphics graphics, float resolution) {
    Graphics2D g2 = (Graphics2D) graphics;
    g2.scale(resolution, resolution);
    g2.setColor((resolution == 1) ? COLOR_1X : COLOR_2X);
    g2.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
}
 
Example 19
Source File: MultiResolutionImageTest.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
void draw(Graphics graphics, float resolution) {
    Graphics2D g2 = (Graphics2D) graphics;
    g2.scale(resolution, resolution);
    g2.setColor((resolution == 1) ? COLOR_1X : COLOR_2X);
    g2.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
}
 
Example 20
Source File: ScaleEvent.java    From whyline with MIT License 2 votes vote down vote up
public void paint(Graphics2D g) {

	g.scale(getSX(), getSY());		

}