Example usage for java.awt Graphics2D fillRect

List of usage examples for java.awt Graphics2D fillRect

Introduction

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

Prototype

public abstract void fillRect(int x, int y, int width, int height);

Source Link

Document

Fills the specified rectangle.

Usage

From source file:IconDemoApp.java

public void paintIcon(Component c, Graphics g, int x, int y) {
    Graphics2D g2d = (Graphics2D) g.create();

    g2d.setColor(Color.WHITE);//from w w w .j  a va 2 s.c  o  m
    g2d.fillRect(x + 1, y + 1, width - 2, height - 2);

    g2d.setColor(Color.BLACK);
    g2d.drawRect(x + 1, y + 1, width - 2, height - 2);

    g2d.setColor(Color.RED);

    g2d.setStroke(stroke);
    g2d.drawLine(x + 10, y + 10, x + width - 10, y + height - 10);
    g2d.drawLine(x + 10, y + height - 10, x + width - 10, y + 10);

    g2d.dispose();
}

From source file:main.MapKit.java

private BufferedImage convert(BufferedImage loadImg, Color newColor) {
    int w = loadImg.getWidth();
    int h = loadImg.getHeight();
    BufferedImage imgOut = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    BufferedImage imgColor = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = imgColor.createGraphics();
    g.setColor(newColor);//from   w  w w.  j  a  va2  s  . c o m
    g.fillRect(0, 0, w + 1, h + 1);
    g.dispose();

    Graphics2D graphics = imgOut.createGraphics();
    graphics.drawImage(loadImg, 0, 0, null);
    graphics.setComposite(MultiplyComposite.Default);
    graphics.drawImage(imgColor, 0, 0, null);
    graphics.dispose();

    return imgOut;
}

From source file:uk.co.modularaudio.service.bufferedimageallocation.impl.debugwindow.imagebrowser.RawImageCanvas.java

@Override
public void paint(final Graphics rawG) {
    final Graphics2D g = (Graphics2D) rawG;
    g.getClipBounds(clipBounds);/* ww w  .j av a2s  .c om*/

    if (curBufferedImage != null) {
        g.setComposite(opaqueComposite);
        g.setColor(Color.DARK_GRAY);
        g.fillRect(0, 0, width, height);
        g.drawImage(curBufferedImage, 0, 0, null);

        // Draw the free blocks over the top
        // We draw 50 transparent fill then outline with full yellow
        g.setColor(DARK_BLUE);
        g.setComposite(ninetyPercentAlphaComposite);
        //         g.setComposite( fiftyPercentAlphaComposite );
        //         g.setComposite( tenPercentAlphaComposite );
        for (final Rectangle freeBlockRectangle : freeBlockRectangles) {
            g.fillRect(freeBlockRectangle.x, freeBlockRectangle.y, freeBlockRectangle.width,
                    freeBlockRectangle.height);
        }

        g.setColor(Color.yellow);
        g.setComposite(opaqueComposite);
        for (final Rectangle freeBlockRectangle : freeBlockRectangles) {
            g.drawRect(freeBlockRectangle.x, freeBlockRectangle.y, freeBlockRectangle.width,
                    freeBlockRectangle.height);
        }

        g.setComposite(opaqueComposite);
        g.setColor(Color.RED);
        for (final Rectangle usedBlockRectangle : usedBlockRectangles) {
            g.drawRect(usedBlockRectangle.x, usedBlockRectangle.y, usedBlockRectangle.width,
                    usedBlockRectangle.height);
        }
    } else {
        g.setColor(Color.gray);
        g.fillRect(0, 0, width, height);
    }
}

From source file:org.gitools.ui.app.heatmap.drawer.AbstractHeatmapDrawer.java

/**
 * Paint a full horizontal or vertical line
 */// w  w w.j  a v  a  2 s .co  m
protected void fillLine(Graphics2D g, Rectangle box, int position, int size, boolean drawingRows) {
    if (drawingRows) {
        g.fillRect(box.x, box.y + position, box.width, size);
    } else {
        g.fillRect(box.x + position, box.y, size, box.height);
    }
}

From source file:edu.ku.brc.af.prefs.PrefsToolbar.java

@Override
protected void paintComponent(final Graphics g) {
    super.paintComponent(g);

    Color base = getBackground();
    Dimension size = getSize();/*from   ww  w. ja  v a2 s  .com*/

    Color grad_top = base;
    Color grad_bot = UIHelper.makeDarker(base, UIHelper.isMacOS() ? 0.15 : 0.1);
    GradientPaint bg = new GradientPaint(new Point(0, 0), grad_top, new Point(0, size.height), grad_bot);
    Graphics2D g2 = (Graphics2D) g;
    g2.setPaint(bg);
    g2.fillRect(0, 0, size.width, size.height);

    g.setColor(UIHelper.makeDarker(base, 0.5));
    g.drawLine(0, size.height - 1, size.width, size.height - 1);
}

From source file:org.n52.oxf.render.sos.ProportionalCircleMapRenderer.java

private Image renderLegend(ObservationSeriesCollection obsValues, String observedProperty) {

    double lowestValue = (Double) obsValues.getMinimum(0);
    double highestValue = (Double) obsValues.getMaximum(0);

    double classDistance = (highestValue - lowestValue) / (NUMBER_OF_CLASSES - 2);
    int dotSizeDistance = (MAX_DOT_SIZE - MIN_DOT_SIZE) / (NUMBER_OF_CLASSES - 2);

    BufferedImage image = new BufferedImage(LEGEND_WIDTH, LEGEND_HEIGHT, BufferedImage.TYPE_INT_RGB);

    Graphics2D g = image.createGraphics();

    // draw white background:
    g.setColor(Color.WHITE);//from  w  w w  .  j  a va  2 s  .  com
    g.fillRect(0, 0, LEGEND_WIDTH, LEGEND_HEIGHT);

    // draw information:
    observedProperty = observedProperty.split(":")[observedProperty.split(":").length - 1];
    g.setColor(Color.BLACK);
    g.drawString("Observed Property:   '" + observedProperty + "'", 25, 25);

    for (int i = 1; i <= NUMBER_OF_CLASSES; i++) {
        // draw text:
        int x_stringLocation = 100;
        int y_location = i * 60;
        g.setColor(Color.BLACK);

        DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(Locale.GERMAN);
        df.applyPattern("#,###,##0.00");

        double lowerBorder = lowestValue + classDistance * (i - 1);
        double upperBorder = lowestValue + classDistance * i;

        g.drawString(i + ". class: " + df.format(lowerBorder) + " - " + df.format(upperBorder),
                x_stringLocation, y_location + 10);

        // draw symbol:
        int x_symbolLocation = 30;
        g.setColor(POINT_INNER_COLOR);
        g.fillOval(x_symbolLocation, y_location, i * dotSizeDistance, i * dotSizeDistance);
    }

    return image;
}

From source file:TexturedPanel.java

/**
 * Creates a new TexturePaint using the provided colors.
 *///w w w .  j av a 2s . c o  m
private void setupDefaultPainter(Color foreground, Color background) {
    if (foreground == null || background == null) {
        ourPainter = null;
        return;
    }

    BufferedImage buff = new BufferedImage(6, 6, BufferedImage.TYPE_INT_ARGB_PRE);

    Graphics2D g2 = buff.createGraphics();

    g2.setColor(background);
    g2.fillRect(0, 0, 6, 6);

    g2.setColor(foreground);
    g2.drawLine(0, 2, 6, 2);
    g2.drawLine(0, 5, 6, 5);

    ourPainter = new TexturePaint(buff, new Rectangle(0, 0, 6, 6));

    g2.dispose();
}

From source file:GradientPanel.java

public void paintComponent(Graphics g) {
    if (isOpaque()) {
        super.paintComponent(g);
        return;/*  w w  w  .  j a v  a2 s  .  com*/
    }

    int width = getWidth();
    int height = getHeight();

    // Create the gradient paint
    GradientPaint paint = null;

    Color sc = getForeground();
    Color ec = getBackground();

    switch (direction) {
    case HORIZONTAL: {
        paint = new GradientPaint(0, height / 2, sc, width, height / 2, ec, cyclic);
        break;
    }
    case VERTICAL: {
        paint = new GradientPaint(width / 2, 0, sc, width / 2, maxLength > 0 ? maxLength : height, ec, cyclic);
        break;
    }
    case DIAGONAL_LEFT: {
        paint = new GradientPaint(0, 0, sc, width, height, ec, cyclic);
        break;
    }
    case DIAGONAL_RIGHT: {
        paint = new GradientPaint(width, 0, sc, 0, height, ec, cyclic);
        break;
    }
    }

    if (paint == null) {
        throw new RuntimeException("Invalid direction specified in GradientPanel");
    }

    // we need to cast to Graphics2D for this operation
    Graphics2D g2d = (Graphics2D) g;

    // save the old paint
    Paint oldPaint = g2d.getPaint();

    // set the paint to use for this operation
    g2d.setPaint(paint);

    // fill the background using the paint
    g2d.fillRect(0, 0, width, height);

    // restore the original paint
    g2d.setPaint(oldPaint);

    super.paintComponent(g);
}

From source file:uk.co.modularaudio.service.gui.impl.racktable.back.AbstractLinkImage.java

private void drawLinkWireIntoImage(final Point sourcePoint, final Point sinkPoint) {
    //      log.debug("Drawing link from " + sourcePoint + " to " + sinkPoint);

    final int fromX = sourcePoint.x;
    final int fromY = sourcePoint.y;
    final int toX = sinkPoint.x;
    final int toY = sinkPoint.y;

    float f1, f2, f3, f4, f5, f6, f7, f8 = 0.0f;
    f1 = fromX;//ww w.j a v  a 2 s.co  m
    f2 = fromY;
    f3 = fromX;
    f4 = fromY + WIRE_DIP_PIXELS;
    f5 = toX;
    f6 = toY + WIRE_DIP_PIXELS;
    f7 = toX;
    f8 = toY;
    final CubicCurve2D cubicCurve = new CubicCurve2D.Float(f1, f2, f3, f4, f5, f6, f7, f8);
    final Rectangle cubicCurveBounds = cubicCurve.getBounds();

    final int imageWidthToUse = cubicCurveBounds.width + LINK_IMAGE_PADDING_FOR_WIRE_RADIUS;
    //      int imageHeightToUse = cubicCurveBounds.height + WIRE_DIP_PIXELS;
    int imageHeightToUse = cubicCurveBounds.height;
    // If the wire is close to vertical (little Y difference) we make the image a little bigger to account for the wire "dip"
    if (Math.abs(sinkPoint.y - sourcePoint.y) <= WIRE_DIP_PIXELS) {
        imageHeightToUse += (WIRE_DIP_PIXELS / 2);
    }

    //      bufferedImage = new BufferedImage( imageWidthToUse, imageHeightToUse, BufferedImage.TYPE_INT_ARGB );
    try {
        tiledBufferedImage = bufferImageAllocationService.allocateBufferedImage(allocationSource,
                allocationMatchToUse, AllocationLifetime.SHORT, AllocationBufferType.TYPE_INT_ARGB,
                imageWidthToUse, imageHeightToUse);

        bufferedImage = tiledBufferedImage.getUnderlyingBufferedImage();
        final Graphics2D g2d = bufferedImage.createGraphics();

        g2d.setComposite(AlphaComposite.Clear);
        g2d.fillRect(0, 0, imageWidthToUse, imageHeightToUse);

        g2d.setComposite(AlphaComposite.SrcOver);

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

        f1 += LINK_IMAGE_DIST_TO_CENTER - cubicCurveBounds.x;
        f2 += LINK_IMAGE_DIST_TO_CENTER - cubicCurveBounds.y;
        f3 += LINK_IMAGE_DIST_TO_CENTER - cubicCurveBounds.x;
        f4 += LINK_IMAGE_DIST_TO_CENTER - cubicCurveBounds.y;
        f5 += LINK_IMAGE_DIST_TO_CENTER - cubicCurveBounds.x;
        f6 += LINK_IMAGE_DIST_TO_CENTER - cubicCurveBounds.y;
        f7 += LINK_IMAGE_DIST_TO_CENTER - cubicCurveBounds.x;
        f8 += LINK_IMAGE_DIST_TO_CENTER - cubicCurveBounds.y;

        final CubicCurve2D offSetCubicCurve = new CubicCurve2D.Float(f1, f2, f3, f4, f5, f6, f7, f8);

        // Draw the highlight and shadow
        if (DRAW_HIGHTLIGHT_AND_SHADOW) {
            final Graphics2D sG2d = (Graphics2D) g2d.create();
            sG2d.translate(WIRE_SHADOW_X_OFFSET, WIRE_SHADOW_Y_OFFSET);
            sG2d.setColor(Color.BLUE.darker());
            sG2d.setStroke(new BasicStroke(WIRE_SHADOW_WIDTH, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
            sG2d.draw(offSetCubicCurve);

            final Graphics2D hG2d = (Graphics2D) g2d.create();
            hG2d.translate(WIRE_HIGHLIGHT_X_OFFSET, WIRE_HIGHLIGHT_Y_OFFSET);
            hG2d.setColor(Color.WHITE);
            hG2d.setStroke(
                    new BasicStroke(WIRE_HIGHLIGHT_WIDTH, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
            hG2d.draw(offSetCubicCurve);
        }

        g2d.setColor(Color.BLACK);
        g2d.setStroke(wireStroke);
        g2d.draw(offSetCubicCurve);

        g2d.setColor(Color.BLUE);
        g2d.setStroke(wireBodyStroke);
        g2d.draw(offSetCubicCurve);

        // For debugging, draw a green line around the outside of this image.
        if (DRAW_WIRE_BOUNDING_BOX) {
            g2d.setStroke(basicStrokeOfOne);
            g2d.setColor(Color.GREEN);
            g2d.drawRect(0, 0, imageWidthToUse - 1, imageHeightToUse - 1);
        }

        rectangle.x = cubicCurveBounds.x - LINK_IMAGE_DIST_TO_CENTER;
        rectangle.y = cubicCurveBounds.y - LINK_IMAGE_DIST_TO_CENTER;
        rectangle.width = imageWidthToUse;
        rectangle.height = imageHeightToUse;
    } catch (final Exception e) {
        final String msg = "Exception caught allocating buffered image: " + e.toString();
        log.error(msg, e);
    }
}

From source file:com.tremolosecurity.scale.totp.TotpController.java

@PostConstruct
public void init() {
    this.error = null;
    HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
            .getRequest();/*from ww w .  j av  a2s  .c o m*/

    this.scaleTotpConfig = (ScaleTOTPConfigType) commonConfig.getScaleConfig();

    this.login = request.getRemoteUser();

    UnisonUserData userData;
    try {
        userData = this.scaleSession.loadUserFromUnison(this.login,
                new AttributeData(scaleTotpConfig.getServiceConfiguration().getLookupAttributeName(),
                        scaleTotpConfig.getUiConfig().getDisplayNameAttribute(),
                        scaleTotpConfig.getAttributeName()));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return;
    }

    this.user = userData.getUserObj();

    this.displayName = userData.getUserObj().getDisplayName();

    ScaleAttribute scaleAttr = userData.getUserObj().getAttrs().get(scaleTotpConfig.getAttributeName());
    if (scaleAttr == null) {
        if (logger.isDebugEnabled())
            logger.debug("no sattribute");
        this.error = "Token not found";
        return;
    }

    this.encryptedToken = scaleAttr.getValue();

    try {
        byte[] decryptionKeyBytes = Base64.decodeBase64(scaleTotpConfig.getDecryptionKey().getBytes("UTF-8"));
        SecretKey decryptionKey = new SecretKeySpec(decryptionKeyBytes, 0, decryptionKeyBytes.length, "AES");

        Gson gson = new Gson();
        Token token = gson.fromJson(new String(Base64.decodeBase64(this.encryptedToken.getBytes("UTF-8"))),
                Token.class);
        byte[] iv = org.bouncycastle.util.encoders.Base64.decode(token.getIv());
        IvParameterSpec spec = new IvParameterSpec(iv);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, decryptionKey, spec);

        String decryptedJSON = new String(
                cipher.doFinal(Base64.decodeBase64(token.getEncryptedRequest().getBytes("UTF-8"))));

        if (logger.isDebugEnabled())
            logger.debug(decryptedJSON);

        TOTPKey totp = gson.fromJson(decryptedJSON, TOTPKey.class);

        this.otpURL = "otpauth://totp/" + totp.getUserName() + "@" + totp.getHost() + "?secret="
                + totp.getSecretKey();

    } catch (Exception e) {
        e.printStackTrace();
        this.error = "Could not decrypt token";
    }

    try {
        int size = 250;
        Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix byteMatrix = qrCodeWriter.encode(this.otpURL, BarcodeFormat.QR_CODE, size, size, hintMap);
        int CrunchifyWidth = byteMatrix.getWidth();
        BufferedImage image = new BufferedImage(CrunchifyWidth, CrunchifyWidth, BufferedImage.TYPE_INT_RGB);
        image.createGraphics();

        Graphics2D graphics = (Graphics2D) image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, CrunchifyWidth, CrunchifyWidth);
        graphics.setColor(Color.BLACK);

        for (int i = 0; i < CrunchifyWidth; i++) {
            for (int j = 0; j < CrunchifyWidth; j++) {
                if (byteMatrix.get(i, j)) {
                    graphics.fillRect(i, j, 1, 1);
                }
            }
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        ImageIO.write(image, "png", baos);

        this.encodedQRCode = new String(Base64.encodeBase64(baos.toByteArray()));
    } catch (Exception e) {
        e.printStackTrace();
        this.error = "Could not encode QR Code";
    }

}