Example usage for java.awt Graphics2D scale

List of usage examples for java.awt Graphics2D scale

Introduction

In this page you can find the example usage for java.awt Graphics2D scale.

Prototype

public abstract void scale(double sx, double sy);

Source Link

Document

Concatenates the current Graphics2D Transform with a scaling transformation Subsequent rendering is resized according to the specified scaling factors relative to the previous scaling.

Usage

From source file:SwingTimerBasedAnimationScaleRotate.java

public void paint(Graphics g) {
    int h = getHeight();
    int w = getWidth();

    Graphics2D g2d = (Graphics2D) g;

    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    g2d.translate(w / 2, h / 2);//from   w  ww .  j av a2  s  .c  o m
    g2d.rotate(angle);
    g2d.scale(scale, scale);

    g2d.fill(r);
}

From source file:org.shredzone.commons.captcha.impl.DefaultCaptchaGenerator.java

/**
 * Draws a single character./*w w w  .ja  va  2 s . co m*/
 *
 * @param g2d
 *            {@link Graphics2D} context
 * @param ch
 *            character to draw
 * @param x
 *            left x position of the character
 * @param boxWidth
 *            width of the box
 */
private void drawCharacter(Graphics2D g2d, char ch, int x, int boxWidth) {
    double degree = (rnd.nextDouble() * rotationAmplitude * 2) - rotationAmplitude;
    double scale = 1 - (rnd.nextDouble() * scaleAmplitude / 100);

    Graphics2D cg2d = (Graphics2D) g2d.create();
    cg2d.setFont(font.deriveFont(fontSize));

    cg2d.translate(x + (boxWidth / 2), height / 2);
    cg2d.rotate(degree * PI / 90);
    cg2d.scale(scale, scale);

    FontMetrics fm = cg2d.getFontMetrics();
    int charWidth = fm.charWidth(ch);
    int charHeight = fm.getAscent() + fm.getDescent();

    cg2d.drawString(String.valueOf(ch), -(charWidth / 2), fm.getAscent() - (charHeight / 2));

    cg2d.dispose();
}

From source file:juicebox.track.EigenvectorTrack.java

private void drawRotatedString(Graphics2D g2, String string, float x, float y) {
    AffineTransform orig = g2.getTransform();
    g2.rotate(0);//from w  w  w .j  av a2  s. c om
    g2.setColor(Color.BLUE);
    g2.translate(x, 0);
    g2.scale(-1, 1);
    g2.translate(-x, 0);
    g2.drawString(string, x, y);
    g2.setTransform(orig);
}

From source file:krasa.cpu.CpuUsagePanel.java

/**
 * it will probably be better synchronized, not sure
 */// w  w  w. j av  a2 s.  c o m
private synchronized void draw(Graphics g, Image bufferedImage) {
    UIUtil.drawImage(g, bufferedImage, 0, 0, null);
    if (UIUtil.isJreHiDPI((Graphics2D) g) && !UIUtil.isUnderDarcula()) {
        Graphics2D g2 = (Graphics2D) g.create(0, 0, getWidth(), getHeight());
        float s = JBUI.sysScale(g2);
        g2.scale(1 / s, 1 / s);
        g2.setColor(UIUtil.isUnderIntelliJLaF() ? Gray.xC9 : Gray.x91);
        g2.drawLine(0, 0, (int) (s * getWidth()), 0);
        g2.scale(1, 1);
        g2.dispose();
    }
}

From source file:de.uni_siegen.wineme.come_in.thumbnailer.thumbnailers.PDFBoxThumbnailer.java

private BufferedImage convertToImage(final PDPage page, final int imageType, final int thumbWidth,
        final int thumbHeight)/*     */ throws IOException
/*     */ {//from  w  ww. j a va2s .  c  o m
    /* 707 */ final PDRectangle mBox = page.findMediaBox();
    /* 708 */ final float widthPt = mBox.getWidth();
    /* 709 */ final float heightPt = mBox.getHeight();
    /* 711 */ final int widthPx = thumbWidth;
    // Math.round(widthPt * scaling);
    /* 712 */ final int heightPx = thumbHeight;
    // Math.round(heightPt * scaling);
    /* 710 */ final double scaling = Math.min((double) thumbWidth / widthPt, (double) thumbHeight / heightPt);
    // resolution / 72.0F;
    /*     */
    /* 714 */ final Dimension pageDimension = new Dimension((int) widthPt, (int) heightPt);
    /*     */
    /* 716 */ final BufferedImage retval = new BufferedImage(widthPx, heightPx, imageType);
    /* 717 */ final Graphics2D graphics = (Graphics2D) retval.getGraphics();
    /* 718 */ graphics.setBackground(PDFBoxThumbnailer.TRANSPARENT_WHITE);
    /* 719 */ graphics.clearRect(0, 0, retval.getWidth(), retval.getHeight());
    /* 720 */ graphics.scale(scaling, scaling);
    /* 721 */ final PageDrawer drawer = new PageDrawer();
    /* 722 */ drawer.drawPage(graphics, page, pageDimension);
    /*     */ try
    /*     */ {
        /* 728 */ final int rotation = page.findRotation();
        /* 729 */ if (rotation == 90 || rotation == 270)
        /*     */ {
            /* 731 */ final int w = retval.getWidth();
            /* 732 */ final int h = retval.getHeight();
            /* 733 */ final BufferedImage rotatedImg = new BufferedImage(w, h, retval.getType());
            /* 734 */ final Graphics2D g = rotatedImg.createGraphics();
            /* 735 */ g.rotate(Math.toRadians(rotation), w / 2, h / 2);
            /* 736 */ g.drawImage(retval, null, 0, 0);
            /*     */ }
        /*     */ }
    /*     */ catch (final ImagingOpException e)
    /*     */ {
        /* 741 */ //log.warn("Unable to rotate page image", e);
        /*     */ }
    /*     */
    /* 744 */ return retval;
    /*     */ }

From source file:com.piketec.jenkins.plugins.tpt.publisher.PieChart.java

/**
 * Render the pie chart with the given height
 * /*from w ww . j  a v a  2 s . co  m*/
 * @param height
 *          The height of the resulting image
 * @return The pie chart rendered as an image
 */
public BufferedImage render(int height) {
    BufferedImage image = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();
    g2.scale(zoom, zoom);
    // fill background to white
    g2.setColor(Color.WHITE);
    g2.fill(new Rectangle(totalWidth, totalHeight));
    // prepare render hints
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
            RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2.setStroke(new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));

    // draw shadow image
    g2.drawImage(pieShadow.getImage(), 0, 0, pieShadow.getImageObserver());

    double start = 0;
    List<Arc2D> pies = new ArrayList<>();
    // pie segmente erzeugen und fuellen
    if (total == 0) {
        g2.setColor(BRIGHT_GRAY);
        g2.fillOval(centerX - radius, centerY - radius, 2 * radius, 2 * radius);
        g2.setColor(Color.WHITE);
        g2.drawOval(centerX - radius, centerY - radius, 2 * radius, 2 * radius);
    } else {
        for (Segment s : segments) {
            double portionDegrees = s.getPortion() / total;
            Arc2D pie = paintPieSegment(g2, start, portionDegrees, s.getColor());
            if (withSubSegments) {
                double smallRadius = radius * s.getSubSegmentRatio();
                paintPieSegment(g2, start, portionDegrees, smallRadius, s.getColor().darker());
            }
            start += portionDegrees;
            // portion degree jetzt noch als String (z.B. "17.3%" oder "20%" zusammenbauen)
            String p = String.format(Locale.ENGLISH, "%.1f", Math.rint(portionDegrees * 1000) / 10.0);
            p = removeSuffix(p, ".0"); // evtl. ".0" bei z.B. "25.0" abschneiden (-> "25")
            s.setPercent(p + "%");
            pies.add(pie);
        }
        // weissen Rahmen um die pie segmente zeichen
        g2.setColor(Color.WHITE);
        for (Arc2D pie : pies) {
            g2.draw(pie);
        }
    }
    // Legende zeichnen
    renderLegend(g2);
    // "xx%" Label direkt auf die pie segmente zeichen
    g2.setColor(Color.WHITE);
    float fontSize = 32f;
    g2.setFont(NORMALFONT.deriveFont(fontSize).deriveFont(Font.BOLD));
    start = 0;
    for (Segment s : segments) {
        if (s.getPortion() < 1E-6) {
            continue; // ignore segments with portions that are extremely small
        }
        double portionDegrees = s.getPortion() / total;
        double angle = start + portionDegrees / 2; // genau in der Mitte des Segments
        double xOffsetForCenteredTxt = 8 * s.getPercent().length(); // assume roughly 8px per char
        int x = (int) (centerX + 0.6 * radius * Math.sin(2 * Math.PI * angle) - xOffsetForCenteredTxt);
        int y = (int) (centerY - 0.6 * radius * Math.cos(2 * Math.PI * angle) + fontSize / 2);
        g2.drawString(s.getPercent(), x, y);
        start += portionDegrees;
    }
    return image;
}

From source file:ImagePrint.java

public int print(Graphics g, PageFormat pf, int pageIndex) {
    Graphics2D g2d = (Graphics2D) g;
    g.translate((int) (pf.getImageableX()), (int) (pf.getImageableY()));
    if (pageIndex == 0) {
        double pageWidth = pf.getImageableWidth();
        double pageHeight = pf.getImageableHeight();
        double imageWidth = printImage.getIconWidth();
        double imageHeight = printImage.getIconHeight();
        double scaleX = pageWidth / imageWidth;
        double scaleY = pageHeight / imageHeight;
        double scaleFactor = Math.min(scaleX, scaleY);
        g2d.scale(scaleFactor, scaleFactor);
        g.drawImage(printImage.getImage(), 0, 0, null);
        return Printable.PAGE_EXISTS;
    }//  ww  w  .  j ava 2  s. c o m
    return Printable.NO_SUCH_PAGE;
}

From source file:BookTest.java

public void drawPage(Graphics2D g2, PageFormat pf, int page) {
    if (message.equals(""))
        return;/*from www  . j  a  v  a  2 s.  c o m*/
    page--; // account for cover page

    drawCropMarks(g2, pf);
    g2.clip(new Rectangle2D.Double(0, 0, pf.getImageableWidth(), pf.getImageableHeight()));
    g2.translate(-page * pf.getImageableWidth(), 0);
    g2.scale(scale, scale);
    FontRenderContext context = g2.getFontRenderContext();
    Font f = new Font("Serif", Font.PLAIN, 72);
    TextLayout layout = new TextLayout(message, f, context);
    AffineTransform transform = AffineTransform.getTranslateInstance(0, layout.getAscent());
    Shape outline = layout.getOutline(transform);
    g2.draw(outline);
}

From source file:edworld.pdfreader4humans.PDFReader.java

public BufferedImage createPageImage(int pageNumber, int scaling, Color inkColor, Color backgroundColor,
        boolean showStructure) throws IOException {
    Map<String, Font> fonts = new HashMap<String, Font>();
    PDRectangle cropBox = getPageCropBox(pageNumber);
    BufferedImage image = new BufferedImage(Math.round(cropBox.getWidth() * scaling),
            Math.round(cropBox.getHeight() * scaling), BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = image.createGraphics();
    graphics.setBackground(backgroundColor);
    graphics.clearRect(0, 0, image.getWidth(), image.getHeight());
    graphics.setColor(backgroundColor);/*from   w  ww  .  j ava 2s.  co m*/
    graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
    graphics.setColor(inkColor);
    graphics.scale(scaling, scaling);
    for (Component component : getFirstLevelComponents(pageNumber))
        draw(component, graphics, inkColor, backgroundColor, showStructure, fonts);
    graphics.dispose();
    return image;
}

From source file:ddf.catalog.transformer.input.pptx.PptxInputTransformer.java

/**
 * If the slide show doesn't contain any slides, then return null. Otherwise, return jpeg
 * image data of the first slide in the deck.
 *
 * @param slideShow/*from w  w w.  j av  a2s . c o m*/
 * @return jpeg thumbnail or null if thumbnail can't be created
 * @throws IOException
 */
private byte[] generatePptxThumbnail(XMLSlideShow slideShow) throws IOException {

    if (slideShow.getSlides().isEmpty()) {
        LOGGER.info("the powerpoint file does not contain any slides, skipping thumbnail generation");
        return null;
    }

    Dimension pgsize = slideShow.getPageSize();

    int largestDimension = (int) Math.max(pgsize.getHeight(), pgsize.getWidth());
    float scalingFactor = IMAGE_HEIGHTWIDTH / largestDimension;
    int scaledHeight = (int) (pgsize.getHeight() * scalingFactor);
    int scaledWidth = (int) (pgsize.getWidth() * scalingFactor);
    BufferedImage img = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_RGB);

    Graphics2D graphics = img.createGraphics();

    try {
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
                RenderingHints.VALUE_FRACTIONALMETRICS_ON);

        graphics.scale(scalingFactor, scalingFactor);

        slideShow.getSlides().get(0).draw(graphics);

        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            ImageIOUtil.writeImage(img, FORMAT_NAME, outputStream, RESOLUTION_DPI, IMAGE_QUALITY);
            return outputStream.toByteArray();
        }
    } catch (RuntimeException e) {
        if (e.getCause() instanceof javax.imageio.IIOException) {
            LOGGER.warn("unable to generate thumbnail for PPTX file", e);
        } else {
            throw e;
        }
    } finally {
        graphics.dispose();
    }

    return null;
}