Example usage for java.awt Graphics2D getBackground

List of usage examples for java.awt Graphics2D getBackground

Introduction

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

Prototype

public abstract Color getBackground();

Source Link

Document

Returns the background color used for clearing a region.

Usage

From source file:edu.csudh.goTorosBank.WithdrawServlet.java

public String writeIntoCheck(String filePath, String username, String theAmount, String amountInWords,
        String dateWrote, String person_payingto, String billType) throws IOException, NullPointerException {

    File blueCheck = new File("blank-blue-check.jpg");
    String pathToOriginal = getServletContext().getRealPath("/" + blueCheck);

    File file = new File(pathToOriginal);
    BufferedImage imageToCopy = null;
    try {//from w  w w  .j  a v a2  s .  co m
        imageToCopy = ImageIO.read(file);
    } catch (IOException e) {
        e.printStackTrace();
    }

    String amount = theAmount;
    String person_gettingPayed = person_payingto;
    String amountinWords = amountInWords;
    String date = dateWrote;
    String bill_type = billType;

    int w = imageToCopy.getWidth();
    int h = imageToCopy.getHeight();
    BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = img.createGraphics();
    g2d.setColor(g2d.getBackground());
    g2d.fillRect(0, 0, w, h);
    g2d.drawImage(imageToCopy, 0, -100, null);
    g2d.setPaint(Color.black);
    g2d.setFont(new java.awt.Font("Serif", Font.BOLD, 36));
    //g2d.setFont(new Font("Serif", Font.BOLD, 36));

    FontMetrics fm = g2d.getFontMetrics();
    int x = img.getWidth() - fm.stringWidth(amount) - 100;
    int y = fm.getHeight();
    g2d.drawString(amount, x - 70, y + 335);
    g2d.drawString(person_gettingPayed, x - 800, y + 329);
    g2d.drawString(amountinWords, x - 940, y + 390);
    g2d.drawString(date, x - 340, y + 245);
    g2d.drawString(bill_type, x - 900, y + 530);

    String signature = "Use The Force";
    g2d.setFont(new java.awt.Font("Monotype Corsiva", Font.BOLD | Font.ITALIC, 36));
    g2d.drawString(signature, x - 340, y + 530);
    g2d.dispose();
    /*write check to file*/
    String filename = fileNameGenerator(username);
    String fullname = filePath + "_" + filename + ".jpg";
    ImageIO.write(img, "jpg", new File(fullname));
    return fullname;
}

From source file:org.jahia.services.image.AbstractJava2DImageService.java

public boolean rotateImage(Image image, File outputFile, boolean clockwise) throws IOException {
    BufferedImage originalImage = ((BufferImage) image).getOriginalImage();

    BufferedImage dest = getDestImage(originalImage.getHeight(), originalImage.getWidth(), originalImage);
    // Paint source image into the destination, scaling as needed
    Graphics2D graphics2D = getGraphics2D(dest, OperationType.ROTATE);

    double angle = Math.toRadians(clockwise ? 90 : -90);
    double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
    int w = originalImage.getWidth(), h = originalImage.getHeight();
    int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math.floor(h * cos + w * sin);
    graphics2D.translate((neww - w) / 2, (newh - h) / 2);
    graphics2D.rotate(angle, w / (double) 2, h / (double) 2);
    graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
    if (originalImage.getColorModel() instanceof IndexColorModel) {
        graphics2D.drawImage(originalImage, 0, 0, graphics2D.getBackground(), null);
    } else {//w  ww.  j a va  2s  .c o  m
        graphics2D.drawImage(originalImage, 0, 0, null);
    }

    // Save destination image
    saveImageToFile(dest, ((BufferImage) image).getMimeType(), outputFile);
    return true;
}

From source file:au.com.gaiaresources.bdrs.controller.test.TestDataCreator.java

private byte[] getAndScaleImageData(int targetWidth, int targetHeight, File file) throws IOException {
    if (file == null) {
        return null;
    }//from  w  w  w  . ja v a  2  s  .co  m

    BufferedImage sourceImage = ImageIO.read(file);
    BufferedImage targetImage;
    if (targetWidth > -1 && targetHeight > -1) {
        // Resize the image as required to fit the space
        targetImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2_scaled = targetImage.createGraphics();
        // Better scaling
        g2_scaled.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BICUBIC);

        g2_scaled.setBackground(Color.WHITE);
        g2_scaled.clearRect(0, 0, targetWidth, targetHeight);

        int sourceWidth = sourceImage.getWidth();
        int sourceHeight = sourceImage.getHeight();

        double widthRatio = (double) targetWidth / (double) sourceWidth;
        double heightRatio = (double) targetHeight / (double) targetHeight;

        if (heightRatio > widthRatio) {
            int scaledHeight = (int) Math.round(widthRatio * sourceHeight);
            g2_scaled.drawImage(sourceImage, 0, (targetImage.getHeight() - scaledHeight) / 2,
                    targetImage.getWidth(), scaledHeight, g2_scaled.getBackground(), null);
        } else {
            int scaledWidth = (int) Math.round(heightRatio * sourceWidth);
            g2_scaled.drawImage(sourceImage, (targetImage.getWidth() - scaledWidth) / 2, 0, scaledWidth,
                    targetImage.getHeight(), new Color(0, 0, 0, 255), null);
        }
    } else {
        targetImage = sourceImage;
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream(targetWidth * targetHeight);
    ImageIO.write(targetImage, "png", baos);
    baos.flush();
    byte[] data = baos.toByteArray();
    baos.close();

    return data;
}

From source file:com.projity.pm.graphic.gantt.GanttRenderer.java

public void paintNonWorkingDays(Graphics2D g2, Rectangle bounds) {
    BarFormat calFormat = getCalendarFormat();
    if (calFormat == null)
        return;//from   w  ww  . ja v a  2 s.c  o m
    //non working days
    Color oldColor = g2.getColor();
    Paint oldPaint = g2.getPaint();
    CoordinatesConverter coord = ((GanttParams) graphInfo).getCoord();
    Project project = coord.getProject();
    WorkingCalendar wc = (WorkingCalendar) project.getWorkCalendar();

    if (coord.getTimescaleManager().isShowWholeDays()) {
        boolean useScale2 = coord.getTimescaleManager().getCurrentScaleIndex() == 0; //valid only for current time scales
        TimeIterator i = coord.getTimeIterator(bounds.getX(), bounds.getMaxX(), useScale2);
        long startNonworking = -1L, endNonWorking = -1L;
        Calendar cal = DateTime.calendarInstance();

        PredefinedPaint paint = (PredefinedPaint) calFormat.getMiddle().getPaint();//new PredefinedPaint(PredefinedPaint.DOT_LINE,Colors.VERY_LIGHT_GRAY,Color.WHITE);
        paint.applyPaint(g2, useTextures());
        while (i.hasNext()) {
            TimeInterval interval = i.next();
            long s = interval.getStart();
            if (CalendarService.getInstance().getDay(wc, s).isWorking()) {
                if (startNonworking != -1L) {
                    drawNonWorking(g2, startNonworking, endNonWorking, cal, coord, bounds, useScale2);
                    startNonworking = endNonWorking = -1L;
                }
            } else {
                if (startNonworking == -1L)
                    startNonworking = s;
                endNonWorking = s;

            }
        }
        if (startNonworking != -1L) {
            drawNonWorking(g2, startNonworking, endNonWorking, cal, coord, bounds, useScale2);
            startNonworking = endNonWorking = -1L;
        }
    }

    if (container != null) {
        //scale2 separation lines
        TimeIterator i = coord.getTimeIterator(bounds.getX(), bounds.getMaxX(), true);
        g2.setPaint(new PredefinedPaint(PredefinedPaint.DOT_LINE2, Color.GRAY, g2.getBackground()));
        while (i.hasNext()) {
            TimeInterval interval = i.next();
            int startX = (int) Math.round(coord.toX(interval.getStart()));
            g2.drawLine(startX, bounds.y, startX, bounds.y + bounds.height);
        }

        //project start
        int projectStartX = (int) Math.round(coord.toX(project.getStart()));
        if (projectStartX >= bounds.getX() && projectStartX <= bounds.getMaxX()) {
            g2.setPaint(new PredefinedPaint(PredefinedPaint.DASH_LINE, Color.GRAY, g2.getBackground()));
            g2.drawLine(projectStartX, bounds.y, projectStartX, bounds.y + bounds.height);
        }

        //project start
        long statusDate = project.getStatusDate();
        if (statusDate != 0) {
            int statusDateX = (int) Math.round(coord.toX(statusDate));
            if (statusDateX >= bounds.getX() && statusDateX <= bounds.getMaxX()) {
                g2.setPaint(new PredefinedPaint(PredefinedPaint.DOT_LINE2, Color.GREEN, g2.getBackground()));
                g2.drawLine(statusDateX, bounds.y, statusDateX, bounds.y + bounds.height);
            }
        }

        if (oldColor != null)
            g2.setColor(oldColor);
        if (oldPaint != null)
            g2.setPaint(oldPaint);

    }
}

From source file:org.photovault.swingui.PhotoCollectionThumbView.java

private void paintThumbnail(Graphics2D g2, PhotoInfo photo, int startx, int starty, boolean isSelected) {
    log.debug("paintThumbnail entry " + photo.getUuid());
    long startTime = System.currentTimeMillis();
    long thumbReadyTime = 0;
    long thumbDrawnTime = 0;
    long endTime = 0;
    // Current position in which attributes can be drawn
    int ypos = starty + rowHeight / 2;
    boolean useOldThumbnail = false;

    Thumbnail thumbnail = null;/*from  ww w.  j  a v a2  s.  c  om*/
    log.debug("finding thumb");
    boolean hasThumbnail = photo.hasThumbnail();
    log.debug("asked if has thumb");
    if (hasThumbnail) {
        log.debug("Photo " + photo.getUuid() + " has thumbnail");
        thumbnail = photo.getThumbnail();
        log.debug("got thumbnail");
    } else {
        /*
         Check if the thumbnail has been just invalidated. If so, use the 
         old one until we get the new thumbnail created.
         */
        thumbnail = photo.getOldThumbnail();
        if (thumbnail != null) {
            useOldThumbnail = true;
        } else {
            // No success, use default thumnail.
            thumbnail = Thumbnail.getDefaultThumbnail();
        }

        // Inform background task scheduler that we have some work to do
        ctrl.getBackgroundTaskScheduler().registerTaskProducer(this, TaskPriority.CREATE_VISIBLE_THUMBNAIL);
    }
    thumbReadyTime = System.currentTimeMillis();

    log.debug("starting to draw");
    // Find the position for the thumbnail
    BufferedImage img = thumbnail.getImage();
    if (img == null) {
        thumbnail = Thumbnail.getDefaultThumbnail();
        img = thumbnail.getImage();
    }

    float scaleX = ((float) thumbWidth) / ((float) img.getWidth());
    float scaleY = ((float) thumbHeight) / ((float) img.getHeight());
    float scale = Math.min(scaleX, scaleY);
    int w = (int) (img.getWidth() * scale);
    int h = (int) (img.getHeight() * scale);

    int x = startx + (columnWidth - w) / (int) 2;
    int y = starty + (rowHeight - h) / (int) 2;

    log.debug("drawing thumbnail");

    // Draw shadow
    int offset = isSelected ? 2 : 0;
    int shadowX[] = { x + 3 - offset, x + w + 1 + offset, x + w + 1 + offset };
    int shadowY[] = { y + h + 1 + offset, y + h + 1 + offset, y + 3 - offset };
    GeneralPath polyline = new GeneralPath(GeneralPath.WIND_EVEN_ODD, shadowX.length);
    polyline.moveTo(shadowX[0], shadowY[0]);
    for (int index = 1; index < shadowX.length; index++) {
        polyline.lineTo(shadowX[index], shadowY[index]);
    }
    ;
    BasicStroke shadowStroke = new BasicStroke(4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
    Stroke oldStroke = g2.getStroke();
    g2.setStroke(shadowStroke);
    g2.setColor(Color.DARK_GRAY);
    g2.draw(polyline);
    g2.setStroke(oldStroke);

    // Paint thumbnail
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2.drawImage(img, new AffineTransform(scale, 0f, 0f, scale, x, y), null);
    if (useOldThumbnail) {
        creatingThumbIcon.paintIcon(this, g2,
                startx + (columnWidth - creatingThumbIcon.getIconWidth()) / (int) 2,
                starty + (rowHeight - creatingThumbIcon.getIconHeight()) / (int) 2);
    }
    log.debug("Drawn, drawing decorations");
    if (isSelected) {
        Stroke prevStroke = g2.getStroke();
        Color prevColor = g2.getColor();
        g2.setStroke(new BasicStroke(3.0f));
        g2.setColor(Color.BLUE);
        g2.drawRect(x, y, w, h);
        g2.setColor(prevColor);
        g2.setStroke(prevStroke);
    }

    thumbDrawnTime = System.currentTimeMillis();

    boolean drawAttrs = (thumbWidth >= 100);
    if (drawAttrs) {
        // Increase ypos so that attributes are drawn under the image
        ypos += ((int) h) / 2 + 3;

        // Draw the attributes

        // Draw the qualoity icon to the upper left corner of the thumbnail
        int quality = photo.getQuality();
        if (showQuality && quality != 0) {
            int qx = startx + (columnWidth - quality * starIcon.getIconWidth()) / (int) 2;
            for (int n = 0; n < quality; n++) {
                starIcon.paintIcon(this, g2, qx, ypos);
                qx += starIcon.getIconWidth();
            }
            ypos += starIcon.getIconHeight();
        }
        ypos += 6;

        if (photo.getRawSettings() != null) {
            // Draw the "RAW" icon
            int rx = startx + (columnWidth + w - rawIcon.getIconWidth()) / (int) 2 - 5;
            int ry = starty + (columnWidth - h - rawIcon.getIconHeight()) / (int) 2 + 5;
            rawIcon.paintIcon(this, g2, rx, ry);
        }
        if (photo.getHistory().getHeads().size() > 1) {
            // Draw the "unresolved conflicts" icon
            int rx = startx + (columnWidth + w - 10) / (int) 2 - 20;
            int ry = starty + (columnWidth - h - 10) / (int) 2;
            g2.setColor(Color.RED);
            g2.fillRect(rx, ry, 10, 10);
        }

        Color prevBkg = g2.getBackground();
        if (isSelected) {
            g2.setBackground(Color.BLUE);
        } else {
            g2.setBackground(this.getBackground());
        }
        Font attrFont = new Font("Arial", Font.PLAIN, 10);
        FontRenderContext frc = g2.getFontRenderContext();
        if (showDate && photo.getShootTime() != null) {
            FuzzyDate fd = new FuzzyDate(photo.getShootTime(), photo.getTimeAccuracy());

            String dateStr = fd.format();
            TextLayout txt = new TextLayout(dateStr, attrFont, frc);
            // Calculate the position for the text
            Rectangle2D bounds = txt.getBounds();
            int xpos = startx + ((int) (columnWidth - bounds.getWidth())) / 2 - (int) bounds.getMinX();
            g2.clearRect(xpos - 2, ypos - 2, (int) bounds.getWidth() + 4, (int) bounds.getHeight() + 4);
            txt.draw(g2, xpos, (int) (ypos + bounds.getHeight()));
            ypos += bounds.getHeight() + 4;
        }
        String shootPlace = photo.getShootingPlace();
        if (showPlace && shootPlace != null && shootPlace.length() > 0) {
            TextLayout txt = new TextLayout(photo.getShootingPlace(), attrFont, frc);
            // Calculate the position for the text
            Rectangle2D bounds = txt.getBounds();
            int xpos = startx + ((int) (columnWidth - bounds.getWidth())) / 2 - (int) bounds.getMinX();

            g2.clearRect(xpos - 2, ypos - 2, (int) bounds.getWidth() + 4, (int) bounds.getHeight() + 4);
            txt.draw(g2, xpos, (int) (ypos + bounds.getHeight()));
            ypos += bounds.getHeight() + 4;
        }
        g2.setBackground(prevBkg);
    }
    endTime = System.currentTimeMillis();
    log.debug("paintThumbnail: exit " + photo.getUuid());
    log.debug("Thumb fetch " + (thumbReadyTime - startTime) + " ms");
    log.debug("Thumb draw " + (thumbDrawnTime - thumbReadyTime) + " ms");
    log.debug("Deacoration draw " + (endTime - thumbDrawnTime) + " ms");
    log.debug("Total " + (endTime - startTime) + " ms");
}