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:be.fedict.eid.idp.sp.PhotoServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    LOG.debug("doGet");
    response.setContentType("image/jpg");
    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, max-age=-1"); // http 1.1
    response.setHeader("Pragma", "no-cache, no-store"); // http 1.0
    response.setDateHeader("Expires", -1);
    ServletOutputStream out = response.getOutputStream();
    HttpSession session = request.getSession();

    byte[] photoData = (byte[]) session.getAttribute(PHOTO_SESSION_ATTRIBUTE);

    if (null != photoData) {
        BufferedImage photo = ImageIO.read(new ByteArrayInputStream(photoData));
        if (null == photo) {
            /*/* w  w w.  j  ava 2 s. co  m*/
             * In this case we render a photo containing some error message.
             */
            photo = new BufferedImage(140, 200, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = (Graphics2D) photo.getGraphics();
            RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING,
                    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            graphics.setRenderingHints(renderingHints);
            graphics.setColor(Color.WHITE);
            graphics.fillRect(1, 1, 140 - 1 - 1, 200 - 1 - 1);
            graphics.setColor(Color.RED);
            graphics.setFont(new Font("Dialog", Font.BOLD, 20));
            graphics.drawString("Photo Error", 0, 200 / 2);
            graphics.dispose();
            ImageIO.write(photo, "jpg", out);
        } else {
            out.write(photoData);
        }
    }
    out.close();
}

From source file:nl.b3p.kaartenbalie.core.server.b3pLayering.ConfigLayer.java

protected BufferedImage createBaseImage(OGCRequest ogcrequest, Map parameterMap) throws Exception {

    Boolean transparant = (Boolean) parameterMap.get("transparant");
    // Image width & heigth...
    int width = Integer.parseInt(ogcrequest.getParameter(OGCConstants.WMS_PARAM_WIDTH));
    int height = Integer.parseInt(ogcrequest.getParameter(OGCConstants.WMS_PARAM_HEIGHT));
    BufferedImage bufImage = null;

    bufImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bufImage.createGraphics();
    if (!transparant.booleanValue()) {
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, width, height);
    }/*from  ww w  .  j  a v  a2 s . co  m*/
    return bufImage;
}

From source file:be.fedict.eid.applet.service.PhotoServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    LOG.debug("doGet");
    response.setContentType("image/jpg");
    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, max-age=-1"); // http 1.1
    response.setHeader("Pragma", "no-cache, no-store"); // http 1.0
    response.setDateHeader("Expires", -1);
    ServletOutputStream out = response.getOutputStream();
    HttpSession session = request.getSession();
    byte[] photoData = (byte[]) session.getAttribute(IdentityDataMessageHandler.PHOTO_SESSION_ATTRIBUTE);
    if (null != photoData) {
        BufferedImage photo = ImageIO.read(new ByteArrayInputStream(photoData));
        if (null == photo) {
            /*//  ww w .jav a2 s  . c o  m
             * In this case we render a photo containing some error message.
             */
            photo = new BufferedImage(140, 200, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = (Graphics2D) photo.getGraphics();
            RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING,
                    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            graphics.setRenderingHints(renderingHints);
            graphics.setColor(Color.WHITE);
            graphics.fillRect(1, 1, 140 - 1 - 1, 200 - 1 - 1);
            graphics.setColor(Color.RED);
            graphics.setFont(new Font("Dialog", Font.BOLD, 20));
            graphics.drawString("Photo Error", 0, 200 / 2);
            graphics.dispose();
            ImageIO.write(photo, "jpg", out);
        } else {
            out.write(photoData);
        }
    }
    out.close();
}

From source file:TexturedPanel.java

/**
 * Paints this component with its textured background.
 */// ww  w  . j  a v a2  s .  c o m
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    if (ourPainter != null) {
        int w = getWidth();
        int h = getHeight();
        Insets in = getInsets();

        int x = in.left;
        int y = in.top;
        w = w - in.left - in.right;
        h = h - in.top - in.bottom;

        if (w >= 0 && h >= 0) {
            Graphics2D g2 = (Graphics2D) g;
            Paint pt = g2.getPaint();
            g2.setPaint(ourPainter);
            g2.fillRect(x, y, w, h);
            g2.setPaint(pt);
        }
    }
}

From source file:test.be.fedict.eid.applet.JGraphTest.java

private void graphToFile(BasicVisualizationServer<String, String> visualization, File file) throws IOException {
    Dimension size = visualization.getSize();
    int width = (int) (size.getWidth() + 1);
    int height = (int) (size.getHeight() + 1);
    LOG.debug("width: " + width);
    LOG.debug("height: " + height);
    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = bufferedImage.createGraphics();
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics.setColor(Color.WHITE);
    graphics.fillRect(0, 0, 900, 650);
    visualization.setBounds(0, 0, 900, 650);
    visualization.paint(graphics);/*from  w  w w  .j  a  v  a  2  s .  c  o m*/
    graphics.dispose();
    ImageIO.write(bufferedImage, "png", file);
}

From source file:org.apache.fop.util.BitmapImageUtilTestCase.java

private BufferedImage createTestImage() {
    BufferedImage buf = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = buf.createGraphics();
    g2d.setBackground(Color.WHITE);
    g2d.clearRect(0, 0, buf.getWidth(), buf.getHeight());

    //A few rectangles rotated and with different color
    Graphics2D copy = (Graphics2D) g2d.create();
    copy.translate(170, 170);//  w w w  .  ja  v a 2  s  .  c o m
    int c = 12;
    for (int i = 0; i < c; i++) {
        float f = ((i + 1) / (float) c);
        Color col = new Color(0.0f, 1 - f, 0.0f);
        copy.setColor(col);
        copy.fillRect(0, 0, 120, 120);
        copy.rotate(-2 * Math.PI / c);
    }
    copy.dispose();

    //the same in gray scales
    copy = (Graphics2D) g2d.create();
    copy.translate(470, 310);
    c = 12;
    for (int i = 0; i < c; i++) {
        float f = ((i + 1) / (float) c);
        Color col = new Color(f, f, f);
        copy.setColor(col);
        copy.fillRect(0, 0, 120, 120);
        copy.rotate(-2 * Math.PI / c);
    }
    copy.dispose();
    return buf;
}

From source file:Paints.java

/** Draw the example */
public void paint(Graphics g1) {
    Graphics2D g = (Graphics2D) g1;
    // Paint the entire background using a GradientPaint.
    // The background color varies diagonally from deep red to pale blue
    g.setPaint(new GradientPaint(0, 0, new Color(150, 0, 0), WIDTH, HEIGHT, new Color(200, 200, 255)));
    g.fillRect(0, 0, WIDTH, HEIGHT); // fill the background

    // Use a different GradientPaint to draw a box.
    // This one alternates between deep opaque green and transparent green.
    // Note: the 4th arg to Color() constructor specifies color opacity
    g.setPaint(new GradientPaint(0, 0, new Color(0, 150, 0), 20, 20, new Color(0, 150, 0, 0), true));
    g.setStroke(new BasicStroke(15)); // use wide lines
    g.drawRect(25, 25, WIDTH - 50, HEIGHT - 50); // draw the box

    // The glyphs of fonts can be used as Shape objects, which enables
    // us to use Java2D techniques with letters Just as we would with
    // any other shape. Here we get some letter shapes to draw.
    Font font = new Font("Serif", Font.BOLD, 10); // a basic font
    Font bigfont = // a scaled up version
            font.deriveFont(AffineTransform.getScaleInstance(30.0, 30.0));
    GlyphVector gv = bigfont.createGlyphVector(g.getFontRenderContext(), "JAV");
    Shape jshape = gv.getGlyphOutline(0); // Shape of letter J
    Shape ashape = gv.getGlyphOutline(1); // Shape of letter A
    Shape vshape = gv.getGlyphOutline(2); // Shape of letter V

    // We're going to outline the letters with a 5-pixel wide line
    g.setStroke(new BasicStroke(5.0f));

    // We're going to fake shadows for the letters using the
    // following Paint and AffineTransform objects
    Paint shadowPaint = new Color(0, 0, 0, 100); // Translucent black
    AffineTransform shadowTransform = AffineTransform.getShearInstance(-1.0, 0.0); // Shear to the right
    shadowTransform.scale(1.0, 0.5); // Scale height by 1/2

    // Move to the baseline of our first letter
    g.translate(65, 270);//ww w  . j  ava  2s . co  m

    // Draw the shadow of the J shape
    g.setPaint(shadowPaint);
    g.translate(15, 20); // Compensate for the descender of the J
    // transform the J into the shape of its shadow, and fill it
    g.fill(shadowTransform.createTransformedShape(jshape));
    g.translate(-15, -20); // Undo the translation above

    // Now fill the J shape with a solid (and opaque) color
    g.setPaint(Color.blue); // Fill with solid, opaque blue
    g.fill(jshape); // Fill the shape
    g.setPaint(Color.black); // Switch to solid black
    g.draw(jshape); // And draw the outline of the J

    // Now draw the A shadow
    g.translate(75, 0); // Move to the right
    g.setPaint(shadowPaint); // Set shadow color
    g.fill(shadowTransform.createTransformedShape(ashape)); // draw shadow

    // Draw the A shape using a solid transparent color
    g.setPaint(new Color(0, 255, 0, 125)); // Transparent green as paint
    g.fill(ashape); // Fill the shape
    g.setPaint(Color.black); // Switch to solid back
    g.draw(ashape); // Draw the outline

    // Move to the right and draw the shadow of the letter V
    g.translate(175, 0);
    g.setPaint(shadowPaint);
    g.fill(shadowTransform.createTransformedShape(vshape));

    // We're going to fill the next letter using a TexturePaint, which
    // repeatedly tiles an image. The first step is to obtain the image.
    // We could load it from an image file, but here we create it
    // ourselves by drawing a into an off-screen image. Note that we use
    // a GradientPaint to fill the off-screen image, so the fill pattern
    // combines features of both Paint classes.
    BufferedImage tile = // Create an image
            new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB);
    Graphics2D tg = tile.createGraphics(); // Get its Graphics for drawing
    tg.setColor(Color.pink);
    tg.fillRect(0, 0, 50, 50); // Fill tile background with pink
    tg.setPaint(new GradientPaint(40, 0, Color.green, // diagonal gradient
            0, 40, Color.gray)); // green to gray
    tg.fillOval(5, 5, 40, 40); // Draw a circle with this gradient

    // Use this new tile to create a TexturePaint and fill the letter V
    g.setPaint(new TexturePaint(tile, new Rectangle(0, 0, 50, 50)));
    g.fill(vshape); // Fill letter shape
    g.setPaint(Color.black); // Switch to solid black
    g.draw(vshape); // Draw outline of letter

    // Move to the right and draw the shadow of the final A
    g.translate(160, 0);
    g.setPaint(shadowPaint);
    g.fill(shadowTransform.createTransformedShape(ashape));

    g.fill(ashape); // Fill letter A
    g.setPaint(Color.black); // Revert to solid black
    g.draw(ashape); // Draw the outline of the A
}

From source file:com.us.servlet.AuthCode.java

protected void service(HttpServletRequest request, HttpServletResponse response) {
    final CodeAuth bean = AppHelper.CODE_AUTH;
    int width = NumberUtils.toInt(request.getParameter("width"), bean.getWidth());
    int height = NumberUtils.toInt(request.getParameter("height"), bean.getHeight());
    int x = width / (bean.getLength() + 1);
    int codeY = height - 4;
    int fontHeight = height - 2;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics2D graphics = image.createGraphics();
    graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    if (StringUtil.hasText(request.getParameter("bgcolor"))) {
        graphics.setBackground(ColorHelper.hex2RGB(request.getParameter("bgcolor")));
    }/*from  w w  w .  j  av a  2  s .  c  o  m*/
    graphics.fillRect(0, 0, width, height);
    graphics.setFont(new Font(bean.getFont(), Font.BOLD, fontHeight));
    graphics.drawRect(0, 0, width - 1, height - 1);
    // 
    if (bean.isBreakLine()) {
        for (int i = 0; i < 15; i++) {
            int x1 = RandomUtils.nextInt(width);
            int y1 = RandomUtils.nextInt(height);
            int x2 = RandomUtils.nextInt(12);
            int y2 = RandomUtils.nextInt(12);
            graphics.drawLine(x1, y1, x + x2, y1 + y2);
        }
    }
    char[] CHARSET_AREA = null;
    if (bean.getType().charAt(0) == '1') {
        CHARSET_AREA = ArrayUtils.addAll(CHARSET_AREA, BIG_LETTERS);
    }
    if (bean.getType().charAt(1) == '1') {
        CHARSET_AREA = ArrayUtils.addAll(CHARSET_AREA, SMALL_LETTER);
    }
    if (bean.getType().charAt(2) == '1') {
        CHARSET_AREA = ArrayUtils.addAll(CHARSET_AREA, NUMBERS);
    }
    StringBuilder randomCode = new StringBuilder();
    for (int i = 0; i < bean.getLength(); i++) {
        String rand = String.valueOf(CHARSET_AREA[RandomUtils.nextInt(CHARSET_AREA.length)]);
        graphics.setColor(ColorHelper.color(RandomUtils.nextInt(255), RandomUtils.nextInt(255),
                RandomUtils.nextInt(255)));
        graphics.drawString(rand, (i + 1) * x, codeY);
        randomCode.append(rand);
    }
    HttpSession session = request.getSession();
    session.setAttribute(bean.getSessionKey(), randomCode.toString());
    // ?
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setContentType("image/png");
    try {
        // Servlet?
        ServletOutputStream sos = response.getOutputStream();
        ImageIO.write(image, "png", sos);
        sos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

/**
 * renders one frame of the animation./*w  w w .ja v a2 s  .c  om*/
 */
private Image renderFrame(ITimePosition[] sortedTimeArray, int currentTimeIndex, int screenW, int screenH,
        IBoundingBox bbox, Set<OXFFeature> selectedFeatures, ObservationSeriesCollection obsValues) {
    ContextBoundingBox contextBBox = new ContextBoundingBox(bbox);

    BufferedImage image = new BufferedImage(screenW, screenH, BufferedImage.TYPE_INT_RGB);

    Graphics2D g = image.createGraphics();

    ITimePosition currentTimePos = sortedTimeArray[currentTimeIndex];

    // draw white background:
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, screenW, screenH);

    // draw time-string into map:
    g.setColor(Color.BLACK);
    g.drawString(currentTimePos.toString(), 20, 20);

    for (OXFFeature dotFeature : selectedFeatures) {

        //
        // draw the points into the image at the georeferenced position of the corresponding feature:
        //
        Point pRealWorld = (Point) dotFeature.getGeometry();

        java.awt.Point pScreen = ContextBoundingBox.realworld2Screen(contextBBox.getActualBBox(), screenW,
                screenH, new Point2D.Double(pRealWorld.getCoordinate().x, pRealWorld.getCoordinate().y));

        ObservedValueTuple tuple = obsValues.getTuple(dotFeature, currentTimePos);

        // if there wasn't a tuple for the current time position go backwards through the sortedTimeArray and take the most recent one:
        int j = currentTimeIndex - 1;
        while (tuple == null && j >= 0) {
            tuple = obsValues.getTuple(dotFeature, sortedTimeArray[j]);
            j--;
        }

        // if a tuple was found -> draw the dot:
        if (tuple != null) {
            int dotSize = computeDotSize((Double) tuple.getValue(0), (Double) obsValues.getMinimum(0),
                    (Double) obsValues.getMaximum(0));
            g.setColor(POINT_INNER_COLOR);
            g.fillOval(pScreen.x - (dotSize / 2), pScreen.y - (dotSize / 2), dotSize, dotSize);
        }
        // otherwise draw "no data available"
        else {
            g.setColor(Color.BLACK);

            // draw point of feature:
            g.fillOval(pScreen.x - (FeatureGeometryRenderer.DOT_SIZE_POINT / 2),
                    pScreen.y - (FeatureGeometryRenderer.DOT_SIZE_POINT / 2),
                    FeatureGeometryRenderer.DOT_SIZE_POINT, FeatureGeometryRenderer.DOT_SIZE_POINT);

            g.drawString("No data available", pScreen.x + X_SHIFT, pScreen.y + Y_SHIFT);
        }
    }

    return image;
}

From source file:org.eclipse.wb.internal.swing.model.property.editor.beans.PropertyEditorWrapper.java

private Image paintValue(GC gc, int width, int height) throws Exception {
    // create AWT graphics
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics2D = (Graphics2D) image.getGraphics();
    // prepare color's
    Color background = gc.getBackground();
    Color foreground = gc.getForeground();
    // fill graphics
    graphics2D.setColor(SwingUtils.getAWTColor(background));
    graphics2D.fillRect(0, 0, width, height);
    // set color//from www  .j  a  v  a2  s.c o m
    graphics2D.setBackground(SwingUtils.getAWTColor(background));
    graphics2D.setColor(SwingUtils.getAWTColor(foreground));
    // set font
    FontData[] fontData = gc.getFont().getFontData();
    String name = fontData.length > 0 ? fontData[0].getName() : "Arial";
    graphics2D.setFont(new java.awt.Font(name, java.awt.Font.PLAIN, graphics2D.getFont().getSize() - 1));
    // paint image
    m_propertyEditor.paintValue(graphics2D, new java.awt.Rectangle(0, 0, width, height));
    // conversion
    try {
        return SwingImageUtils.convertImage_AWT_to_SWT(image);
    } finally {
        image.flush();
        graphics2D.dispose();
    }
}