Example usage for java.awt Graphics2D setColor

List of usage examples for java.awt Graphics2D setColor

Introduction

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

Prototype

public abstract void setColor(Color c);

Source Link

Document

Sets this graphics context's current color to the specified color.

Usage

From source file:TexturedPanel.java

/**
 * Creates a new TexturePaint using the provided colors.
 *//* w ww  . j a va  2 s. 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:com.tremolosecurity.scale.totp.TotpController.java

@PostConstruct
public void init() {
    this.error = null;
    HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
            .getRequest();/*from   w  ww  .jav a 2  s. 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";
    }

}

From source file:AreaSubtract.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    double halfWdith = getSize().width / 2;
    double halfHeight = getSize().height / 2;

    stem.setFrame(halfWdith, halfHeight - 42, 40.0, 40.0);
    st1 = new Area(stem);
    stem.setFrame(halfWdith + 3, halfHeight - 47, 50.0, 50.0);
    st2 = new Area(stem);
    st1.subtract(st2);/*from   www.  j a va 2  s  .  com*/
    g2.fill(st1);

    g2.setColor(Color.yellow);

}

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

@Override
public BufferedImage createCaptcha(char[] text) {
    if (text == null || text.length == 0) {
        throw new IllegalArgumentException("No captcha text given");
    }/*from www  .j  av a2 s.c  om*/

    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = image.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setBackground(Color.WHITE);
    g2d.setColor(Color.BLACK);

    clearCanvas(g2d);

    if (showGrid) {
        drawGrid(g2d);
    }

    int charMaxWidth = width / text.length;
    int xPos = 0;
    for (char ch : text) {
        drawCharacter(g2d, ch, xPos, charMaxWidth);
        xPos += charMaxWidth;
    }

    g2d.dispose();
    return image;
}

From source file:facebean.FaceBean.java

@Override
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    // Face/*from   ww w . java  2 s .  c  om*/
    int w = getWidth();
    int h = getHeight();
    int pad = 12;
    int cw = w - pad * 2;
    int ch = h - pad * 2;
    g2.setColor(getBackground());
    g2.fillArc(pad, pad, cw, ch, 0, 360);
    g2.setColor(getForeground());
    g2.drawArc(pad, pad, cw, ch, 0, 360);
    // Mouth
    int sw = cw / 2;
    int sh = ch / 2;
    if (mSmile == true)
        g2.drawArc(w / 2 - sw / 2, h / 2 - sh / 2, sw, sh, 270 - mMouthWidth / 2, mMouthWidth);
    else
        g2.drawArc(w / 2 - sw / 2, h / 2 + sh / 3, sw, sh, 90 - mMouthWidth / 2, mMouthWidth);
    // Eyes
    int er = 4;
    g2.fillArc(w / 2 - cw * 1 / 8 - er / 2, h / 2 - ch / 4 - er, er, er, 0, 360);
    g2.fillArc(w / 2 + cw * 1 / 8 - er / 2, h / 2 - ch / 4 - er, er, er, 0, 360);
}

From source file:Composite.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;

    Dimension d = getSize();/*from  w ww  .jav  a 2 s.  c o m*/
    int w = d.width;
    int h = d.height;

    BufferedImage buffImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    Graphics2D gbi = buffImg.createGraphics();

    g2.setColor(Color.white);
    g2.fillRect(0, 0, d.width, d.height);

    int rectx = w / 4;
    int recty = h / 4;

    gbi.setColor(new Color(0.0f, 0.0f, 1.0f, 1.0f));
    gbi.fill(new Rectangle2D.Double(rectx, recty, 150, 100));
    gbi.setColor(new Color(1.0f, 0.0f, 0.0f, 1.0f));
    gbi.setComposite(ac);
    gbi.fill(new Ellipse2D.Double(rectx + rectx / 2, recty + recty / 2, 150, 100));

    g2.drawImage(buffImg, null, 0, 0);
}

From source file:TexturedPanel.java

/**
 * Creates a new TexturePaint using the provided colors and texture map.
 *//* w  ww . ja va  2s.c  om*/
private void setupTexturePainter(Color foreground, Color background, boolean[][] texture, int scale) {
    if (texture == null || texture.length < 1 || texture[0].length < 1) {
        setupDefaultPainter(foreground, background);
        return;
    }

    else if (foreground == null || background == null) {
        ourPainter = null;
        return;
    }

    scale = Math.max(1, scale);
    int w = texture[0].length;
    int h = texture.length;

    BufferedImage buff = new BufferedImage(w * scale, h * scale, BufferedImage.TYPE_INT_ARGB_PRE);

    Graphics2D g2 = buff.createGraphics();

    g2.setColor(background);
    g2.fillRect(0, 0, w * scale, h * scale);

    g2.setColor(foreground);
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            try {
                if (texture[i][j])
                    g2.fillRect(j * scale, i * scale, scale, scale);
            }
            // g2.drawLine(j, i, j, i); }
            catch (ArrayIndexOutOfBoundsException aob) {
            }
        }
    }

    ourPainter = new TexturePaint(buff, new Rectangle(0, 0, w * scale, h * scale));

    g2.dispose();
}

From source file:net.technicpack.ui.lang.ResourceLoader.java

public BufferedImage colorImage(BufferedImage loadImg, Color color) {
    BufferedImage img = new BufferedImage(loadImg.getWidth(), loadImg.getHeight(), BufferedImage.TRANSLUCENT);
    Graphics2D graphics = img.createGraphics();

    graphics.setColor(color);
    graphics.drawImage(loadImg, null, 0, 0);
    graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, 1.0f));
    graphics.fillRect(0, 0, loadImg.getWidth(), loadImg.getHeight());

    graphics.dispose();//from  w  ww .  j  ava  2  s . c  om
    return img;
}

From source file:net.sf.dynamicreports.jasper.builder.JasperConcatenatedReportBuilder.java

public JasperConcatenatedReportBuilder toPng(OutputStream outputStream, float zoom) throws DRException {
    Validate.notNull(outputStream, "outputStream must not be null");
    Validate.isTrue(zoom > 0, "zoom must be > 0");

    int maxWidth = 0;
    int maxHeight = 0;

    for (JasperPrint jasperPrint : jasperReportHandler.getPrintList()) {
        int pages = jasperPrint.getPages().size();
        int pageWidth = (int) (jasperPrint.getPageWidth() * zoom);
        maxWidth += pageWidth * pages + (pages - 1) + 2;
        int height = (int) (jasperPrint.getPageHeight() * zoom) + 2;
        if (height > maxHeight) {
            maxHeight = height;/*from  w  w w  .ja v  a2  s .c  o m*/
        }
    }

    Image pageImage = new BufferedImage(maxWidth, maxHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = (Graphics2D) pageImage.getGraphics();
    g2d.setColor(Color.LIGHT_GRAY);
    g2d.fill(new Rectangle2D.Float(1, 1, maxWidth - 1, maxHeight - 1));

    int offset = 1;
    for (JasperPrint jasperPrint : jasperReportHandler.getPrintList()) {
        int pageWidth = (int) (jasperPrint.getPageWidth() * zoom);
        for (int i = 0; i < jasperPrint.getPages().size(); i++) {
            try {
                JRGraphics2DExporter exporter = new JRGraphics2DExporter();
                exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
                exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, pageImage.getGraphics());
                exporter.setParameter(JRGraphics2DExporterParameter.OFFSET_X, offset);
                exporter.setParameter(JRGraphics2DExporterParameter.OFFSET_Y, 1);
                exporter.setParameter(JRExporterParameter.PAGE_INDEX, new Integer(i));
                exporter.setParameter(JRGraphics2DExporterParameter.ZOOM_RATIO, new Float(zoom));
                exporter.exportReport();
                offset += pageWidth + 1;
            } catch (JRException e) {
                throw new DRException(e);
            }
        }
    }
    try {
        ImageIO.write((RenderedImage) pageImage, "png", outputStream);
    } catch (IOException e) {
        throw new DRException(e);
    }
    return this;
}

From source file:c.depthchart.ViewerPanel.java

private void writeStats(Graphics2D g2)
/* write statistics in bottom-left corner, or
   "Loading" at start time *//*from   w w w . j  a va  2s  .  co m*/
{
    g2.setColor(Color.BLUE);
    g2.setFont(msgFont);
    int panelHeight = getHeight();
    if (imageCount > 0) {
        double avgGrabTime = (double) totalTime / imageCount;
        g2.drawString("Pic " + imageCount + "  " + df.format(avgGrabTime) + " ms", 5, panelHeight - 10); // bottom left
    } else // no image yet
        g2.drawString("Loading...", 5, panelHeight - 10);
}