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:org.cruk.mga.CreateReport.java

/**
 * Draws the x-axis for the number of sequences and the legend.
 *
 * @param g2/*from   www  . j ava2  s .c o  m*/
 * @param x0
 * @param y
 * @param tickIntervals
 * @param maxSequenceCount
 * @return
 */
private int drawAxisAndLegend(Graphics2D g2, int x0, int y, int tickIntervals, long maxSequenceCount) {
    g2.setColor(Color.BLACK);
    g2.setFont(axisFont);

    boolean millions = maxSequenceCount / tickIntervals >= 1000000;
    long largestTickValue = maxSequenceCount;
    if (millions)
        largestTickValue /= 1000000;
    int w = g2.getFontMetrics().stringWidth(Long.toString(largestTickValue));
    int x1 = plotWidth - (w / 2) - gapSize;
    g2.drawLine(x0, y, x1, y);

    int tickFontHeight = g2.getFontMetrics().getAscent();
    int tickHeight = tickFontHeight / 2;
    for (int i = 0; i <= tickIntervals; i++) {
        int x = x0 + i * (x1 - x0) / tickIntervals;
        g2.drawLine(x, y, x, y + tickHeight);
        long tickValue = i * maxSequenceCount / tickIntervals;
        if (millions)
            tickValue /= 1000000;
        String s = Long.toString(tickValue);
        int xs = x - g2.getFontMetrics().stringWidth(s) / 2 + 1;
        int ys = y + tickHeight + tickFontHeight + 1;
        g2.drawString(s, xs, ys);
    }

    g2.setFont(font);
    int fontHeight = g2.getFontMetrics().getAscent();
    String s = "Number of sequences";
    if (millions)
        s += " (millions)";
    int xs = x0 + (x1 - x0 - g2.getFontMetrics().stringWidth(s)) / 2;
    int ys = y + tickHeight + tickFontHeight + fontHeight + fontHeight / 3;
    g2.drawString(s, xs, ys);

    int yl = ys + fontHeight * 2;
    int xl = x0;

    int barHeight = (int) (fontHeight * 0.7f);
    int barWidth = 3 * barHeight;
    int yb = yl + (int) (fontHeight * 0.3f);
    int gap = (int) (fontHeight * 0.4f);

    g2.setColor(Color.GREEN);
    g2.fillRect(xl, yb, barWidth, barHeight);
    g2.setColor(Color.BLACK);
    g2.drawRect(xl, yb, barWidth, barHeight);
    g2.setFont(axisFont);
    String label = "Sequenced species/genome";
    xl += barWidth + gap;
    g2.drawString(label, xl, yl + fontHeight);
    xl += g2.getFontMetrics().stringWidth(label) + gap * 3;

    g2.setColor(Color.ORANGE);
    g2.fillRect(xl, yb, barWidth, barHeight);
    g2.setColor(Color.BLACK);
    g2.drawRect(xl, yb, barWidth, barHeight);
    label = "Control";
    xl += barWidth + gap;
    g2.drawString(label, xl, yl + fontHeight);
    xl += g2.getFontMetrics().stringWidth(label) + gap * 3;

    g2.setColor(Color.RED);
    g2.fillRect(xl, yb, barWidth, barHeight);
    g2.setColor(Color.BLACK);
    g2.drawRect(xl, yb, barWidth, barHeight);
    label = "Contaminant";
    xl += barWidth + gap;
    g2.drawString(label, xl, yl + fontHeight);
    xl += g2.getFontMetrics().stringWidth(label) + gap * 3;

    g2.setColor(ADAPTER_COLOR);
    g2.fillRect(xl, yb, barWidth, barHeight);
    g2.setColor(Color.BLACK);
    g2.drawRect(xl, yb, barWidth, barHeight);
    label = "Adapter";
    xl += barWidth + gap;
    g2.drawString(label, xl, yl + fontHeight);
    xl += g2.getFontMetrics().stringWidth(label) + gap * 3;

    g2.setColor(Color.BLACK);
    g2.drawRect(xl, yb, barWidth, barHeight);
    label = "Unmapped";
    xl += barWidth + gap;
    g2.drawString(label, xl, yl + fontHeight);
    xl += g2.getFontMetrics().stringWidth(label) + gap * 3;

    g2.setColor(Color.GRAY);
    g2.fillRect(xl, yb, barWidth, barHeight);
    g2.setColor(Color.BLACK);
    g2.drawRect(xl, yb, barWidth, barHeight);
    label = "Unknown";
    xl += barWidth + gap;
    g2.drawString(label, xl, yl + fontHeight);

    return x1;
}

From source file:org.tsho.dmc2.core.chart.TrajectoryMultiRenderer.java

public void render(final Graphics2D g2, final Rectangle2D dataArea, final PlotRenderingInfo info) {

    ValueAxis domainAxis = plot.getDomainAxis();
    ValueAxis rangeAxis = plot.getRangeAxis();

    /* transients */
    if (!continua) {
        state = STATE_TRANSIENTS;//from  w w w . j  av  a2s .  c o m

        for (int i = 0; i < stepperList.length; i++) {
            stepperList[i].initialize();
            prevX[i] = 0;
            prevY[i] = 0;
        }

        for (index = 0; index < transients; index++) {
            for (int i = 0; i < stepperList.length; i++) {
                stepperList[i].step();
            }
            if (stopped) {
                state = STATE_STOPPED;
                return;
            }
        }

        //            for (int i = 0; i < stepperList.length; i++) {
        //                stepperList[i].initialize();
        //                for (index = 0; index < transients; index++) {
        //                    stepperList[i].step();
        //                    if (stopped) {
        //                        state = STATE_STOPPED;
        //                        return;
        //                    }
        //                }
        //                prevX[i] = 0;
        //                prevY[i] = 0;
        //            }

        index = 0;
    }

    state = STATE_POINTS;

    Stepper.Point2D point;
    int x, y;
    int start = index;
    int end = 0;
    if (index == 0) {
        end = start + iterations + 1;
    } else {
        end = start + iterations;
    }

    Stroke stroke = new BasicStroke(7f);
    Stroke origStroke = g2.getStroke();
    Color color = Color.BLACK;

    for (; index < end; index++) {

        for (int i = 0; i < stepperList.length; i++) {
            point = stepperList[i].getCurrentPoint2D();

            if (!timePlot) {
                x = (int) domainAxis.valueToJava2D(point.getX(), dataArea, RectangleEdge.BOTTOM);
            } else {
                x = (int) domainAxis.valueToJava2D(index + transients, dataArea, RectangleEdge.BOTTOM);
            }

            y = (int) rangeAxis.valueToJava2D(point.getY(), dataArea, RectangleEdge.LEFT);

            g2.setPaint(paintList[i]);

            if (connectWithLines) {
                if (index > 0) {
                    g2.drawLine(x, y, prevX[i], prevY[i]);
                }

                prevX[i] = x;
                prevY[i] = y;
            }

            if (bigDots) {
                g2.fillRect(x - 1, y - 1, 3, 3);
            } else {
                g2.fillRect(x, y, 1, 1);
            }

            stepperList[i].step();
        }

        if (stopped) {
            state = STATE_STOPPED;
            return;
        }

        if (delay > 0) {
            try {
                Thread.sleep(delay);
            } catch (final InterruptedException e) {
            } finally {
            }
        }

    }
    state = STATE_FINISHED;
}

From source file:com.flexoodb.common.FlexUtils.java

static public void createTextBanner(String text, int fontsize, int width, int height, int x, int y,
        String outputfile) throws Exception {
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    img.createGraphics();//from w  ww  .j av  a 2 s  . com
    Graphics2D g = (Graphics2D) img.getGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, img.getWidth(), img.getHeight());
    Font font = new Font("Arial", Font.BOLD, fontsize);
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
    g.setFont(font);
    g.setColor(Color.BLUE.darker());
    g.drawString(text, x, y);
    ImageIO.write(img, "png", new File(outputfile));
}

From source file:org.tsho.dmc2.core.chart.CyclesRenderer.java

public void render(final Graphics2D g2, final Rectangle2D dataArea, final PlotRenderingInfo info) {
    ValueAxis domainAxis = plot.getDomainAxis();
    ValueAxis rangeAxis = plot.getRangeAxis();

    state = STATE_RUNNING;//from   ww w .  j  a v a 2s . c o m

    g2.setColor(Color.BLACK);

    VariableDoubles initialValues = map.getVariables();
    int xLabelIndex = VariableDoubles.indexOf(initialValues, xLabel);
    int yLabelIndex = VariableDoubles.indexOf(initialValues, yLabel);

    int x, y;
    String varNames[] = map.getVarNames();
    int dim = map.getNVar();
    double tmp[] = new double[dim];
    double tmp1[] = new double[dim];
    double vRangeMax[] = VariableDoubles.toArray(rangeMax);
    double vRangeMin[] = VariableDoubles.toArray(rangeMin);
    double[] cycle;
    double[] cycleModulus = new double[dim];

    double[][] list = new double[period][dim];

    int counter = 0;
    main: while (counter < maxPoints) {

        if (stopped) {
            state = STATE_STOPPED;
            return;
        }

        for (int iii = 0; iii < dim; iii++) {
            double val = (vRangeMax[iii] - vRangeMin[iii]) * Math.random();
            initialValues.put(varNames[iii], vRangeMin[iii] + val);
        }
        counter++;

        try {
            cycle = Lua.findCycles(map, parameters, period, initialValues, epsilon, 100, cycleModulus);
        } catch (AlgorithmFailedException e) {
            continue;
        }

        if (period > 1) {
            System.arraycopy(cycle, 0, list[0], 0, dim);

            for (int i = 0; i < period - 1; i++) { /*fill cycle data*/
                map.evaluate(VariableDoubles.toArray(parameters), list[i], list[i + 1]);
            }

            // compare i + 1 with all previous
            for (int j = 0; j < period; j++) {
                for (int i = j + 1; i < period; i++) {
                    if (equal(list[j], list[i], epsilon)) {
                        continue main;
                    }
                }
            }

            // check if it's a real solution
            map.evaluate(VariableDoubles.toArray(parameters), list[period - 1], tmp);
            if (!equal(tmp, list[0], epsilon))
                continue main;

            checkStability(cycleModulus, g2);

            for (int i = 0; i < period; i++) {
                x = (int) domainAxis.valueToJava2D(list[i][xLabelIndex], dataArea, RectangleEdge.BOTTOM);

                y = (int) rangeAxis.valueToJava2D(list[i][yLabelIndex], dataArea, RectangleEdge.LEFT);
                if (bigDots) {
                    g2.fillRect(x - 1, y - 1, 3, 3);
                } else {
                    g2.fillRect(x, y, 1, 1);
                }
            }
        } else { // period == 1
            x = (int) domainAxis.valueToJava2D(cycle[xLabelIndex], dataArea, RectangleEdge.BOTTOM);

            y = (int) rangeAxis.valueToJava2D(cycle[yLabelIndex], dataArea, RectangleEdge.LEFT);

            // check if it's a real solution
            map.evaluate(VariableDoubles.toArray(parameters), cycle, tmp);
            if (!equal(tmp, cycle, epsilon))
                continue main;

            checkStability(cycleModulus, g2);

            if (bigDots) {
                g2.fillRect(x - 1, y - 1, 3, 3);
            } else {
                g2.fillRect(x, y, 1, 1);
            }
        }

    }

    state = STATE_FINISHED;
}

From source file:com.flexoodb.common.FlexUtils.java

static public BufferedImage createCaptcha(String text, Color background, Color fontcolor, int fontsize,
        int width, int height, int x, int y) throws Exception {
    BufferedImage img = new BufferedImage(width + 10, height, BufferedImage.TYPE_INT_RGB);
    img.createGraphics();//from   w  w  w.  ja v a  2 s.  co m
    Graphics2D g = (Graphics2D) img.getGraphics();
    g.setColor(background);
    g.fillRect(0, 0, img.getWidth(), img.getHeight());
    Font font = new Font("Monospaced", Font.BOLD + Font.ITALIC, fontsize);

    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
    g.setFont(font);
    g.setColor(fontcolor);

    char[] c = text.toCharArray();

    java.util.Random random = new java.util.Random();

    for (int i = 0; i < c.length; i++) {
        int j = random.nextInt(9);
        if (j > 5) {
            j = j - 5;
        } else {
            j = -1 * (j - 2);
        }

        char[] c1 = new char[1];
        c1[0] = c[i];
        g.drawChars(c1, 0, 1, x + (i * 16), y + j);
    }

    for (int i = -10; i < 20; i++) {
        int j = random.nextInt(8);
        g.drawOval((i + j) - 20, ((i * 8) - (j > 3 ? 4 : -4)), width + 20, height);
    }
    //ImageIO.write(img,"png",new File(outputfile));
    return img;

}

From source file:org.tsho.dmc2.core.chart.LyapunovRenderer.java

public boolean renderVsTime(Graphics2D g2, Rectangle2D dataArea) {

    final int imageWidth = (int) dataArea.getWidth();

    g2.setPaint(Color.red);//from  w ww . j a  va2  s  .co  m

    final int timeStep;
    if (Math.abs(upper - lower) >= imageWidth) {
        timeStep = ((int) upper - (int) lower) / imageWidth;
    } else {
        timeStep = 1;
    }

    int prevX[] = new int[model.getNVar()];
    int prevY[] = new int[model.getNVar()];

    //? 14.7.2004
    //LyapunovExpCalc lyapunovExpCalc;
    LuaLyapExp integrator = new LuaLyapExp(model, parameters, initialPoint);
    String colNames[] = new String[model.getNVar() + 1];
    colNames[0] = "time";
    for (int j = 1; j < model.getNVar() + 1; j++)
        colNames[j] = "" + (new Integer(j));
    Dataset dataset = new Dataset(colNames);
    lyapunovComponent.setDataobject(dataset);
    double tmpRow[];
    if (model instanceof ODE) {
        double step = stepSize; // stepSize and timeStep probably mean the same thing, one for discrete another for ODE
        //checking trajectory

        for (int i = 0; i < (int) (upper / stepSize); i++) {
            double[] result = new double[model.getNVar()];
            int transX, transY;

            double[] temp = integrator.evaluateODEStep(step);
            double tt = integrator.getTime();
            for (int h = 0; h < temp.length; h++) {
                result[h] = temp[h] / tt;
            }
            tmpRow = new double[model.getNVar() + 1];
            tmpRow[0] = i;
            for (int a1 = 1; a1 < tmpRow.length; a1++)
                tmpRow[a1] = result[a1 - 1];
            try {
                dataset.addRow(tmpRow);
            } catch (DatasetException de) {
                System.out.println("" + de);
            }

            for (int j = 0; j < result.length; j++) {
                if (Double.isNaN(result[j]))
                    break;
                transX = (int) plot.getDomainAxis().valueToJava2D(i * stepSize, dataArea, RectangleEdge.BOTTOM);
                transY = (int) plot.getRangeAxis().valueToJava2D(result[j], dataArea, RectangleEdge.LEFT);

                g2.setPaint(paintSequence[j]);

                if (bigDots) {
                    g2.fillRect(transX - 1, transY - 1, 3, 3);

                } else {
                    g2.fillRect(transX, transY, 1, 1);
                }

                if (connectWithLines) {
                    if (i > (int) lower) {
                        g2.drawLine(transX, transY, prevX[j], prevY[j]);
                    }

                    prevX[j] = transX;
                    prevY[j] = transY;
                }
            }

            if (stopped == true) {
                return false;
            }
        }

    } else {//? 14.7.2004 section of code above
        for (int i = (int) lower; i < (int) upper; i += timeStep) {
            double[] result = new double[model.getNVar()];
            int transX, transY;

            result = Lua.evaluateLyapunovExponents(model, parameters, initialPoint, i);
            tmpRow = new double[model.getNVar() + 1];
            tmpRow[0] = i;
            for (int a1 = 1; a1 < tmpRow.length; a1++)
                tmpRow[a1] = result[a1 - 1];
            try {
                dataset.addRow(tmpRow);
            } catch (DatasetException de) {
                System.out.println("" + de);
            }

            for (int j = 0; j < result.length; j++) {
                if (Double.isNaN(result[j]))
                    break;

                transX = (int) plot.getDomainAxis().valueToJava2D(i, dataArea, RectangleEdge.BOTTOM);
                transY = (int) plot.getRangeAxis().valueToJava2D(result[j], dataArea, RectangleEdge.LEFT);

                g2.setPaint(paintSequence[j]);

                if (bigDots) {
                    g2.fillRect(transX - 1, transY - 1, 3, 3);

                } else {
                    g2.fillRect(transX, transY, 1, 1);
                }

                if (connectWithLines) {
                    if (i > (int) lower) {
                        g2.drawLine(transX, transY, prevX[j], prevY[j]);
                    }

                    prevX[j] = transX;
                    prevY[j] = transY;
                }
            }

            if (stopped == true) {
                return false;
            }
        }
    } //? } belongs to else 14.7.2004

    return true;
}

From source file:de.tor.tribes.ui.panels.MinimapPanel.java

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    try {/*from w ww .j a va 2  s  .c o m*/
        Graphics2D g2d = (Graphics2D) g;
        g2d.clearRect(0, 0, getWidth(), getHeight());
        g2d.drawImage(mBuffer, 0, 0, null);

        if (iCurrentView == ID_MINIMAP) {
            g2d.setColor(Color.YELLOW);

            int mapWidth = rVisiblePart.width;
            int mapHeight = rVisiblePart.height;

            int w = (int) Math.rint(((double) getWidth() / mapWidth) * (double) iWidth);
            int h = (int) Math.rint(((double) getHeight() / mapHeight) * (double) iHeight);

            double posX = ((double) getWidth() / mapWidth * (double) (iX - rVisiblePart.x)) - w / 2;
            double posY = ((double) getHeight() / mapHeight * (double) (iY - rVisiblePart.y)) - h / 2;

            g2d.drawRect((int) Math.rint(posX), (int) Math.rint(posY), w, h);

            if (iCurrentCursor == ImageManager.CURSOR_SHOT) {
                if (rDrag != null) {
                    g2d.setColor(Color.ORANGE);
                    g2d.drawRect((int) rDrag.getMinX(), (int) rDrag.getMinY(),
                            (int) (rDrag.getWidth() - rDrag.getX()), (int) (rDrag.getHeight() - rDrag.getY()));
                }
            } else if (iCurrentCursor == ImageManager.CURSOR_ZOOM) {
                if (rDrag != null) {
                    g2d.setColor(Color.CYAN);
                    g2d.drawRect((int) rDrag.getX(), (int) rDrag.getY(),
                            (int) (rDrag.getWidth() - rDrag.getX()),
                            (int) ((rDrag.getWidth() - rDrag.getX()) * ((double) getHeight()) / getWidth()));
                }
            }
        }

        if (showControls) {
            //g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .2f));
            Rectangle r = minimapButtons.get(ID_MINIMAP);
            g2d.setColor(Color.WHITE);
            Point menuPos = r.getLocation();
            menuPos.translate(-2, -2);
            //draw border
            g2d.fillRect(menuPos.x, menuPos.y, 88, 30);
            g2d.setColor(Color.BLACK);
            //check if mouse is inside minimap button
            if (getMousePosition() != null && r.contains(getMousePosition())) {
                g2d.setColor(Color.YELLOW);
                g2d.fillRect(r.x, r.y, r.width, r.height);
                g2d.setColor(Color.BLACK);
            }
            g2d.drawImage(minimapIcons.get(ID_MINIMAP), r.x, r.y, null);
            g2d.drawRect(r.x, r.y, r.width, r.height);

            r = minimapButtons.get(ID_ALLY_CHART);
            //check if mouse is inside ally chart button
            if (getMousePosition() != null && r.contains(getMousePosition())) {
                g2d.setColor(Color.YELLOW);
                g2d.fillRect(r.x, r.y, r.width, r.height);
                g2d.setColor(Color.BLACK);
            }
            g2d.drawImage(minimapIcons.get(ID_ALLY_CHART), r.x, r.y, null);
            g2d.drawRect(r.x, r.y, r.width, r.height);

            r = minimapButtons.get(ID_TRIBE_CHART);
            //check if mouse is inside tribe chart button
            if (getMousePosition() != null && r.contains(getMousePosition())) {
                g2d.setColor(Color.YELLOW);
                g2d.fillRect(r.x, r.y, r.width, r.height);
                g2d.setColor(Color.BLACK);

            }
            g2d.drawImage(minimapIcons.get(ID_TRIBE_CHART), r.x, r.y, null);
            g2d.drawRect(r.x, r.y, r.width, r.height);
        }
        g2d.dispose();
    } catch (Exception e) {
        logger.error("Failed painting Minimap", e);
    }
}

From source file:it.unibo.alchemist.boundary.gui.effects.DrawShape.java

@SuppressFBWarnings("ES_COMPARING_STRINGS_WITH_EQ")
@Override/* w w  w  .  j ava2s .c  o m*/
public void apply(final Graphics2D g, final Node<?> n, final int x, final int y) {
    if (molString != molStringCached // NOPMD: pointer comparison is wanted here
            || incarnation == null || curIncarnation != prevIncarnation) { // NOPMD: pointer comparison is wanted here
        molStringCached = molString;
        prevIncarnation = curIncarnation;
        incarnation = SupportedIncarnations.get(curIncarnation.getCurrent()).get();
        /*
         * Process in a separate thread: if it fails, does not kill EDT.
         */
        final Thread th = new Thread(() -> molecule = incarnation.createMolecule(molString));
        th.start();
        try {
            th.join();
        } catch (final InterruptedException e) {
            L.error("Bug.", e);
        }
    }
    if (!molFilter || (molecule != null && n.contains(molecule))) {
        final double ks = (scaleFactor.getVal() - MIN_SCALE) * 2 / (double) (SCALE_DIFF);
        final int sizex = size.getVal();
        final int startx = x - sizex / 2;
        final int sizey = (int) Math.ceil(sizex * ks);
        final int starty = y - sizey / 2;
        final Color toRestore = g.getColor();
        colorCache = new Color(red.getVal(), green.getVal(), blue.getVal(), alpha.getVal());
        Color newcolor = colorCache;
        if (molPropertyFilter && molecule != null) {
            final int minV = (int) (minprop.getVal() * FastMath.pow(PROPERTY_SCALE, propoom.getVal()));
            final int maxV = (int) (maxprop.getVal() * FastMath.pow(PROPERTY_SCALE, propoom.getVal()));
            if (minV < maxV) {
                @SuppressWarnings({ "rawtypes", "unchecked" })
                double propval = incarnation.getProperty((Node) n, molecule, property);
                if (isWritingPropertyValue()) {
                    g.setColor(colorCache);
                    g.drawString(Double.toString(propval), startx + sizex, starty + sizey);
                }
                propval = Math.min(Math.max(propval, minV), maxV);
                propval = (propval - minV) / (maxV - minV);
                if (reverse) {
                    propval = 1f - propval;
                }
                newcolor = c.alter(newcolor, (float) propval);
            }
        }
        g.setColor(newcolor);
        switch (mode) {
        case FillEllipse:
            g.fillOval(startx, starty, sizex, sizey);
            break;
        case DrawEllipse:
            g.drawOval(startx, starty, sizex, sizey);
            break;
        case DrawRectangle:
            g.drawRect(startx, starty, sizex, sizey);
            break;
        case FillRectangle:
            g.fillRect(startx, starty, sizex, sizey);
            break;
        default:
            g.fillOval(startx, starty, sizex, sizey);
        }
        g.setColor(toRestore);
    }
}

From source file:org.cruk.mga.CreateReport.java

/**
 * Draws the labels for each dataset ID, returning the x coordinate for
 * subsequent drawing for each row.//  ww  w  .  j  a  v a  2  s .c  o  m
 *
 * @param g2
 * @param offset
 * @param separation
 * @param multiGenomeAlignmentSummaries
 * @return
 */
private int drawLabels(Graphics2D g2, int offset, int separation,
        Collection<MultiGenomeAlignmentSummary> multiGenomeAlignmentSummaries) {
    int n = multiGenomeAlignmentSummaries.size();
    boolean drawNumbers = false;
    if (n > 1) {
        int i = 0;
        for (MultiGenomeAlignmentSummary multiGenomeAlignmentSummary : multiGenomeAlignmentSummaries) {
            i++;
            String datasetId = multiGenomeAlignmentSummary.getDatasetId();
            String datasetDisplayLabel = datasetDisplayLabels.get(datasetId);
            if (!Integer.toString(i).equals(datasetDisplayLabel)) {
                drawNumbers = true;
                break;
            }
        }
    }
    int x = gapSize;
    int y = offset;
    int maxWidth = 0;
    if (drawNumbers) {
        for (int i = 1; i <= n; i++) {
            String s = Integer.toString(i) + ".";
            g2.drawString(s, x, y);
            maxWidth = Math.max(maxWidth, g2.getFontMetrics().stringWidth(s));
            y += separation;
        }
        x += maxWidth + gapSize / 2;
    }
    y = offset;
    maxWidth = 0;
    for (MultiGenomeAlignmentSummary multiGenomeAlignmentSummary : multiGenomeAlignmentSummaries) {
        String datasetId = multiGenomeAlignmentSummary.getDatasetId();
        String datasetDisplayLabel = datasetDisplayLabels.get(datasetId);
        g2.drawString(datasetDisplayLabel, x, y);
        maxWidth = Math.max(maxWidth, g2.getFontMetrics().stringWidth(datasetDisplayLabel));
        y += separation;
    }
    int acceptableWidth = (int) (0.15 * plotWidth);
    if (maxWidth > acceptableWidth) {
        Composite origComposite = g2.getComposite();
        y = offset - g2.getFontMetrics().getHeight() - separation / 4;
        for (int i = 0; i < n; i++) {
            g2.setColor(Color.WHITE);
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
            g2.fillRect(x + acceptableWidth, y, gapSize, separation);
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
            g2.fillRect(x + acceptableWidth + gapSize, y, plotWidth - x - acceptableWidth - gapSize,
                    separation);
            y += separation;
        }
        maxWidth = acceptableWidth;
        g2.setComposite(origComposite);
    }
    return x + maxWidth + gapSize;
}

From source file:com.aerohive.nms.web.config.lbs.services.HmFolderServiceImpl.java

private BufferedImage createFloorImage(HmFolder floor, double scale, int floorWidth, int floorHeight,
        Map<Long, Integer> channelMap, Map<Long, Integer> colorMap, int borderX, int borderY, double gridSize)
        throws Exception {
    BufferedImage image = new BufferedImage(floorWidth + borderX + 1, floorHeight + borderY + 1,
            BufferedImage.TYPE_INT_ARGB);
    if (floor == null) {
        return image;
    }/*from w w  w . j a v  a 2s . co  m*/
    double metricWidth = 0.0, metricHeight = 0.0, offsetX = 0.0, offsetY = 0.0;
    int imageWidth = 0;
    LengthUnit lengthUnit = LengthUnit.METERS;

    if (null != floor.getMetricWidth()) {
        metricWidth = floor.getMetricWidth().doubleValue();
    }
    if (null != floor.getMetricHeight()) {
        metricHeight = floor.getMetricHeight().doubleValue();
    }
    if (null != floor.getImageWidth()) {
        imageWidth = floor.getImageWidth().intValue();
    }
    if (null != floor.getOffsetX()) {
        offsetX = floor.getOffsetX().doubleValue();
    }
    if (null != floor.getOffsetY()) {
        offsetY = floor.getOffsetY().doubleValue();
    }
    if (null != floor.getLengthUnit()) {
        lengthUnit = floor.getLengthUnit();
    }

    Graphics2D g2 = image.createGraphics();
    g2.setStroke(new BasicStroke(1));
    if (getDistanceMetric(metricWidth, lengthUnit) == 0) {
        g2.setColor(new Color(255, 255, 255));
        g2.fillRect(borderX, 0, floorWidth + 1, borderY);
        g2.fillRect(0, 0, borderX, borderY + floorHeight + 1);
        g2.setColor(new Color(120, 120, 120));
        g2.drawLine(0, borderY, floorWidth + borderX, borderY);
        g2.drawLine(borderX, 0, borderX, floorHeight + borderY);
        g2.setColor(new Color(255, 255, 204));
        g2.fillRect(borderX + 2, borderY + 2, 162, 25);
        g2.setColor(new Color(0, 51, 102));
        g2.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 12));
        g2.drawString("Please size this floor plan.", borderX + 8, borderY + 19);
        return image;
    }
    double screenWidth = scale * getDistanceMetric(metricWidth, lengthUnit);
    double imageScale = screenWidth / imageWidth;
    int originX = (int) (getDistanceMetric(offsetX, lengthUnit) * scale);
    int originY = (int) (getDistanceMetric(offsetY, lengthUnit) * scale);
    g2.setColor(new Color(255, 255, 255));
    if (floor.getBackground() != null && floor.getBackground().length() > 0) {
        LinkedMultiValueMap<String, String> metadata = new LinkedMultiValueMap<>();
        String url = getImageBaseUrl(floor.getOwnerId()) + floor.getBackground();
        try {
            BufferedImage map = ImageIO.read(clientFileService.getFile(url, "AFS_TOKEN", metadata));
            AffineTransform transform = new AffineTransform();
            transform.scale(imageScale, imageScale);
            g2.drawImage(map, new AffineTransformOp(transform, null), getFloorX(0, borderX, originX),
                    getFloorY(0, borderY, originY));
        } catch (Exception e) {
            logger.error(String.format("image file not found with url: %s", url));
            double screenHeight = scale * getDistanceMetric(metricHeight, lengthUnit);
            g2.fillRect(getFloorX(0, borderX, originX), getFloorY(0, borderY, originY), (int) screenWidth,
                    (int) screenHeight);
        }
    } else {
        double screenHeight = scale * getDistanceMetric(metricHeight, lengthUnit);
        g2.fillRect(getFloorX(0, borderX, originX), getFloorY(0, borderY, originY), (int) screenWidth,
                (int) screenHeight);
    }
    g2.setColor(new Color(204, 204, 204));
    // Right edge border
    g2.drawLine(borderX + floorWidth, borderY + 1, borderX + floorWidth, borderY + floorHeight);
    // Left edge border (right of tick marks)
    g2.drawLine(borderX + 1, borderY + floorHeight, borderX + floorWidth, borderY + floorHeight);
    g2.setColor(new Color(255, 255, 255));
    g2.fillRect(borderX, 0, floorWidth + 1, borderY);
    g2.fillRect(0, 0, borderX, borderY + floorHeight + 1);
    g2.setColor(new Color(120, 120, 120));
    g2.drawLine(0, borderY, floorWidth + borderX, borderY);
    g2.drawLine(borderX, 0, borderX, floorHeight + borderY);

    Font font = new Font(Font.SANS_SERIF, Font.BOLD, 12);
    double actualWidth = floorWidth / scale;
    double actualHeight = floorHeight / scale;
    String firstLabel;
    double unitScale = scale;
    if (LengthUnit.FEET == lengthUnit) {
        firstLabel = "0 feet";
        actualWidth /= HmFolder.FEET_TO_METERS;
        actualHeight /= HmFolder.FEET_TO_METERS;
        unitScale *= HmFolder.FEET_TO_METERS;
    } else {
        firstLabel = "0 meters";
    }
    g2.drawString(firstLabel, borderX + 4, 12);
    double gridX = gridSize;
    while (gridX < actualWidth) {
        int x = (int) (gridX * unitScale) + borderX;
        g2.drawLine(x, 0, x, borderY);
        boolean label = true;
        if (gridX + gridSize >= actualWidth) {
            // Last mark
            if (x + getNumberPixelWidth(gridX) + 2 > floorWidth) {
                label = false;
            }
        }
        if (label) {
            g2.drawString("" + (int) gridX, x + 4, 12);
        }
        gridX += gridSize;
    }

    double gridY = 0;
    while (gridY < actualHeight) {
        int y = (int) (gridY * unitScale) + borderY;
        g2.drawLine(0, y, borderX, y);
        double lx = gridY;
        int dx = 1;
        for (int bx = borderX; bx >= 16; bx -= 7) {
            if (lx < 10) {
                dx += 7;
            } else {
                lx /= 10;
            }
        }
        boolean label = true;
        if (gridY + gridSize >= actualHeight) {
            // Last mark
            if (y - borderY + 13 > floorHeight) {
                label = false;
            }
        }
        if (label) {
            g2.drawString("" + (int) gridY, dx, y + 13);
        }
        gridY += gridSize;
    }

    double mapToImage = getMapToMetric(metricWidth, imageWidth, lengthUnit) * scale;
    if (floor.getPerimeter().size() > 0) {
        g2.setStroke(new BasicStroke(2));
        g2.setColor(new Color(2, 159, 245));
        int[] xPoints = new int[floor.getPerimeter().size()];
        int[] yPoints = new int[floor.getPerimeter().size()];
        int nPoints = 0;
        int perimId = floor.getPerimeter().get(0).getId();
        for (int i = 0; i < floor.getPerimeter().size(); i++) {
            HmVertex vertex = floor.getPerimeter().get(i);
            if (vertex.getId() != perimId) {
                g2.drawPolygon(xPoints, yPoints, nPoints);
                nPoints = 0;
                perimId = vertex.getId();
            }
            xPoints[nPoints] = getFloorX((int) (vertex.getX() * mapToImage), borderX, originX);
            yPoints[nPoints++] = getFloorY((int) (vertex.getY() * mapToImage), borderY, originY);
        }
        g2.drawPolygon(xPoints, yPoints, nPoints);
    }

    g2.setStroke(new BasicStroke(1));
    g2.setColor(new Color(0, 170, 0));
    g2.setFont(font.deriveFont(Font.BOLD, 11));

    List<HmDeviceLocationEx> devices = deviceLocationExRep.findAllHmDevices(floor.getId());
    if (null != devices && !devices.isEmpty()) {
        for (HmDeviceLocationEx device : devices) {
            double x = device.getX() * mapToImage;
            double y = device.getY() * mapToImage;
            createNodeImage(device.getId(), channelMap, colorMap, getFloorX((int) x, borderX, originX),
                    getFloorY((int) y, borderY, originY), g2);
        }
    } else {
        List<HmDevicePlanningEx> plannedDevices = devicePlanningExRep.findAllPlannedDevices(floor.getId());
        for (HmDevicePlanningEx plannedDevice : plannedDevices) {
            double x = plannedDevice.getX() * mapToImage;
            double y = plannedDevice.getY() * mapToImage;
            createNodeImage(plannedDevice.getId(), channelMap, colorMap, getFloorX((int) x, borderX, originX),
                    getFloorY((int) y, borderY, originY), g2);
        }
    }
    return image;
}