Example usage for java.awt Graphics2D drawRect

List of usage examples for java.awt Graphics2D drawRect

Introduction

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

Prototype

public void drawRect(int x, int y, int width, int height) 

Source Link

Document

Draws the outline of the specified rectangle.

Usage

From source file:org.openmeetings.app.data.record.BatikMethods.java

public void paintRect(Graphics2D g2d, int x, int y, int width, int height, Color linecoler, int thickness,
        Color fillColor) throws Exception {

    //int x, int y, int width, int height
    //g2d.fill(new Rectangle(x,y,width,height));

    g2d.setStroke(new BasicStroke(thickness));

    if (linecoler != null) {
        g2d.setPaint(linecoler);//from  w w  w  . ja  v  a  2s .  c o m
        g2d.drawRect(x, y, width, height);
    }

    if (fillColor != null) {
        g2d.setPaint(fillColor);
        g2d.fillRect(x, y, width, height);
    }

}

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);/*www  .ja v a 2 s . 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:de.tor.tribes.ui.algo.TimeFrameVisualizer.java

private void renderPopup(HashMap<String, Object> pPopupInfo, Graphics2D pG2D) {
    Point location = (Point) pPopupInfo.get("popup.location");
    String label = (String) pPopupInfo.get("popup.label");
    if (location == null || label == null) {
        return;/*from  www.j  a  va 2  s.c  o  m*/
    }
    pG2D.setColor(new Color(255, 255, 204));
    Rectangle2D labelBounds = pG2D.getFontMetrics().getStringBounds(label, pG2D);
    pG2D.fillRect(location.x, location.y - (int) labelBounds.getHeight(), (int) labelBounds.getWidth() + 5,
            (int) labelBounds.getHeight() + 5);
    pG2D.setColor(Color.BLACK);
    pG2D.drawRect(location.x, location.y - (int) labelBounds.getHeight(), (int) labelBounds.getWidth() + 5,
            (int) labelBounds.getHeight() + 5);
    pG2D.drawString(label, location.x + 2, location.y);

}

From source file:cish.CISH.java

/**
 * Inits some icons.//from w w w. j ava 2  s.  c  o m
 */
private void initComponents2() {
    BufferedImage bi = new BufferedImage(8, 8, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = bi.createGraphics();
    g.setColor(new Color(COLORS[0][0], COLORS[0][1], COLORS[0][2]));
    g.drawRect(0, 0, 7, 7);
    jLabel3.setIcon(new ImageIcon(bi));

    bi = new BufferedImage(8, 8, BufferedImage.TYPE_INT_ARGB);
    g = bi.createGraphics();
    g.setColor(new Color(COLORS[1][0], COLORS[1][1], COLORS[1][2]));
    g.drawRect(0, 0, 7, 7);
    jLabel4.setIcon(new ImageIcon(bi));

    bi = new BufferedImage(8, 8, BufferedImage.TYPE_INT_ARGB);
    g = bi.createGraphics();
    g.setColor(new Color(COLORS[4][0], COLORS[4][1], COLORS[4][2]));
    g.drawRect(0, 0, 7, 7);
    jLabel5.setIcon(new ImageIcon(bi));
}

From source file:org.squidy.designer.components.versioning.Versioning.java

@Override
protected void paintShape(PPaintContext paintContext) {
    super.paintShape(paintContext);

    Graphics2D g = paintContext.getGraphics();

    PBounds bounds = getBoundsReference();
    double x = bounds.getX();
    double y = bounds.getY();
    double width = bounds.getWidth();
    double height = bounds.getHeight();

    //      g.setColor(Color.RED);
    //      g.draw(bounds);

    g.setClip(bounds);/* w w  w  . j  a va  2s.  com*/

    for (int i = 0; i < 7; i++) {
        PAffineTransform transform = new PAffineTransform();
        transform.scale(0.12, 0.12);
        transform.translate(i * 122, 0);

        paintContext.pushTransform(transform);

        if (!isRenderPrimitive()) {
            versionNode.fullPaint(paintContext);
        }

        if (i == 6) {
            g.setStroke(StrokeUtils.getBasicStroke(5f));
            g.setColor(Color.GRAY);
            if (isRenderPrimitiveRect())
                g.drawRect((int) x, (int) y, 100, (int) (height / 0.12));
            else
                g.drawRoundRect((int) x, (int) y, 100, (int) (height / 0.12), 15, 15);

            g.setColor(COLOR_FILL);
            if (isRenderPrimitiveRect())
                g.fillRect((int) x, (int) y, 100, (int) (height / 0.12));
            else
                g.fillRoundRect((int) x, (int) y, 100, (int) (height / 0.12), 15, 15);
        }

        paintContext.popTransform(transform);
    }

    g.setClip(null);
}

From source file:org.squidy.designer.zoom.NavigationShape.java

@Override
protected void paintShapeZoomedIn(PPaintContext paintContext) {
    super.paintShapeZoomedIn(paintContext);

    Graphics2D g = (Graphics2D) paintContext.getGraphics();

    if (showNavigation) {// && !isHierarchicalZoomInProgress()) {

        PBounds bounds = getBoundsReference();

        int x = (int) bounds.getX();
        int y = (int) bounds.getY();
        int width = (int) bounds.getWidth();
        int height = (int) bounds.getHeight();

        g.setColor(Constants.Color.COLOR_SHAPE_BACKGROUND);
        if (isRenderPrimitiveRect())
            g.fillRect(x, y, width, 60);
        else {/*ww w.  jav  a 2 s.co m*/
            g.clearRect(x, y, width, 60);
            g.fillRoundRect(x, y, width, 60, 25, 25);
        }

        g.setColor(Constants.Color.COLOR_SHAPE_BORDER);
        if (isRenderPrimitiveRect())
            g.drawRect(x, y, width, 60);
        else
            g.drawRoundRect(x, y, width, 60, 25, 25);

        g.setFont(fontBreadcrumb);

        if (titleBounds == null) {
            FontMetrics fm = g.getFontMetrics();
            titleBounds = new Rectangle2D.Double(bounds.getX() + 455 + titleGap,
                    bounds.getY() + 25 - fm.getHeight(), FontUtils.getWidthOfText(fm, getTitle()) + 10,
                    fm.getHeight() + 5);
        }

        // Font font = internalFont.deriveFont(3.2f);
        for (int i = 0; i < 3; i++) {
            if (isRenderPrimitiveRect())
                g.fillRect((int) (bounds.getX() + 430), (int) bounds.getY() + i * 15 + 10, 5, 10);
            else
                g.fillOval((int) (bounds.getX() + 430), (int) bounds.getY() + i * 15 + 10, 5, 10);
        }

        if (!ShapeUtils.isApparent(titleInputWrapper)) {
            g.drawString(getTitle(), (int) (bounds.getX() + 460 + titleGap), (int) (bounds.getY() + 25));
        }

        if (croppedBreadcrumb == null) {
            croppedBreadcrumb = FontUtils.createCroppedLabelIfNecessary(g.getFontMetrics(), getBreadcrumb(),
                    (int) bounds.getWidth() * 10 - 450);
        }
        g.drawString(croppedBreadcrumb, (int) (bounds.getX() + 460), (int) (bounds.getY() + 50));
    }
}

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  a  2 s . 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.fhcrc.cpl.viewer.quant.gui.PanelWithSpectrumChart.java

License:asdf

/**
 *
 * @param imageWidthEachScan/*from   w w w .  ja va  2  s  .  c  o  m*/
 * @param imageHeightEachScan
 * @param maxTotalImageHeight a hard boundary on the total image height.  If imageHeightEachScan is too big,
 * given the total number of charts and this arg, it gets knocked down
 * @param outputFile
 * @throws java.io.IOException
 */
public void savePerScanSpectraImage(int imageWidthEachScan, int imageHeightEachScan, int maxTotalImageHeight,
        File outputFile) throws IOException {
    int numCharts = scanLineChartMap.size();

    int widthPaddingForLabels = 50;

    imageHeightEachScan = Math.min(imageHeightEachScan, maxTotalImageHeight / numCharts);

    List<Integer> allScanNumbers = new ArrayList<Integer>(scanLineChartMap.keySet());
    Collections.sort(allScanNumbers);
    List<PanelWithChart> allCharts = new ArrayList<PanelWithChart>();

    for (int scanNumber : allScanNumbers) {
        PanelWithLineChart scanChart = scanLineChartMap.get(scanNumber);
        allCharts.add(scanChart);
        scanChart.setSize(imageWidthEachScan - widthPaddingForLabels, imageHeightEachScan);
    }

    BufferedImage perScanChartImage = MultiChartDisplayPanel.createImageForAllCharts(allCharts);

    BufferedImage perScanChartImageWithLabels = new BufferedImage(imageWidthEachScan,
            perScanChartImage.getHeight(), BufferedImage.TYPE_INT_RGB);

    Graphics2D g = perScanChartImageWithLabels.createGraphics();
    g.drawImage(perScanChartImage, widthPaddingForLabels, 0, null);
    g.setPaint(Color.WHITE);
    g.drawRect(0, 0, widthPaddingForLabels, perScanChartImage.getHeight());

    for (int i = 0; i < allCharts.size(); i++) {
        int scanNumber = allScanNumbers.get(i);

        int chartTop = i * imageHeightEachScan;
        int chartMiddle = chartTop + (imageHeightEachScan / 2);

        if (lightFirstScanLine > 0 && lightLastScanLine > 0) {
            if (scanNumber >= lightFirstScanLine && scanNumber <= lightLastScanLine)
                g.setPaint(Color.GREEN);
            else
                g.setPaint(Color.RED);
        } else
            g.setPaint(Color.BLACK);

        g.drawString("" + scanNumber, 5, chartMiddle);
    }
    g.dispose();

    ImageIO.write(perScanChartImageWithLabels, "png", outputFile);
}

From source file:org.squidy.designer.shape.ZoomShape.java

/**
 * @param paintContext/*from   w w  w  .j a v a2s.c o m*/
 */
protected void paintShapeZoomedIn(PPaintContext paintContext) {

    Shape shapeZoomedIn = getZoomedInShape();
    if (shapeZoomedIn != null) {
        Graphics2D g = paintContext.getGraphics();
        Rectangle bounds = shapeZoomedIn.getBounds();

        g.setPaint(getZoomedInFillPaint());
        if (isRenderPrimitiveRect())
            g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
        else
            g.fill(shapeZoomedIn);

        g.setStroke(STROKE_ZOOMED_IN);
        g.setPaint(getZoomedInDrawPaint());
        if (isRenderPrimitiveRect())
            g.drawRect(bounds.x, bounds.y, bounds.width, bounds.height);
        else
            g.draw(shapeZoomedIn);
    }
}

From source file:org.squidy.designer.shape.ZoomShape.java

/**
 * @param paintContext/*from   w  w  w.j  av a 2 s. co  m*/
 */
protected void paintShapeZoomedOut(PPaintContext paintContext) {

    Shape shapeZoomedOut = getZoomedOutShape();
    if (shapeZoomedOut != null) {
        Graphics2D g = paintContext.getGraphics();
        Rectangle bounds = shapeZoomedOut.getBounds();

        g.setPaint(getZoomedOutFillPaint());
        if (isRenderPrimitiveRect())
            g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
        else
            g.fill(shapeZoomedOut);

        g.setStroke(STROKE_ZOOMED_OUT);
        g.setPaint(getZoomedOutDrawPaint());
        if (isRenderPrimitiveRect())
            g.drawRect(bounds.x, bounds.y, bounds.width, bounds.height);
        else
            g.draw(shapeZoomedOut);
    }
}