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:edu.ku.brc.ui.GradiantButton.java

/**
 * Draws the button body/*from  w  w w.j ava 2s . c o  m*/
 * @param g2 the graphics to be painted into
 * @param w the width of the control
 * @param h the height of the control
 * @param color the of the background
 */
protected void drawButtonBody(Graphics2D g2, int w, int h, Color color) {
    // draw the button body
    Color grad_top = color.brighter();
    Color grad_bot = color.darker();
    GradientPaint bg = new GradientPaint(new Point(0, 0), grad_top, new Point(0, h), grad_bot);
    g2.setPaint(bg);
    g2.fillRect(0, 0, w, h);
}

From source file:uk.co.modularaudio.service.audioanalysis.impl.analysers.StaticThumbnailAnalyser.java

@Override
public void completeAnalysis(final AnalysisContext context, final AnalysedData analysedData,
        final HashedRef hashedRef) {
    final float rmsDb = AudioMath.levelToDbF(maxRmsValue);

    final float adjustedDb = (THUMBNAIL_DB - rmsDb);

    final float adjustmentAbs = AudioMath.dbToLevelF(adjustedDb);

    final Graphics2D g2d = (Graphics2D) og2d.create(BORDER_WIDTH, BORDER_WIDTH, usableWidth, usableHeight);

    //      g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
    g2d.setColor(Color.BLACK);//from w w w  .j  a v a 2 s .co m
    g2d.fillRect(0, 0, usableWidth, usableHeight);

    g2d.setColor(minMaxColor);

    int curDataCount = 0;
    final int oneSideHeight = (usableHeight - 1) / 2;
    final int zeroYValue = oneSideHeight;

    for (int i = 0; i < usableWidth; i++) {
        float minSample = thumbnailValues[curDataCount++] * adjustmentAbs;
        float maxSample = thumbnailValues[curDataCount++] * adjustmentAbs;
        curDataCount++;

        minSample = (minSample < -1.0f ? -1.0f : minSample);
        maxSample = (maxSample > 1.0f ? 1.0f : maxSample);

        // assume goes from +1.0 to -1.0
        final int curX = i;
        final int minY = zeroYValue + (int) (minSample * oneSideHeight);
        final int maxY = zeroYValue + (int) (maxSample * oneSideHeight);
        g2d.drawLine(curX, minY, curX, maxY);
    }

    g2d.setColor(rmsColor);

    curDataCount = 0;
    for (int i = 0; i < usableWidth; i++) {
        curDataCount += 2;
        float rms = thumbnailValues[curDataCount++] * adjustmentAbs;

        rms = (rms > 1.0f ? 1.0f : rms);

        // lSample goes from +1.0 to -1.0
        // We need it to go from 0 to height
        final int curX = i;
        final int minY = zeroYValue + (int) (rms * oneSideHeight);
        final int maxY = zeroYValue + (int) (-rms * oneSideHeight);
        g2d.drawLine(curX, minY, curX, maxY);
    }

    try {
        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        final ImageOutputStream ios = ImageIO.createImageOutputStream(os);

        ImageIO.write(bufferedImage, "png", ios);
        final InputStream contents = new ByteArrayInputStream(os.toByteArray());
        // Now save this generated thumb nail onto disk then pass the path to this in the analysed data
        hashedStorageService.storeContentsInWarehouse(hashedWarehouse, hashedRef, contents);

        analysedData
                .setPathToStaticThumbnail(hashedStorageService.getPathToHashedRef(hashedWarehouse, hashedRef));
    } catch (final Exception e) {
        final String msg = "Exception caught serialising static thumb nail: " + e.toString();
        log.error(msg, e);
    }
}

From source file:com.boundlessgeo.geoserver.api.controllers.ThumbnailController.java

/**
 * Creates a thumbnail for the layer as a Resource, and updates the layer with the new thumbnail
 * @param ws The workspace of the layer//from   ww w .j a  v a2s  .  c  o  m
 * @param layer The layer or layerGroup to get the thumbnail for
 * @return The thumbnail image as a Resource
 * @throws Exception
 */
protected void createThumbnail(WorkspaceInfo ws, PublishedInfo layer, HttpServletRequest baseRequest)
        throws Exception {
    //Sync against this map/layer
    Semaphore s = semaphores.get(layer);
    s.acquire();
    try {
        //(SUITE-1072) Initialize the thumbnail to a blank image in case the WMS request crashes geoserver
        BufferedImage blankImage = new BufferedImage(THUMBNAIL_SIZE * 2, THUMBNAIL_SIZE * 2,
                BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D g = blankImage.createGraphics();
        g.setColor(new Color(0, 0, 0, 0));
        g.fillRect(0, 0, blankImage.getWidth(), blankImage.getHeight());
        writeThumbnail(layer, blankImage);

        //Set up getMap request
        String url = baseRequest.getScheme() + "://localhost:" + baseRequest.getLocalPort()
                + baseRequest.getContextPath() + "/" + ws.getName() + "/wms/reflect";

        url += "?FORMAT=" + MIME_TYPE;

        ReferencedEnvelope bbox = null;
        if (layer instanceof LayerInfo) {
            url += "&LAYERS=" + layer.getName();
            url += "&STYLES=" + ((LayerInfo) layer).getDefaultStyle().getName();
            bbox = ((LayerInfo) layer).getResource().boundingBox();
        } else if (layer instanceof LayerGroupInfo) {

            LayerGroupHelper helper = new LayerGroupHelper((LayerGroupInfo) layer);
            bbox = ((LayerGroupInfo) layer).getBounds();
            url += "&CRS=" + CRS.toSRS(bbox.getCoordinateReferenceSystem());

            List<LayerInfo> layerList = helper.allLayersForRendering();
            if (layerList.size() > 0) {
                url += "&LAYERS=";
                for (int i = 0; i < layerList.size(); i++) {
                    if (i > 0) {
                        url += ",";
                    }
                    url += layerList.get(i) == null ? "" : layerList.get(i).getName();
                }
            }
            List<StyleInfo> styleList = helper.allStylesForRendering();
            if (styleList.size() > 0) {
                url += "&STYLES=";
                for (int i = 0; i < styleList.size(); i++) {
                    if (i > 0) {
                        url += ",";
                    }
                    if (styleList.get(i) == null) {
                        url += layerList.get(i).getDefaultStyle() == null ? ""
                                : layerList.get(i).getDefaultStyle().getName();
                    } else {
                        url += styleList.get(i) == null ? "" : styleList.get(i).getName();
                    }
                }
            }
        } else {
            throw new RuntimeException("layer must be one of LayerInfo or LayerGroupInfo");
        }
        //Set the size of the HR thumbnail
        //Take the smallest bbox dimension as the min dimension. We can then crop the other 
        //dimension to give a square thumbnail
        url += "&BBOX=" + ((float) bbox.getMinX()) + "," + ((float) bbox.getMinY()) + ","
                + ((float) bbox.getMaxX()) + "," + ((float) bbox.getMaxY());
        if (bbox.getWidth() < bbox.getHeight()) {
            url += "&WIDTH=" + (2 * THUMBNAIL_SIZE);
            url += "&HEIGHT=" + (2 * THUMBNAIL_SIZE * Math.round(bbox.getHeight() / bbox.getWidth()));
        } else {
            url += "&HEIGHT=" + (2 * THUMBNAIL_SIZE);
            url += "&WIDTH=" + (2 * THUMBNAIL_SIZE * Math.round(bbox.getWidth() / bbox.getHeight()));
        }

        //Run the getMap request through the WMS Reflector
        //WebMap response = wms.reflect(request);            
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        BufferedImage image = ImageIO.read(con.getInputStream());
        if (image == null) {
            throw new RuntimeException(
                    "Failed to encode thumbnail for " + ws.getName() + ":" + layer.getName());
        }
        writeThumbnail(layer, image);
    } finally {
        s.release();
    }
}

From source file:Textures.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g;

    g2d.setColor(new Color(212, 212, 212));
    g2d.drawRect(10, 15, 90, 60);/*from w w w .ja  v a  2s  .  com*/

    BufferedImage bimage1 = null;

    URL url1 = ClassLoader.getSystemResource("a.png");

    try {
        bimage1 = ImageIO.read(url1);
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

    Rectangle rect1 = new Rectangle(0, 0, bimage1.getWidth(), bimage1.getHeight());
    TexturePaint texture1 = new TexturePaint(bimage1, rect1);

    g2d.setPaint(texture1);
    g2d.fillRect(10, 15, 90, 60);
}

From source file:de.tor.tribes.ui.algo.TimeFrameVisualizer.java

/** Creates new form TimeFrameVisualizer */
public TimeFrameVisualizer() {
    initComponents();/*from w  w w .ja v  a 2 s. co m*/
    try {
        STROKED = new BufferedImage(3, 3, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = STROKED.createGraphics();
        g2d.setColor(Constants.DS_BACK_LIGHT);
        g2d.fillRect(0, 0, 3, 3);
        g2d.setColor(Constants.DS_BACK);
        g2d.drawLine(0, 2, 2, 0);
        g2d.dispose();
        DAILY_START_FRAME_FILL = new BufferedImage(3, 3, BufferedImage.TYPE_INT_RGB);
        g2d = DAILY_START_FRAME_FILL.createGraphics();
        g2d.setColor(Color.CYAN);
        g2d.fillRect(0, 0, 3, 3);
        g2d.setColor(Color.BLUE);
        g2d.drawLine(0, 2, 2, 0);
        g2d.dispose();
        EXACT_START_FRAME_FILL = new BufferedImage(3, 3, BufferedImage.TYPE_INT_RGB);
        g2d = EXACT_START_FRAME_FILL.createGraphics();
        g2d.setColor(Color.CYAN);
        g2d.fillRect(0, 0, 3, 3);
        g2d.dispose();
        ONE_DAY_START_FRAME_FILL = new BufferedImage(3, 3, BufferedImage.TYPE_INT_RGB);
        g2d = ONE_DAY_START_FRAME_FILL.createGraphics();
        g2d.setColor(Color.CYAN);
        g2d.fillRect(0, 0, 3, 3);
        g2d.setColor(Color.BLUE);
        g2d.drawLine(1, 0, 1, 2);
        g2d.dispose();
        ARRIVE_FRAME_FILL = new BufferedImage(3, 3, BufferedImage.TYPE_INT_RGB);
        g2d = ARRIVE_FRAME_FILL.createGraphics();
        g2d.setColor(Color.RED);
        g2d.fillRect(0, 0, 3, 3);
        g2d.setColor(Color.BLACK);
        g2d.drawLine(0, 2, 2, 0);
        g2d.dispose();
    } catch (Exception e) {
    }

    popupInfo = new HashMap<String, Object>();
    addMouseMotionListener(new MouseMotionListener() {

        @Override
        public void mouseDragged(MouseEvent e) {
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            repaint();
        }
    });

    addMouseListener(new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {
            fireClickEvent(e);
        }

        @Override
        public void mousePressed(MouseEvent e) {
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }
    });
}

From source file:de.tor.tribes.ui.algo.TimeFrameVisualizer.java

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

    if (mTimeFrame == null) {
        renderNoInfoView(g);/*from w ww .j  a v a 2  s  .co m*/
    } else {
        updateSize();
        LongRange startRange = mTimeFrame.getStartRange();
        LongRange arriveRange = mTimeFrame.getArriveRange();
        HashMap<LongRange, TimeSpan> startRanges = mTimeFrame
                .startTimespansToRangesMap(AnyTribe.getSingleton());
        HashMap<LongRange, TimeSpan> arriveRanges = mTimeFrame.arriveTimespansToRangesMap(null);
        long minValue = startRange.getMinimumLong();
        long maxValue = arriveRange.getMaximumLong();
        Graphics2D g2d = (Graphics2D) g;
        g2d.setPaint(new TexturePaint(STROKED, new Rectangle(0, 0, 3, 3)));
        g2d.fillRect(0, 0, getWidth(), getHeight());

        //draw frame around the entire range
        renderRange(new LongRange(startRange.getMinimumLong(), arriveRange.getMaximumLong()), startRange,
                arriveRange, false, false, g2d, null, popupInfo);
        g2d.setColor(Constants.DS_BACK);
        popupInfo.clear();
        //fill start range
        renderRange(startRange, startRange, arriveRange, true, false, g2d, null, popupInfo);
        //fill arrive range
        renderRange(arriveRange, startRange, arriveRange, false, true, g2d, null, popupInfo);
        Paint p = g2d.getPaint();
        Iterator<LongRange> rangeKeys = startRanges.keySet().iterator();
        while (rangeKeys.hasNext()) {
            LongRange currentRange = rangeKeys.next();

            TimeSpan spanForRange = startRanges.get(currentRange);
            if (spanForRange != null) {
                if (spanForRange.isValidAtEveryDay()) {
                    g2d.setPaint(new TexturePaint(DAILY_START_FRAME_FILL, new Rectangle(0, 0, 3, 3)));
                } else if (spanForRange.isValidAtExactTime()) {
                    g2d.setPaint(new TexturePaint(EXACT_START_FRAME_FILL, new Rectangle(0, 0, 3, 3)));
                } else {
                    g2d.setPaint(new TexturePaint(ONE_DAY_START_FRAME_FILL, new Rectangle(0, 0, 3, 3)));
                }
            }
            renderRange(currentRange, startRange, arriveRange, false, false, g2d, spanForRange, popupInfo);
        }

        Composite c = g2d.getComposite();
        rangeKeys = arriveRanges.keySet().iterator();
        while (rangeKeys.hasNext()) {
            LongRange currentRange = rangeKeys.next();
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f));
            g2d.setPaint(new TexturePaint(ARRIVE_FRAME_FILL, new Rectangle(0, 0, 3, 3)));
            TimeSpan spanForRange = arriveRanges.get(currentRange);
            renderRange(currentRange, startRange, arriveRange, false, false, g2d, spanForRange, popupInfo);
        }
        g2d.setComposite(c);
        g2d.setPaint(p);
        renderDayMarkers(minValue, maxValue, g2d);
        renderPopup(popupInfo, g2d);
    }
}

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);//from  w w  w  . j  a va2 s . co  m
    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();
    return img;
}

From source file:ShapeMover.java

public void update(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    Dimension dim = getSize();//from w w  w  . j  a  va2  s.com
    int w = (int) dim.getWidth();
    int h = (int) dim.getHeight();
    g2.setStroke(new BasicStroke(8.0f));

    if (isFirstTime) {
        area = new Rectangle(dim);
        rect.setLocation(w / 2 - 50, h / 2 - 25);
        isFirstTime = false;
    }

    // Clears the rectangle that was previously drawn.
    g2.setPaint(Color.white);
    g2.fillRect(0, 0, w, h);

    g2.setColor(Color.red);
    g2.draw(rect);
    g2.setColor(Color.black);
    g2.fill(rect);
}

From source file:Composite.java

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

    Dimension d = getSize();// w  w w.jav a 2s .  c om
    int w = d.width;
    int h = d.height;

    // Creates the buffered image.
    BufferedImage buffImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    Graphics2D gbi = buffImg.createGraphics();

    // Clears the previously drawn image.
    g2.setColor(Color.white);
    g2.fillRect(0, 0, d.width, d.height);

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

    // Draws the rectangle and ellipse into the buffered image.
    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));

    // Draws the buffered image.
    g2.drawImage(buffImg, null, 0, 0);
}

From source file:org.osjava.reportrunner_plugins.renderers.jfreechart.creators.TexturedBarRenderer.java

public java.awt.Paint getSeriesPaint(int row) {
    BufferedImage bufferedImage = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
    Graphics2D big = bufferedImage.createGraphics();
    TexturePaint texture = null;/*  www  .  ja  v  a2  s . c  o m*/
    int rowNum = row + 1;
    int patNum = 13;
    int formula = rowNum - ((rowNum / patNum) * patNum);

    if (formula == 0) {
        big.setColor(Color.orange);
        big.fillRect(0, 0, 5, 5);
        big.fillRect(1, 0, 5, 5);
        Rectangle r = new Rectangle(0, 0, 5, 5);
        texture = new TexturePaint(bufferedImage, r);

    } else if (formula == 1) {
        big.setColor(Color.red);
        big.fillRect(0, 0, 5, 5);
        big.fillRect(1, 0, 5, 5);
        Rectangle r = new Rectangle(0, 0, 5, 5);
        texture = new TexturePaint(bufferedImage, r);
    } else if (formula == 2) {
        Color color = Color.blue;
        big.setColor(Color.white);
        big.fillRect(0, 0, 5, 5);
        big.setColor(color);
        big.fillRect(0, 1, 5, 5);
        Rectangle r = new Rectangle(0, 0, 5, 5);

        texture = new TexturePaint(bufferedImage, r);
    } else if (formula == 3) {
        Color color = Color.yellow;
        big.setColor(Color.orange);
        big.fillRect(0, 0, 5, 5);
        big.setColor(color);
        big.fillRect(1, 1, 4, 4);
        Rectangle r = new Rectangle(0, 0, 5, 5);

        texture = new TexturePaint(bufferedImage, r);
    } else if (formula == 4) {
        big.setColor(Color.green);
        big.fillRect(0, 0, 5, 5);
        big.fillRect(1, 0, 5, 5);
        Rectangle r = new Rectangle(0, 0, 5, 5);
        texture = new TexturePaint(bufferedImage, r);
    } else if (formula == 5) {
        big.setColor(Color.magenta);
        big.fillRect(0, 0, 5, 5);
        big.setColor(Color.pink);
        big.fillRect(0, 0, 4, 4);

        Rectangle r = new Rectangle(0, 0, 5, 5);
        texture = new TexturePaint(bufferedImage, r);
    } else if (formula == 6) {
        float[] x = { .5f, 1.5f, 2.0f, 2.5f, 3.0f, 3.5f, 4.0f, 4.5f, 5.0f };
        float[] y = { .5f, 1.5f, 2.0f, 2.5f, 3.0f, 3.5f, 4.0f, 4.5f, 5.0f };
        GeneralPath path = new GeneralPath();
        path.moveTo(x[0], y[0]);
        for (int j = 1; j < x.length; j++) {
            path.lineTo(x[j], y[j]);
        }

        big.setColor(new Color(226, 199, 252));
        big.fillRect(0, 0, 5, 5);
        big.setColor(Color.blue);
        big.setStroke(new BasicStroke(1.0f));

        big.draw(path);
        Rectangle r = new Rectangle(0, 0, 5, 5);
        texture = new TexturePaint(bufferedImage, r);
    } else if (formula == 7) {
        big.setColor(Color.lightGray);
        big.fillRect(0, 0, 5, 5);
        big.setColor(Color.red);
        big.fillRect(1, 0, 5, 5);
        Rectangle r = new Rectangle(0, 0, 5, 5);
        texture = new TexturePaint(bufferedImage, r);
    } else if (formula == 8) {
        float[] x = { .5f, 1.5f, 2.0f, 2.5f, 3.0f, 3.5f, 4.0f, 4.5f, 5.0f };
        float[] y = { .5f, 1.5f, 2.0f, 2.5f, 3.0f, 3.5f, 4.0f, 4.5f, 5.0f };
        GeneralPath path = new GeneralPath();
        path.moveTo(x[0], y[0]);
        for (int j = 1; j < x.length; j++) {
            path.lineTo(x[j], y[j]);

        }
        big.setColor(Color.blue);
        big.fillRect(0, 0, 5, 5);
        big.setColor(Color.cyan);
        big.setStroke(new BasicStroke(2.0f));
        big.draw(path);
        Rectangle r = new Rectangle(0, 0, 5, 5);
        texture = new TexturePaint(bufferedImage, r);
    } else if (formula == 9) {
        Color color = new Color(0xBBBBDD);
        big.setColor(color);
        big.fillRect(0, 0, 5, 5);
        big.setColor(new Color(199, 201, 230));
        big.fillRect(1, 0, 5, 5);
        Rectangle r = new Rectangle(0, 0, 5, 5);
        texture = new TexturePaint(bufferedImage, r);
    } else if (formula == 10) {
        float[] x = { 1, 2, 3, 4, 5 };
        float[] y = { 1, 2, 3, 4, 5 };
        GeneralPath path = new GeneralPath();
        path.moveTo(x[0], y[1]);
        path.lineTo(x[1], y[0]);
        path.moveTo(x[0], y[2]);
        path.lineTo(x[2], y[0]);
        path.moveTo(x[0], y[3]);
        path.lineTo(x[3], y[0]);
        path.moveTo(x[0], y[4]);
        path.lineTo(x[4], y[0]);
        big.setColor(Color.red);
        big.fillRect(0, 0, 5, 5);
        big.setColor(new Color(242, 242, 193));
        big.setStroke(new BasicStroke(3.0f));
        big.draw(path);
        Rectangle r = new Rectangle(0, 0, 5, 5);
        texture = new TexturePaint(bufferedImage, r);
    } else if (formula == 11) {
        big.setColor(new Color(252, 169, 171));
        big.fillOval(0, 0, 5, 6);
        big.setColor(new Color(252, 230, 230));
        big.fillOval(0, 0, 3, 3);
        Rectangle r = new Rectangle(0, 0, 5, 5);
        texture = new TexturePaint(bufferedImage, r);
    } else if (formula == 12) {
        big.setColor(Color.green);
        big.fillRect(0, 0, 5, 5);
        big.setColor(new Color(20, 178, 38));
        big.fillRect(2, 2, 5, 5);
        Rectangle r = new Rectangle(0, 0, 5, 5);
        texture = new TexturePaint(bufferedImage, r);
    }
    return texture;

}