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

The following examples show how to use java.awt.Graphics2D#scale() . 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
@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 Project: blog-codes   File: mxGraphOutline.java    License: Apache License 2.0 6 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 Project: FancyBing   File: GuiBoard.java    License: 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
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 5
Source Project: netbeans   File: ImageUtilities.java    License: 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 6
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 7
@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 8
/**
 * 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 9
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 10
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 11
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 12
/**
 * 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
@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 14
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 15
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 16
Source Project: hottub   File: Test8004859.java    License: 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 17
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 18
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 19
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 Project: whyline   File: ScaleEvent.java    License: MIT License 2 votes vote down vote up
public void paint(Graphics2D g) {

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

}