Example usage for java.awt Graphics2D drawString

List of usage examples for java.awt Graphics2D drawString

Introduction

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

Prototype

public abstract void drawString(AttributedCharacterIterator iterator, float x, float y);

Source Link

Document

Renders the text of the specified iterator applying its attributes in accordance with the specification of the TextAttribute class.

Usage

From source file:be.fedict.eidviewer.gui.printing.IDPrintout.java

public int print(Graphics graphics, PageFormat pageFormat, int pageNumber) throws PrinterException {
    // we only support printing all in one single page
    if (pageNumber > 0)
        return Printable.NO_SUCH_PAGE;

    logger.finest(new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
            .append("width", pageFormat.getWidth()).append("height", pageFormat.getHeight())
            .append("imageableWidth", pageFormat.getImageableWidth())
            .append("imageableHeight", pageFormat.getImageableHeight())
            .append("imageableX", pageFormat.getImageableX()).append("imageableY", pageFormat.getImageableY())
            .append("orientation", pageFormat.getOrientation())
            .append("paper.width", pageFormat.getPaper().getWidth())
            .append("paper.height", pageFormat.getPaper().getHeight())
            .append("paper.imageableWidth", pageFormat.getPaper().getImageableWidth())
            .append("paper.imageableHeight", pageFormat.getPaper().getImageableHeight())
            .append("paper.imageableX", pageFormat.getPaper().getImageableX())
            .append("paper.imageableY", pageFormat.getPaper().getImageableY()).toString());

    logger.finest(new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
            .append("clip.width", graphics.getClipBounds().width)
            .append("clip.height", graphics.getClipBounds().height).append("clip.x", graphics.getClipBounds().x)
            .append("clip.y", graphics.getClipBounds().y).toString());

    // translate graphics2D with origin at top left first imageable location
    Graphics2D graphics2D = (Graphics2D) graphics;
    graphics2D.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

    // keep imageable width and height as variables for clarity (we use them often)
    float imageableWidth = (float) pageFormat.getImageableWidth();
    float imageableHeight = (float) pageFormat.getImageableHeight();

    // Coat of Arms images are stored at approx 36 DPI, scale 1/2 to get to Java default of 72DPI
    AffineTransform coatOfArmsTransform = new AffineTransform();
    coatOfArmsTransform.scale(0.5, 0.5);

    // photo images are stored at approx 36 DPI, scale 1/2 to get to Java default of 72DPI
    AffineTransform photoTransform = new AffineTransform();
    photoTransform.scale(0.5, 0.5);//  w  ww. jav  a 2  s  .  co  m
    photoTransform.translate((imageableWidth * 2) - (photo.getWidth(this)), 0);

    // make sure foreground is black, and draw coat of Arms and photo at the top of the page
    // using the transforms to scale them to 72DPI.
    graphics2D.setColor(Color.BLACK);
    graphics2D.drawImage(coatOfArms, coatOfArmsTransform, null);
    graphics2D.drawImage(photo, photoTransform, null);

    // calculate some sizes that need to take into account the scaling of the graphics, to avoid dragging
    // those non-intuitive "/2" further along in the code.
    float headerHeight = (float) (coatOfArms.getHeight(this)) / 2;
    float coatOfArmsWidth = (float) (coatOfArms.getWidth(this)) / 2;
    float photoWidth = (float) (photo.getWidth(this)) / 2;
    float headerSpaceBetweenImages = imageableWidth
            - (coatOfArmsWidth + photoWidth + (SPACE_BETWEEN_ITEMS * 2));

    logger.finest(new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("headerHeight", headerHeight)
            .append("coatOfArmsWidth", coatOfArmsWidth).append("photoWidth", photoWidth)
            .append("headerSpaceBetweenImages", headerSpaceBetweenImages).toString());

    // get localised strings for card type. We'll take a new line every time a ";" is found in the resource
    String[] cardTypeStr = (bundle.getString("type_" + this.identity.getDocumentType().toString())
            .toUpperCase()).split(";");

    // if a "mention" is present, append it so it appears below the card type string, between brackets

    if (identity.getSpecialOrganisation() != SpecialOrganisation.UNSPECIFIED) {
        String mention = TextFormatHelper.getSpecialOrganisationString(bundle,
                identity.getSpecialOrganisation());
        if (mention != null && !mention.isEmpty()) {
            String[] cardTypeWithMention = new String[cardTypeStr.length + 1];
            System.arraycopy(cardTypeStr, 0, cardTypeWithMention, 0, cardTypeStr.length);
            cardTypeWithMention[cardTypeStr.length] = "(" + mention + ")";
            cardTypeStr = cardTypeWithMention;
        }
    }

    // iterate from MAXIMAL_FONT_SIZE, calculating how much space would be required to fit the card type strings
    // stop when a font size is found where they all fit the space between the graphics in an orderly manner
    boolean sizeFound = false;
    int fontSize;
    for (fontSize = TITLE_MAXIMAL_FONT_SIZE; (fontSize >= MINIMAL_FONT_SIZE) && (!sizeFound); fontSize--) // count down slowly until we find one that fits nicely
    {
        logger.log(Level.FINE, "fontSize=" + fontSize + " sizeFound=" + sizeFound);
        graphics2D.setFont(new Font(FONT, Font.PLAIN, fontSize));
        sizeFound = (ImageUtilities.getTotalStringWidth(graphics2D, cardTypeStr) < headerSpaceBetweenImages)
                && (ImageUtilities.getTotalStringHeight(graphics2D, cardTypeStr) < headerHeight);
    }

    // unless with extremely small papers, a size should always have been found.
    // draw the card type strings, centered, between the images at the top of the page
    if (sizeFound) {
        graphics2D.setFont(new Font(FONT, Font.PLAIN, fontSize + 1));
        float cardTypeHeight = cardTypeStr.length * ImageUtilities.getStringHeight(graphics2D);
        float cardTypeBaseLine = ((headerHeight - cardTypeHeight) / 2) + ImageUtilities.getAscent(graphics2D);
        float cardTypeLineHeight = ImageUtilities.getStringHeight(graphics2D);

        logger.finest(new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                .append("cardTypeHeight", cardTypeHeight).append("cardTypeBaseLine", cardTypeBaseLine)
                .append("cardTypeLineHeight", cardTypeLineHeight).toString());

        for (int i = 0; i < cardTypeStr.length; i++) {
            float left = (coatOfArmsWidth + SPACE_BETWEEN_ITEMS
                    + (headerSpaceBetweenImages - ImageUtilities.getStringWidth(graphics2D, cardTypeStr[i]))
                            / 2);
            float leading = (float) cardTypeLineHeight * i;
            graphics2D.drawString(cardTypeStr[i], left, cardTypeBaseLine + leading);
        }
    }

    // populate idAttributes with all the information from identity and address
    // as well as date printed and some separators
    List<IdentityAttribute> idAttributes = populateAttributeList();

    // draw a horizontal line just below the header (images + card type titles)
    graphics2D.drawLine(0, (int) headerHeight, (int) imageableWidth, (int) headerHeight);

    // calculate how much space is left between the header and the bottom of the imageable area

    headerHeight += 32; // take some distance from header

    float imageableDataHeight = imageableHeight - headerHeight;
    float totalDataWidth = 0, totalDataHeight = 0;
    float labelWidth, widestLabelWidth = 0;
    float valueWidth, widestValueWidth = 0;

    // iterate from MAXIMAL_FONT_SIZE, calculating how much space would be required to fit the information in idAttributes into
    // the space between the header and the bottom of the imageable area
    // stop when a font size is found where it all fits in an orderly manner
    sizeFound = false;
    for (fontSize = MAXIMAL_FONT_SIZE; (fontSize >= MINIMAL_FONT_SIZE) && (!sizeFound); fontSize--) // count down slowly until we find one that fits nicely
    {
        logger.log(Level.FINE, "fontSize=" + fontSize + " sizeFound=" + sizeFound);
        graphics2D.setFont(new Font(FONT, Font.PLAIN, fontSize));

        widestLabelWidth = 0;
        widestValueWidth = 0;

        for (IdentityAttribute attribute : idAttributes) {
            if (attribute == SEPARATOR)
                continue;

            labelWidth = ImageUtilities.getStringWidth(graphics2D, attribute.getLabel());
            valueWidth = ImageUtilities.getStringWidth(graphics2D, attribute.getValue());
            if (labelWidth > widestLabelWidth)
                widestLabelWidth = labelWidth;
            if (valueWidth > widestValueWidth)
                widestValueWidth = valueWidth;
        }

        totalDataWidth = widestLabelWidth + SPACE_BETWEEN_ITEMS + widestValueWidth;
        totalDataHeight = ImageUtilities.getStringHeight(graphics2D)
                + (ImageUtilities.getStringHeight(graphics2D) * idAttributes.size());

        if ((totalDataWidth < imageableWidth) && (totalDataHeight < imageableDataHeight))
            sizeFound = true;
    }

    logger.finest(new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
            .append("widestLabelWidth", widestLabelWidth).append("widestValueWidth", widestValueWidth)
            .append("totalDataWidth", totalDataWidth).append("totalDataHeight", totalDataHeight).toString());

    // unless with extremely small papers, a size should always have been found.
    // draw the identity, addess and date printed information, in 2 columns, centered inside the
    // space between the header and the bottom of the imageable area
    if (sizeFound) {
        graphics2D.setFont(new Font(FONT, Font.PLAIN, fontSize));
        float labelsLeft = (imageableWidth - totalDataWidth) / 2;
        float valuesLeft = labelsLeft + widestLabelWidth + SPACE_BETWEEN_ITEMS;
        float dataLineHeight = ImageUtilities.getStringHeight(graphics2D);
        float dataTop = dataLineHeight + headerHeight + ((imageableDataHeight - totalDataHeight) / 2);
        float lineNumber = 0;

        logger.finest(new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("labelsLeft", labelsLeft)
                .append("valuesLeft", valuesLeft).append("dataLineHeight", dataLineHeight)
                .append("dataTop", dataTop).toString());

        for (IdentityAttribute attribute : idAttributes) {
            if (attribute != SEPARATOR) // data
            {
                graphics2D.setColor(attribute.isRelevant() ? Color.BLACK : Color.LIGHT_GRAY);
                graphics2D.drawString(attribute.getLabel(), labelsLeft,
                        dataTop + (lineNumber * dataLineHeight));
                graphics2D.drawString(attribute.getValue(), valuesLeft,
                        dataTop + (lineNumber * dataLineHeight));
            } else // separator
            {
                int y = (int) (((dataTop + (lineNumber * dataLineHeight) + (dataLineHeight / 2)))
                        - ImageUtilities.getAscent(graphics2D));
                graphics2D.setColor(Color.BLACK);
                graphics2D.drawLine((int) labelsLeft, y, (int) (labelsLeft + totalDataWidth), y);
            }
            lineNumber++;
        }
    }

    // tell Java printing that all this makes for a page worth printing :-)
    return Printable.PAGE_EXISTS;
}

From source file:me.solhub.simple.engine.DebugLocationsStructure.java

@Override
protected void draw() {
    Graphics2D g = (Graphics2D) getGraphics();
    Graphics2D bbg = (Graphics2D) _backBuffer.getGraphics();

    //anti-aliasing code
    //        bbg.setRenderingHint(
    //                RenderingHints.KEY_TEXT_ANTIALIASING,
    //                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    //        bbg.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );

    bbg.setColor(Color.WHITE);/*from w w  w .j ava  2 s.  c om*/
    bbg.fillRect(0, 0, _windowWidth, _windowHeight);

    bbg.translate(_xOffset, _yOffset);

    // draw destinations
    bbg.setColor(_simState.startingDestination.getColor());
    bbg.drawOval(-(int) _simState.startingDestination.getRadius(),
            -(int) _simState.startingDestination.getRadius(),
            (int) _simState.startingDestination.getRadius() * 2,
            (int) _simState.startingDestination.getRadius() * 2);
    Iterator<Entry<Vector2D, Color>> blah = _destinationColors.entrySet().iterator();
    double destinationRadius = _simState.getDestinationRadius();
    while (blah.hasNext()) {
        Entry<Vector2D, Color> temp = blah.next();
        bbg.setColor(temp.getValue());
        //calculate center coordinate
        int x = (int) (temp.getKey().getX() - (destinationRadius));
        int y = (int) (temp.getKey().getY() - (destinationRadius));
        //drawOval draws a circle inside a rectangle
        bbg.drawOval(x, y, _simState.getDestinationRadius() * 2, _simState.getDestinationRadius() * 2);
    }

    // draw each of the agents
    Iterator<Agent> agentIter = _simState.getAgentIterator();
    while (agentIter.hasNext()) {
        Agent temp = agentIter.next();
        if (temp.isAlive()) {
            // decide whether to color for destination or group
            //                if( isDestinationColors )
            //                {
            //                    // if stopped then blink white and destination color
            //                    if( temp.hasReachedDestination() )
            //                    {
            //                        if( pulseWhite % 20 == 0 )
            //                        {
            //                            bbg.setColor( Color.WHITE );
            //                        }
            //                        else
            //                        {
            //                            bbg.setColor( temp.getDestinationColor() );
            //                        }
            //                    }
            //                    else
            //                    {
            //                        bbg.setColor( temp.getDestinationColor() );
            //                    }
            //                }
            //                else
            //                {
            //                    // if stopped then blink black and white
            //                    if( temp.hasReachedDestination() )
            //                    {
            //                        if( pulseWhite % 20 == 0 )
            //                        {
            //                            bbg.setColor( Color.WHITE );
            //                        }
            //                        else
            //                        {
            //                            bbg.setColor( temp.getGroup().getGroupColor() );
            //                        }
            //                    }
            //                    //set color to red if cancelled and global and not multiple initiators
            //                    else if(temp.getCurrentDecision().getDecision().getDecisionType().equals(
            //                            DecisionType.CANCELLATION ) 
            //                            && _simState.getCommunicationType().equals( "global" )
            //                            && !Agent.canMultipleInitiate()
            //                            )
            //                    {
            //                        bbg.setColor( Color.RED );
            //                    }
            //                    else
            //                    {
            //                        bbg.setColor( temp.getGroup().getGroupColor() );
            //                    }
            //                }

            double dx = temp.getCurrentDestination().getX() - temp.getCurrentLocation().getX();
            double dy = temp.getCurrentDestination().getY() - temp.getCurrentLocation().getY();
            double heading = Math.atan2(dy, dx);
            Utils.drawDirectionalTriangle(bbg, heading - Math.PI / 2, temp.getCurrentLocation().getX(),
                    temp.getCurrentLocation().getY(), 7, temp.getPreferredDestination().getColor(),
                    temp.getGroup().getGroupColor());

            //                bbg.fillOval( (int) temp.getCurrentLocation().getX() - _agentSize,
            //                        (int) temp.getCurrentLocation().getY() - _agentSize , _agentSize * 2, _agentSize * 2 );
        }
    }
    pulseWhite++;
    bbg.setColor(Color.BLACK);
    // the total number of groups
    bbg.setFont(_infoFont);

    bbg.drawString("Run: " + (_simState.getCurrentSimulationRun() + 1), _fontXOffset, _fontYOffset);
    bbg.drawString("Time: " + _simState.getSimulationTime(), _fontXOffset, _fontYOffset + _fontSize);
    bbg.drawString("Delay: " + LIVE_DELAY, _fontXOffset, _fontYOffset + _fontSize * 2);

    if (_simState.getCommunicationType().equals("global") && !Agent.canMultipleInitiate()) {
        String initiatorName = "None";
        if (_initiatingAgent != null) {
            initiatorName = _initiatingAgent.getId().toString();
        }
        bbg.drawString("Init: " + initiatorName, _fontXOffset, _fontYOffset + _fontSize * 3);
        bbg.drawString("Followers: " + _numberFollowing, _fontXOffset, _fontYOffset + _fontSize * 4);
    } else {
        bbg.drawString("Groups: " + _simState.getNumberGroups(), _fontXOffset, _fontYOffset + _fontSize * 3);
        bbg.drawString("Reached: " + _simState.numReachedDestination, _fontXOffset,
                _fontYOffset + _fontSize * 4);
        bbg.drawString("Inits: " + _simState.numInitiating, _fontXOffset, _fontYOffset + _fontSize * 5);
        bbg.drawString("Eaten: " + _simState.getPredator().getTotalAgentsEaten(), _fontXOffset,
                _fontYOffset + _fontSize * 6);
    }

    g.scale(_zoom, _zoom);
    g.drawImage(_backBuffer, 0, 0, this);
    if (SHOULD_VIDEO) {

        // setup to save to a png
        BufferedImage buff = new BufferedImage(_windowWidth, _windowHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D temp = (Graphics2D) buff.getGraphics();
        temp.scale(8, 4);
        temp.drawImage(_backBuffer, 0, 0, this);
        // sub-directory
        File dir = new File("video");
        dir.mkdir();
        // format string for filename
        String filename = String.format("video/run-%03d-time-%05d.png", _simState.getCurrentSimulationRun(),
                _simState.getSimulationTime());
        File outputfile = new File(filename);
        // save it
        try {
            ImageIO.write(buff, "png", outputfile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:savant.view.tracks.BAMTrackRenderer.java

/**
 * Render the individual bases on top of the read. Depending on the drawing
 * mode this can be either bases read or mismatches.
 *//*from w ww. j a va  2 s.  c  o m*/
private void renderBases(Graphics2D g2, GraphPaneAdapter gp, SAMRecord samRecord, int level, byte[] refSeq,
        Range range, double unitHeight) {

    ColourScheme cs = (ColourScheme) instructions.get(DrawingInstruction.COLOUR_SCHEME);

    boolean baseQualityEnabled = (Boolean) instructions.get(DrawingInstruction.BASE_QUALITY);
    boolean drawingAllBases = lastMode == DrawingMode.SEQUENCE || baseQualityEnabled;

    double unitWidth = gp.getUnitWidth();
    int offset = gp.getOffset();

    // Cutoffs to determine when not to draw
    double leftMostX = gp.transformXPos(range.getFrom());
    double rightMostX = gp.transformXPos(range.getTo()) + unitWidth;

    int alignmentStart = samRecord.getAlignmentStart();

    byte[] readBases = samRecord.getReadBases();
    byte[] baseQualities = samRecord.getBaseQualities();
    boolean sequenceSaved = readBases.length > 0;
    Cigar cigar = samRecord.getCigar();

    // Absolute positions in the reference sequence and the read bases, set after each cigar operator is processed
    int sequenceCursor = alignmentStart;
    int readCursor = alignmentStart;
    List<Rectangle2D> insertions = new ArrayList<Rectangle2D>();

    FontMetrics fm = g2.getFontMetrics(MISMATCH_FONT);
    Rectangle2D charRect = fm.getStringBounds("G", g2);
    boolean fontFits = charRect.getWidth() <= unitWidth && charRect.getHeight() <= unitHeight;
    if (fontFits) {
        g2.setFont(MISMATCH_FONT);
    }
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    for (CigarElement cigarElement : cigar.getCigarElements()) {

        int operatorLength = cigarElement.getLength();
        CigarOperator operator = cigarElement.getOperator();
        Rectangle2D.Double opRect = null;

        double opStart = gp.transformXPos(sequenceCursor);
        double opWidth = operatorLength * unitWidth;

        // Cut off start and width so no drawing happens off-screen, must be done in the order w, then x, since w depends on first value of x
        double x2 = Math.min(rightMostX, opStart + opWidth);
        opStart = Math.max(leftMostX, opStart);
        opWidth = x2 - opStart;

        switch (operator) {
        case D: // Deletion
            if (opWidth > 0.0) {
                renderDeletion(g2, gp, opStart, level, operatorLength, unitHeight);
            }
            break;

        case I: // Insertion
            insertions.add(new Rectangle2D.Double(gp.transformXPos(sequenceCursor),
                    gp.transformYPos(0) - ((level + 1) * unitHeight) - gp.getOffset(), unitWidth, unitHeight));
            break;

        case M: // Match or mismatch
        case X:
        case EQ:
            // some SAM files do not contain the read bases
            if (sequenceSaved || operator == CigarOperator.X) {
                for (int i = 0; i < operatorLength; i++) {
                    // indices into refSeq and readBases associated with this position in the cigar string
                    int readIndex = readCursor - alignmentStart + i;
                    boolean mismatched = false;
                    if (operator == CigarOperator.X) {
                        mismatched = true;
                    } else {
                        int refIndex = sequenceCursor + i - range.getFrom();
                        if (refIndex >= 0 && refSeq != null && refIndex < refSeq.length) {
                            mismatched = refSeq[refIndex] != readBases[readIndex];
                        }
                    }

                    if (mismatched || drawingAllBases) {
                        Color col;
                        if ((mismatched && lastMode != DrawingMode.STANDARD)
                                || lastMode == DrawingMode.SEQUENCE) {
                            col = cs.getBaseColor((char) readBases[readIndex]);
                        } else {
                            col = cs.getColor(samRecord.getReadNegativeStrandFlag() ? ColourKey.REVERSE_STRAND
                                    : ColourKey.FORWARD_STRAND);
                        }

                        if (baseQualityEnabled && col != null) {
                            col = new Color(col.getRed(), col.getGreen(), col.getBlue(), getConstrainedAlpha(
                                    (int) Math.round((baseQualities[readIndex] * 0.025) * 255)));
                        }

                        double xCoordinate = gp.transformXPos(sequenceCursor + i);
                        double top = gp.transformYPos(0) - ((level + 1) * unitHeight) - offset;
                        if (col != null) {
                            opRect = new Rectangle2D.Double(xCoordinate, top, unitWidth, unitHeight);
                            g2.setColor(col);
                            g2.fill(opRect);
                        }
                        if (lastMode != DrawingMode.SEQUENCE && mismatched && fontFits) {
                            // If it's a real mismatch, we want to draw the base letter (space permitting).
                            g2.setColor(new Color(10, 10, 10));
                            String s = new String(readBases, readIndex, 1);
                            charRect = fm.getStringBounds(s, g2);
                            g2.drawString(s, (float) (xCoordinate + (unitWidth - charRect.getWidth()) * 0.5),
                                    (float) (top + fm.getAscent() + (unitHeight - charRect.getHeight()) * 0.5));
                        }
                    }
                }
            }
            break;

        case N: // Skipped
            opRect = new Rectangle2D.Double(opStart, gp.transformYPos(0) - ((level + 1) * unitHeight) - offset,
                    opWidth, unitHeight);
            g2.setColor(cs.getColor(ColourKey.SKIPPED));
            g2.fill(opRect);
            break;

        default: // P - passing, H - hard clip, or S - soft clip
            break;
        }
        if (operator.consumesReadBases()) {
            readCursor += operatorLength;
        }
        if (operator.consumesReferenceBases()) {
            sequenceCursor += operatorLength;
        }
    }
    for (Rectangle2D ins : insertions) {
        drawInsertion(g2, ins.getX(), ins.getY(), ins.getWidth(), ins.getHeight());
    }
}

From source file:com.igormaznitsa.jhexed.swing.editor.ui.exporters.PNGImageExporter.java

public BufferedImage generateImage() throws IOException {
    final int DEFAULT_CELL_WIDTH = 48;
    final int DEFAULT_CELL_HEIGHT = 48;

    final int imgWidth = this.docOptions.getImage() == null ? DEFAULT_CELL_WIDTH * this.docOptions.getColumns()
            : Math.round(this.docOptions.getImage().getSVGWidth());
    final int imgHeight = this.docOptions.getImage() == null ? DEFAULT_CELL_HEIGHT * this.docOptions.getRows()
            : Math.round(this.docOptions.getImage().getSVGHeight());

    final BufferedImage result;
    if (exportData.isBackgroundImageExport() && this.docOptions.getImage() != null) {
        result = this.docOptions.getImage().rasterize(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB);
    } else {//from w  w  w .jav a  2s.  com
        result = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB);
    }

    final Graphics2D gfx = result.createGraphics();
    gfx.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
            RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    gfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    gfx.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    final HexEngine<Graphics2D> engine = new HexEngine<Graphics2D>(DEFAULT_CELL_WIDTH, DEFAULT_CELL_HEIGHT,
            this.docOptions.getHexOrientation());

    final List<HexFieldLayer> reversedNormalizedStack = new ArrayList<HexFieldLayer>();
    for (int i = this.exportData.getLayers().size() - 1; i >= 0; i--) {
        final LayerExportRecord rec = this.exportData.getLayers().get(i);
        if (rec.isAllowed()) {
            reversedNormalizedStack.add(rec.getLayer());
        }
    }

    if (Thread.currentThread().isInterrupted())
        return null;

    final HexFieldValue[] stackOfValues = new HexFieldValue[reversedNormalizedStack.size()];

    engine.setModel(new HexEngineModel<HexFieldValue[]>() {

        @Override
        public int getColumnNumber() {
            return docOptions.getColumns();
        }

        @Override
        public int getRowNumber() {
            return docOptions.getRows();
        }

        @Override
        public HexFieldValue[] getValueAt(final int col, final int row) {
            Arrays.fill(stackOfValues, null);

            for (int index = 0; index < reversedNormalizedStack.size(); index++) {
                stackOfValues[index] = reversedNormalizedStack.get(index).getHexValueAtPos(col, row);
            }
            return stackOfValues;
        }

        @Override
        public HexFieldValue[] getValueAt(final HexPosition pos) {
            return this.getValueAt(pos.getColumn(), pos.getRow());
        }

        @Override
        public void setValueAt(int col, int row, HexFieldValue[] value) {
        }

        @Override
        public void setValueAt(HexPosition pos, HexFieldValue[] value) {
        }

        @Override
        public boolean isPositionValid(final int col, final int row) {
            return col >= 0 && col < docOptions.getColumns() && row >= 0 && row < docOptions.getRows();
        }

        @Override
        public boolean isPositionValid(final HexPosition pos) {
            return this.isPositionValid(pos.getColumn(), pos.getRow());
        }

        @Override
        public void attachedToEngine(final HexEngine<?> engine) {
        }

        @Override
        public void detachedFromEngine(final HexEngine<?> engine) {
        }
    });

    final HexRect2D visibleSize = engine.getVisibleSize();
    final float xcoeff = (float) result.getWidth() / visibleSize.getWidth();
    final float ycoeff = (float) result.getHeight() / visibleSize.getHeight();
    engine.setScale(xcoeff, ycoeff);

    final Image[][] cachedIcons = new Image[this.exportData.getLayers().size()][];
    engine.setRenderer(new ColorHexRender() {

        private final Stroke stroke = new BasicStroke(docOptions.getLineWidth());

        @Override
        public Stroke getStroke() {
            return this.stroke;
        }

        @Override
        public Color getFillColor(HexEngineModel<?> model, int col, int row) {
            return null;
        }

        @Override
        public Color getBorderColor(HexEngineModel<?> model, int col, int row) {
            return exportData.isExportHexBorders() ? docOptions.getColor() : null;
        }

        @Override
        public void drawExtra(HexEngine<Graphics2D> engine, Graphics2D g, int col, int row, Color borderColor,
                Color fillColor) {
        }

        @Override
        public void drawUnderBorder(final HexEngine<Graphics2D> engine, final Graphics2D g, final int col,
                final int row, final Color borderColor, final Color fillColor) {
            final HexFieldValue[] stackValues = (HexFieldValue[]) engine.getModel().getValueAt(col, row);
            for (int i = 0; i < stackValues.length; i++) {
                final HexFieldValue valueToDraw = stackValues[i];
                if (valueToDraw == null) {
                    continue;
                }
                g.drawImage(cachedIcons[i][valueToDraw.getIndex()], 0, 0, null);
            }
        }

    });

    final Path2D hexShape = ((ColorHexRender) engine.getRenderer()).getHexPath();
    final int cellWidth = hexShape.getBounds().width;
    final int cellHeight = hexShape.getBounds().height;

    for (int layerIndex = 0; layerIndex < reversedNormalizedStack.size()
            && !Thread.currentThread().isInterrupted(); layerIndex++) {
        final HexFieldLayer theLayer = reversedNormalizedStack.get(layerIndex);
        final Image[] cacheLineForLayer = new Image[theLayer.getHexValuesNumber()];
        for (int valueIndex = 1; valueIndex < theLayer.getHexValuesNumber(); valueIndex++) {
            cacheLineForLayer[valueIndex] = theLayer.getHexValueForIndex(valueIndex).makeIcon(cellWidth,
                    cellHeight, hexShape, true);
        }
        cachedIcons[layerIndex] = cacheLineForLayer;
    }

    engine.drawWithThreadInterruptionCheck(gfx);
    if (Thread.currentThread().isInterrupted())
        return null;

    if (this.exportData.isCellCommentariesExport()) {
        final Iterator<Entry<HexPosition, String>> iterator = this.cellComments.iterator();
        gfx.setFont(new Font("Arial", Font.BOLD, 12));
        while (iterator.hasNext() && !Thread.currentThread().isInterrupted()) {
            final Entry<HexPosition, String> item = iterator.next();
            final HexPosition pos = item.getKey();
            final String text = item.getValue();
            final float x = engine.calculateX(pos.getColumn(), pos.getRow());
            final float y = engine.calculateY(pos.getColumn(), pos.getRow());

            final Rectangle2D textBounds = gfx.getFontMetrics().getStringBounds(text, gfx);

            final float dx = x - ((float) textBounds.getWidth() - engine.getCellWidth()) / 2;

            gfx.setColor(Color.BLACK);
            gfx.drawString(text, dx, y);
            gfx.setColor(Color.WHITE);
            gfx.drawString(text, dx - 2, y - 2);
        }
    }

    gfx.dispose();

    return result;
}

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

private boolean redraw() {
    Village[][] mVisibleVillages = DataHolder.getSingleton().getVillages();

    if (mVisibleVillages == null || mBuffer == null) {
        return false;
    }/*from  w w  w.ja  va 2 s.  c  o m*/

    Graphics2D g2d = (Graphics2D) mBuffer.getGraphics();
    Composite tempC = g2d.getComposite();
    //clear
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR));
    g2d.fillRect(0, 0, mBuffer.getWidth(), mBuffer.getHeight());

    //reset composite
    g2d.setComposite(tempC);

    boolean markPlayer = GlobalOptions.getProperties().getBoolean("mark.villages.on.minimap");
    if (ServerSettings.getSingleton().getMapDimension() == null) {
        //could not draw minimap if dimensions are not loaded yet
        return false;
    }
    boolean showBarbarian = GlobalOptions.getProperties().getBoolean("show.barbarian");

    Color DEFAULT = Constants.DS_DEFAULT_MARKER;
    try {
        int mark = Integer.parseInt(GlobalOptions.getProperty("default.mark"));
        if (mark == 0) {
            DEFAULT = Constants.DS_DEFAULT_MARKER;
        } else if (mark == 1) {
            DEFAULT = Color.RED;
        } else if (mark == 2) {
            DEFAULT = Color.WHITE;
        }
    } catch (Exception e) {
        DEFAULT = Constants.DS_DEFAULT_MARKER;
    }

    Rectangle mapDim = ServerSettings.getSingleton().getMapDimension();
    double wField = mapDim.getWidth() / (double) visiblePart.width;
    double hField = mapDim.getHeight() / (double) visiblePart.height;

    UserProfile profile = GlobalOptions.getSelectedProfile();
    Tribe currentTribe = InvalidTribe.getSingleton();
    if (profile != null) {
        currentTribe = profile.getTribe();
    }

    for (int i = visiblePart.x; i < (visiblePart.width + visiblePart.x); i++) {
        for (int j = visiblePart.y; j < (visiblePart.height + visiblePart.y); j++) {
            Village v = mVisibleVillages[i][j];
            if (v != null) {
                Color markerColor = null;
                boolean isLeft = false;
                if (v.getTribe() == Barbarians.getSingleton()) {
                    isLeft = true;
                } else {
                    if ((currentTribe != null) && (v.getTribe().getId() == currentTribe.getId())) {
                        //village is owned by current player. mark it dependent on settings
                        if (markPlayer) {
                            markerColor = Color.YELLOW;
                        }
                    } else {
                        try {
                            Marker marker = MarkerManager.getSingleton().getMarker(v.getTribe());
                            if (marker != null && !marker.isShownOnMap()) {
                                marker = null;
                                markerColor = DEFAULT;
                            }

                            if (marker == null) {
                                marker = MarkerManager.getSingleton().getMarker(v.getTribe().getAlly());
                                if (marker != null && marker.isShownOnMap()) {
                                    markerColor = marker.getMarkerColor();
                                }
                            } else {
                                if (marker.isShownOnMap()) {
                                    markerColor = marker.getMarkerColor();
                                }
                            }
                        } catch (Exception e) {
                            markerColor = null;
                        }
                    }
                }

                if (!isLeft) {
                    if (markerColor != null) {
                        g2d.setColor(markerColor);
                    } else {
                        g2d.setColor(DEFAULT);
                    }
                    g2d.fillRect((int) Math.round((i - visiblePart.x) * wField),
                            (int) Math.round((j - visiblePart.y) * hField), (int) Math.floor(wField),
                            (int) Math.floor(hField));
                } else {
                    if (showBarbarian) {
                        g2d.setColor(Color.LIGHT_GRAY);
                        g2d.fillRect((int) Math.round((i - visiblePart.x) * wField),
                                (int) Math.round((j - visiblePart.y) * hField), (int) Math.floor(wField),
                                (int) Math.floor(hField));
                    }
                }
            }
        }
    }

    try {
        if (GlobalOptions.getProperties().getBoolean("map.showcontinents")) {
            g2d.setColor(Color.BLACK);
            Composite c = g2d.getComposite();
            Composite a = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f);
            Font f = g2d.getFont();
            Font t = new Font("Serif", Font.BOLD, (int) Math.round(30 * hField));
            g2d.setFont(t);
            int fact = 10;
            int mid = (int) Math.round(50 * wField);

            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 10; j++) {
                    g2d.setComposite(a);

                    String conti = "K" + (j * 10 + i);
                    Rectangle2D bounds = g2d.getFontMetrics(t).getStringBounds(conti, g2d);
                    int cx = i * fact * 10 - visiblePart.x;
                    int cy = j * fact * 10 - visiblePart.y;
                    cx = (int) Math.round(cx * wField);
                    cy = (int) Math.round(cy * hField);
                    g2d.drawString(conti, (int) Math.rint(cx + mid - bounds.getWidth() / 2),
                            (int) Math.rint(cy + mid + bounds.getHeight() / 2));
                    g2d.setComposite(c);
                    int wk = 100;
                    int hk = 100;

                    if (i == 9) {
                        wk -= 1;
                    }
                    if (j == 9) {
                        hk -= 1;
                    }

                    g2d.drawRect(cx, cy, (int) Math.round(wk * wField), (int) Math.round(hk * hField));
                }
            }
            g2d.setFont(f);
        }
    } catch (Exception e) {
        logger.error("Creation of Minimap failed", e);
    }
    g2d.dispose();
    return true;
}

From source file:de.laures.cewolf.jfree.ThermometerPlot.java

/**
 * Draws the plot on a Java 2D graphics device (such as the screen or a printer).
 *
 * @param g2  the graphics device.//from   w w w .  j a  va  2s. c  o  m
 * @param area  the area within which the plot should be drawn.
 * @param anchor  the anchor point (<code>null</code> permitted).
 * @param parentState  the state from the parent plot, if there is one.
 * @param info  collects info about the drawing.
 */
public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState,
        PlotRenderingInfo info) {

    RoundRectangle2D outerStem = new RoundRectangle2D.Double();
    RoundRectangle2D innerStem = new RoundRectangle2D.Double();
    RoundRectangle2D mercuryStem = new RoundRectangle2D.Double();
    Ellipse2D outerBulb = new Ellipse2D.Double();
    Ellipse2D innerBulb = new Ellipse2D.Double();
    String temp = null;
    FontMetrics metrics = null;
    if (info != null) {
        info.setPlotArea(area);
    }

    // adjust for insets...
    RectangleInsets insets = getInsets();
    insets.trim(area);
    drawBackground(g2, area);

    // adjust for padding...
    Rectangle2D interior = (Rectangle2D) area.clone();
    this.padding.trim(interior);
    int midX = (int) (interior.getX() + (interior.getWidth() / 2));
    int midY = (int) (interior.getY() + (interior.getHeight() / 2));
    int stemTop = (int) (interior.getMinY() + getBulbRadius());
    int stemBottom = (int) (interior.getMaxY() - getBulbDiameter());
    Rectangle2D dataArea = new Rectangle2D.Double(midX - getColumnRadius(), stemTop, getColumnRadius(),
            stemBottom - stemTop);

    outerBulb.setFrame(midX - getBulbRadius(), stemBottom, getBulbDiameter(), getBulbDiameter());

    outerStem.setRoundRect(midX - getColumnRadius(), interior.getMinY(), getColumnDiameter(),
            stemBottom + getBulbDiameter() - stemTop, getColumnDiameter(), getColumnDiameter());

    Area outerThermometer = new Area(outerBulb);
    Area tempArea = new Area(outerStem);
    outerThermometer.add(tempArea);

    innerBulb.setFrame(midX - getBulbRadius() + getGap(), stemBottom + getGap(),
            getBulbDiameter() - getGap() * 2, getBulbDiameter() - getGap() * 2);

    innerStem.setRoundRect(midX - getColumnRadius() + getGap(), interior.getMinY() + getGap(),
            getColumnDiameter() - getGap() * 2, stemBottom + getBulbDiameter() - getGap() * 2 - stemTop,
            getColumnDiameter() - getGap() * 2, getColumnDiameter() - getGap() * 2);

    Area innerThermometer = new Area(innerBulb);
    tempArea = new Area(innerStem);
    innerThermometer.add(tempArea);

    if ((this.dataset != null) && (this.dataset.getValue() != null)) {
        double current = this.dataset.getValue().doubleValue();
        double ds = this.rangeAxis.valueToJava2D(current, dataArea, RectangleEdge.LEFT);

        int i = getColumnDiameter() - getGap() * 2; // already calculated
        int j = getColumnRadius() - getGap(); // already calculated
        int l = (i / 2);
        int k = (int) Math.round(ds);
        if (k < (getGap() + interior.getMinY())) {
            k = (int) (getGap() + interior.getMinY());
            l = getBulbRadius();
        }

        Area mercury = new Area(innerBulb);

        if (k < (stemBottom + getBulbRadius())) {
            mercuryStem.setRoundRect(midX - j, k, i, (stemBottom + getBulbRadius()) - k, l, l);
            tempArea = new Area(mercuryStem);
            mercury.add(tempArea);
        }

        g2.setPaint(getCurrentPaint());
        g2.fill(mercury);

        // draw range indicators...
        if (this.subrangeIndicatorsVisible) {
            g2.setStroke(this.subrangeIndicatorStroke);
            Range range = this.rangeAxis.getRange();

            // draw start of normal range
            double value = this.subrangeInfo[NORMAL][RANGE_LOW];
            if (range.contains(value)) {
                double x = midX + getColumnRadius() + 2;
                double y = this.rangeAxis.valueToJava2D(value, dataArea, RectangleEdge.LEFT);
                Line2D line = new Line2D.Double(x, y, x + 10, y);
                g2.setPaint(this.subrangePaint[NORMAL]);
                g2.draw(line);
            }

            // draw start of warning range
            value = this.subrangeInfo[WARNING][RANGE_LOW];
            if (range.contains(value)) {
                double x = midX + getColumnRadius() + 2;
                double y = this.rangeAxis.valueToJava2D(value, dataArea, RectangleEdge.LEFT);
                Line2D line = new Line2D.Double(x, y, x + 10, y);
                g2.setPaint(this.subrangePaint[WARNING]);
                g2.draw(line);
            }

            // draw start of critical range
            value = this.subrangeInfo[CRITICAL][RANGE_LOW];
            if (range.contains(value)) {
                double x = midX + getColumnRadius() + 2;
                double y = this.rangeAxis.valueToJava2D(value, dataArea, RectangleEdge.LEFT);
                Line2D line = new Line2D.Double(x, y, x + 10, y);
                g2.setPaint(this.subrangePaint[CRITICAL]);
                g2.draw(line);
            }
        }

        // draw the axis...
        if ((this.rangeAxis != null) && (this.axisLocation != NONE)) {
            int drawWidth = AXIS_GAP;
            Rectangle2D drawArea;
            double cursor = 0;

            switch (this.axisLocation) {
            case RIGHT:
                cursor = midX + getColumnRadius();
                drawArea = new Rectangle2D.Double(cursor, stemTop, drawWidth, (stemBottom - stemTop + 1));
                this.rangeAxis.draw(g2, cursor, area, drawArea, RectangleEdge.RIGHT, null);
                break;

            case LEFT:
            default:
                //cursor = midX - COLUMN_RADIUS - AXIS_GAP;
                cursor = midX - getColumnRadius();
                drawArea = new Rectangle2D.Double(cursor, stemTop, drawWidth, (stemBottom - stemTop + 1));
                this.rangeAxis.draw(g2, cursor, area, drawArea, RectangleEdge.LEFT, null);
                break;
            }

        }

        // draw text value on screen
        g2.setFont(this.valueFont);
        g2.setPaint(this.valuePaint);
        metrics = g2.getFontMetrics();
        switch (this.valueLocation) {
        case RIGHT:
            g2.drawString(this.valueFormat.format(current), midX + getColumnRadius() + getGap(), midY);
            break;
        case LEFT:
            String valueString = this.valueFormat.format(current);
            int stringWidth = metrics.stringWidth(valueString);
            g2.drawString(valueString, midX - getColumnRadius() - getGap() - stringWidth, midY);
            break;
        case BULB:
            temp = this.valueFormat.format(current);
            i = metrics.stringWidth(temp) / 2;
            g2.drawString(temp, midX - i, stemBottom + getBulbRadius() + getGap());
            break;
        default:
        }
        /***/
    }

    g2.setPaint(this.thermometerPaint);
    g2.setFont(this.valueFont);

    //  draw units indicator
    metrics = g2.getFontMetrics();
    int tickX1 = midX - getColumnRadius() - getGap() * 2 - metrics.stringWidth(UNITS[this.units]);
    if (tickX1 > area.getMinX()) {
        g2.drawString(UNITS[this.units], tickX1, (int) (area.getMinY() + 20));
    }

    // draw thermometer outline
    g2.setStroke(this.thermometerStroke);
    g2.draw(outerThermometer);
    g2.draw(innerThermometer);

    drawOutline(g2, area);
}

From source file:org.pmedv.blackboard.components.BoardEditor.java

@Override
protected void paintComponent(Graphics g) {
    Boolean useLayerColor = (Boolean) Preferences.values
            .get("org.pmedv.blackboard.BoardDesignerPerspective.useLayerColor");

    // clear all      
    // g.clearRect(0, 0, getWidth(), getHeight());
    g.setColor(BLANK);//  w ww.  ja  v a 2  s .  c  o m
    g.fillRect(0, 0, getWidth(), getHeight());
    // some nice anti aliasing...
    Graphics2D g2 = (Graphics2D) g;
    RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2.setRenderingHints(rh);

    // TODO : Should this be done here?
    // Sort layers by z-index

    g2.setColor(Color.LIGHT_GRAY);
    g2.setStroke(BoardUtil.stroke_1_0f);

    Collections.sort(model.getLayers());
    for (int i = model.getLayers().size() - 1; i >= 0; i--) {
        // Sort items by z-index
        Collections.sort(model.getLayers().get(i).getItems());
        drawLayer(model.getLayers().get(i), g2);
    }

    // draw selection border
    if (state.equals(SelectionState.DRAGGING_NEW_SELECTION) && button1Pressed) {
        g2.setColor(Color.GREEN);
        g2.setStroke(BoardUtil.stroke_1_0f);
        if (dragStopX < dragStartX && dragStopY > dragStartY) {
            selectionBorder.setSize(dragStartX - dragStopX, dragStopY - dragStartY);
            selectionBorder.setLocation(dragStopX, dragStartY);
        } else if (dragStopY < dragStartY && dragStopX > dragStartX) {
            selectionBorder.setSize(dragStopX - dragStartX, dragStartY - dragStopY);
            selectionBorder.setLocation(dragStartX, dragStopY);
        } else if (dragStopX < dragStartX && dragStopY < dragStartY) {
            selectionBorder.setSize(dragStartX - dragStopX, dragStartY - dragStopY);
            selectionBorder.setLocation(dragStopX, dragStopY);
        } else {
            selectionBorder.setSize(dragStopX - dragStartX, dragStopY - dragStartY);
            selectionBorder.setLocation(dragStartX, dragStartY);
        }
        g2.draw(selectionBorder);
    }
    // display shape currently being drawed
    if (lineStartX > 0 && lineStartY > 0 && lineStopX > 0 && lineStopY > 0) {

        if (useLayerColor)
            g2.setColor(model.getCurrentLayer().getColor());
        else
            g2.setColor(palette.getCurrentColor());

        g2.setStroke((BasicStroke) shapesPanel.getThicknessCombo().getSelectedItem());
        // draw new line
        if (editorMode.equals(EditorMode.DRAW_LINE)) {

            if (useLayerColor)
                currentDrawingLine.setColor(model.getCurrentLayer().getColor());
            else
                currentDrawingLine.setColor(palette.getCurrentColor());

            currentDrawingLine.setStartType((LineEdgeType) shapesPanel.getStartLineCombo().getSelectedItem());
            currentDrawingLine.setEndType((LineEdgeType) shapesPanel.getEndLineCombo().getSelectedItem());
            currentDrawingLine.setStroke((BasicStroke) shapesPanel.getThicknessCombo().getSelectedItem());
            currentDrawingLine.getStart().setLocation(lineStartX, lineStartY);
            currentDrawingLine.getEnd().setLocation(lineStopX, lineStopY);
            currentDrawingLine.draw(g2);
        } else if (editorMode.equals(EditorMode.DRAW_MEASURE)) {
            currentDrawingLine.setStroke(Measure.DEFAULT_STROKE);
            currentDrawingLine.getStart().setLocation(lineStartX, lineStartY);
            currentDrawingLine.getEnd().setLocation(lineStopX, lineStopY);
            currentDrawingLine.draw(g2);
        }
        // draw new box or ellipse
        else if (editorMode.equals(EditorMode.DRAW_RECTANGLE) || editorMode.equals(EditorMode.DRAW_ELLIPSE)) {
            int xLoc = lineStartX;
            int yLoc = lineStartY;
            int width = lineStopX - lineStartX;
            int height = lineStopY - lineStartY;
            ShapeStyle style = (ShapeStyle) shapesPanel.getStyleCombo().getSelectedItem();
            if (style == null || style.equals(ShapeStyle.FILLED)) {
                if (editorMode.equals(EditorMode.DRAW_RECTANGLE)) {
                    g2.fillRect(xLoc, yLoc, width, height);
                } else {
                    g2.fillOval(xLoc, yLoc, width, height);
                }
            } else if (style.equals(ShapeStyle.OUTLINED)) {
                g2.setStroke((BasicStroke) shapesPanel.getThicknessCombo().getSelectedItem());
                if (editorMode.equals(EditorMode.DRAW_RECTANGLE)) {
                    g2.drawRect(xLoc, yLoc, width, height);
                } else {
                    g2.drawOval(xLoc, yLoc, width, height);
                }
            }
        }
    }
    // draw selection handles
    if (selectedItem != null) {
        g2.setStroke(BoardUtil.stroke_1_0f);
        g2.setColor(Color.GREEN);
        selectedItem.drawHandles(g2, 8);
    }

    // draw border

    if (zoomLayer != null) {
        TransformUI ui = (TransformUI) (Object) zoomLayer.getUI();
        DefaultTransformModel xmodel = (DefaultTransformModel) ui.getModel();

        if (xmodel.isMirror()) {
            g2.setColor(Color.RED);
        } else {
            g2.setColor(Color.GREEN);
        }
    } else {
        g2.setColor(Color.GREEN);
    }

    g2.setStroke(DEFAULT_STROKE);

    Rectangle border = new Rectangle(0, 0, model.getWidth() - 1, model.getHeight() - 1);
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
    g2.draw(border);

    if (model.getType().equals(BoardType.STRIPES) || model.getType().equals(BoardType.HOLES)) {

        g2.setColor(Color.BLACK);
        g2.setFont(miniFont);

        int index = 1;

        for (int x = 12; x < model.getWidth() - 16; x += 16) {
            g2.drawString(String.valueOf(index++), x, 8);
        }

        index = 1;

        for (int y = 18; y < model.getHeight(); y += 16) {
            g2.drawString(String.valueOf(index++), 3, y);
        }

    }

    if (editorMode.equals(EditorMode.CHECK_CONNECTIONS)) {
        if (connectedLines != null) {
            for (Line line : connectedLines) {
                line.drawFat(g2);
            }
        }
    }

    if (drawing) {

        g2.setColor(Color.BLUE);
        g2.setStroke(DEFAULT_STROKE);

        if (selectedPin != null) {
            g2.drawRect(lineStopX - 8, lineStopY - 8, 16, 16);
        }

    }

    super.paintComponents(g2);
}

From source file:ucar.unidata.idv.ui.ImageGenerator.java

/**
 * Process the lat/lon labels tag//from   w w w.j  a  va 2s. c  om
 *
 * @param child  the XML
 * @param viewManager  the associated view manager
 * @param image  the image to draw on
 * @param imageProps  the image properties
 *
 * @return  a new image
 *
 * @throws Exception  on badness
 */
public BufferedImage doLatLonLabels(Element child, ViewManager viewManager, BufferedImage image,
        Hashtable imageProps) throws Exception {

    if (viewManager == null) {
        throw new IllegalArgumentException("Tag " + TAG_LATLONLABELS + " requires a view");
    }
    if (!(viewManager instanceof MapViewManager)) {
        throw new IllegalArgumentException("Tag " + TAG_LATLONLABELS + " requires a map view");
    }
    MapViewManager mvm = (MapViewManager) viewManager;
    NavigatedDisplay display = (NavigatedDisplay) viewManager.getMaster();
    DecimalFormat format = new DecimalFormat(applyMacros(child, ATTR_FORMAT, "##0.0"));
    Color color = applyMacros(child, ATTR_COLOR, Color.red);
    Color lineColor = applyMacros(child, ATTR_LINECOLOR, color);
    Color bg = applyMacros(child, ATTR_LABELBACKGROUND, (Color) null);

    double[] latValues = Misc.parseDoubles(applyMacros(child, ATTR_LAT_VALUES, ""));
    List<String> latLabels = StringUtil.split(applyMacros(child, ATTR_LAT_LABELS, ""), ",", true, true);
    double[] lonValues = Misc.parseDoubles(applyMacros(child, ATTR_LON_VALUES, ""));
    List<String> lonLabels = StringUtil.split(applyMacros(child, ATTR_LON_LABELS, ""), ",", true, true);

    boolean drawLonLines = applyMacros(child, ATTR_DRAWLONLINES, false);
    boolean drawLatLines = applyMacros(child, ATTR_DRAWLATLINES, false);
    boolean showTop = applyMacros(child, ATTR_SHOWTOP, false);
    boolean showBottom = applyMacros(child, ATTR_SHOWBOTTOM, true);
    boolean showLeft = applyMacros(child, ATTR_SHOWLEFT, true);
    boolean showRight = applyMacros(child, ATTR_SHOWRIGHT, false);

    int width = image.getWidth(null);
    int height = image.getHeight(null);
    int centerX = width / 2;
    int centerY = height / 2;
    EarthLocation nw, ne, se, sw;

    //don: this  what I added
    Double north = (Double) imageProps.get(ATTR_NORTH);
    Double south = (Double) imageProps.get(ATTR_SOUTH);
    Double east = (Double) imageProps.get(ATTR_EAST);
    Double west = (Double) imageProps.get(ATTR_WEST);
    //Assume if we have one we have them all
    if (north != null) {
        nw = DisplayControlImpl.makeEarthLocation(north.doubleValue(), west.doubleValue(), 0);

        ne = DisplayControlImpl.makeEarthLocation(north.doubleValue(), east.doubleValue(), 0);
        sw = DisplayControlImpl.makeEarthLocation(south.doubleValue(), west.doubleValue(), 0);
        se = DisplayControlImpl.makeEarthLocation(south.doubleValue(), east.doubleValue(), 0);

    } else {
        nw = display.screenToEarthLocation(0, 0);
        ne = display.screenToEarthLocation(width, 0);
        se = display.screenToEarthLocation(0, height);
        sw = display.screenToEarthLocation(width, height);
    }

    double widthDegrees = ne.getLongitude().getValue() - nw.getLongitude().getValue();
    double heightDegrees = ne.getLatitude().getValue() - se.getLatitude().getValue();

    Insets insets = getInsets(child, 0);
    int delta = 2;
    int bgPad = 1;

    image = doMatte(image, child, 0);

    Graphics2D g = (Graphics2D) image.getGraphics();
    g.setFont(getFont(child));
    FontMetrics fm = g.getFontMetrics();

    int lineOffsetRight = applyMacros(child, ATTR_LINEOFFSET_RIGHT, 0);
    int lineOffsetLeft = applyMacros(child, ATTR_LINEOFFSET_LEFT, 0);
    int lineOffsetTop = applyMacros(child, ATTR_LINEOFFSET_TOP, 0);
    int lineOffsetBottom = applyMacros(child, ATTR_LINEOFFSET_BOTTOM, 0);

    Stroke lineStroke;
    if (XmlUtil.hasAttribute(child, ATTR_DASHES)) {
        lineStroke = new BasicStroke((float) applyMacros(child, ATTR_LINEWIDTH, 1.0), BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_BEVEL, 1.0f, Misc.parseFloats(applyMacros(child, ATTR_DASHES, "8")), 0.0f);
    } else {
        lineStroke = new BasicStroke((float) applyMacros(child, ATTR_LINEWIDTH, 1.0));
    }

    g.setStroke(lineStroke);
    double leftLon = nw.getLongitude().getValue(CommonUnit.degree);
    double rightLon = ne.getLongitude().getValue(CommonUnit.degree);
    Range lonRange = new Range(leftLon, rightLon);

    for (int i = 0; i < lonValues.length; i++) {
        double lon = GeoUtils.normalizeLongitude(lonRange, lonValues[i]);
        double percent = (lon - nw.getLongitude().getValue()) / widthDegrees;
        //            if(percent<0 || percent>1) continue;
        String label;
        if (i < lonLabels.size()) {
            label = lonLabels.get(i);
        } else {
            label = format.format(lonValues[i]);
        }
        Rectangle2D rect = fm.getStringBounds(label, g);
        int baseX = insets.left + (int) (percent * width);
        int x = baseX - (int) rect.getWidth() / 2;
        int topY;
        if (insets.top == 0) {
            topY = (int) rect.getHeight() + delta;
        } else {
            topY = insets.top - delta;
        }
        if (drawLonLines) {
            g.setColor(lineColor);
            g.drawLine(baseX, insets.top + lineOffsetTop, baseX, insets.top + height - lineOffsetBottom);
        }

        if (showTop) {
            if (bg != null) {
                g.setColor(bg);
                g.fillRect(x - bgPad, topY - (int) rect.getHeight() - bgPad, (int) rect.getWidth() + bgPad * 2,
                        (int) rect.getHeight() + bgPad * 2);
            }
            g.setColor(color);
            g.drawString(label, x, topY);
        }
        int bottomY;
        if (insets.bottom == 0) {
            bottomY = insets.top + height - delta;
        } else {
            bottomY = insets.top + height + (int) rect.getHeight() + delta;
        }
        if (showBottom) {
            if (bg != null) {
                g.setColor(bg);
                g.fillRect(x - bgPad, bottomY - (int) rect.getHeight() - bgPad,
                        (int) rect.getWidth() + bgPad * 2, (int) rect.getHeight() + bgPad * 2);
            }
            g.setColor(color);
            g.drawString(label, x, bottomY);
        }
    }

    for (int i = 0; i < latValues.length; i++) {
        double lat = latValues[i];
        double percent = 1.0 - (lat - se.getLatitude().getValue()) / heightDegrees;
        int baseY = insets.top + (int) (percent * height);
        //            if(percent<0 || percent>1) continue;
        String label;
        if (i < latLabels.size()) {
            label = latLabels.get(i);
        } else {
            label = format.format(lat);
        }
        Rectangle2D rect = fm.getStringBounds(label, g);
        int y = baseY + (int) rect.getHeight() / 2;
        int leftX;
        if (insets.left == 0) {
            leftX = 0 + delta;
        } else {
            leftX = insets.left - (int) rect.getWidth() - delta;
        }
        if (drawLonLines) {
            g.setColor(lineColor);
            g.drawLine(insets.left + lineOffsetRight, baseY, insets.left + width - lineOffsetRight, baseY);
        }

        if (showLeft) {
            if (bg != null) {
                g.setColor(bg);
                g.fillRect(leftX - bgPad, y - (int) rect.getHeight() - bgPad, (int) rect.getWidth() + bgPad * 2,
                        (int) rect.getHeight() + bgPad * 2);
            }
            g.setColor(color);
            g.drawString(label, leftX, y);
        }

        if (insets.right == 0) {
            leftX = insets.left + width - (int) rect.getWidth() - delta;
        } else {
            leftX = insets.left + width + delta;
        }
        if (showRight) {
            if (bg != null) {
                g.setColor(bg);
                g.fillRect(leftX - bgPad, y - (int) rect.getHeight() - bgPad, (int) rect.getWidth() + bgPad * 2,
                        (int) rect.getHeight() + bgPad * 2);
            }
            g.setColor(color);
            g.drawString(label, leftX, y);
        }
    }

    return image;

}

From source file:org.yccheok.jstock.gui.charting.ChartLayerUI.java

private void drawInformationBox(Graphics2D g2, JXLayer<? extends V> layer) {
    if (JStock.instance().getJStockOptions()
            .getYellowInformationBoxOption() == JStockOptions.YellowInformationBoxOption.Hide) {
        return;/*from ww  w  .j  a v a2s  . c  o m*/
    }

    final Font oldFont = g2.getFont();
    final Font paramFont = oldFont;
    final FontMetrics paramFontMetrics = g2.getFontMetrics(paramFont);
    final Font valueFont = oldFont.deriveFont(oldFont.getStyle() | Font.BOLD, (float) oldFont.getSize() + 1);
    final FontMetrics valueFontMetrics = g2.getFontMetrics(valueFont);
    final Font dateFont = oldFont.deriveFont((float) oldFont.getSize() - 1);
    final FontMetrics dateFontMetrics = g2.getFontMetrics(dateFont);

    final List<ChartData> chartDatas = this.chartJDialog.getChartDatas();
    List<String> values = new ArrayList<String>();
    final ChartData chartData = chartDatas.get(this.mainTraceInfo.getDataIndex());

    // Number formats are generally not synchronized. It is recommended to create separate format instances for each thread. 
    // If multiple threads access a format concurrently, it must be synchronized externally.
    // http://stackoverflow.com/questions/2213410/usage-of-decimalformat-for-the-following-case
    final DecimalFormat integerFormat = new DecimalFormat("###,###");

    // It is common to use OHLC for chat, instead of using PrevPrice.        
    values.add(org.yccheok.jstock.gui.Utils.stockPriceDecimalFormat(chartData.openPrice));
    values.add(org.yccheok.jstock.gui.Utils.stockPriceDecimalFormat(chartData.highPrice));
    values.add(org.yccheok.jstock.gui.Utils.stockPriceDecimalFormat(chartData.lowPrice));
    values.add(org.yccheok.jstock.gui.Utils.stockPriceDecimalFormat(chartData.lastPrice));
    values.add(integerFormat.format(chartData.volume));

    final List<String> indicatorParams = new ArrayList<String>();
    final List<String> indicatorValues = new ArrayList<String>();
    final DecimalFormat decimalFormat = new DecimalFormat("0.00");
    for (TraceInfo indicatorTraceInfo : this.indicatorTraceInfos) {
        final int plotIndex = indicatorTraceInfo.getPlotIndex();
        final int seriesIndex = indicatorTraceInfo.getSeriesIndex();
        final int dataIndex = indicatorTraceInfo.getDataIndex();
        final String name = this.getLegendName(plotIndex, seriesIndex);
        final Number value = this.getValue(plotIndex, seriesIndex, dataIndex);
        if (name == null || value == null) {
            continue;
        }
        indicatorParams.add(name);
        indicatorValues.add(decimalFormat.format(value));
    }

    assert (values.size() == params.size());
    int index = 0;
    final int paramValueWidthMargin = 10;
    final int paramValueHeightMargin = 0;
    // Slightly larger than dateInfoHeightMargin, as font for indicator is
    // larger than date's.
    final int infoIndicatorHeightMargin = 8;
    int maxInfoWidth = -1;
    // paramFontMetrics will always "smaller" than valueFontMetrics.
    int totalInfoHeight = Math.max(paramFontMetrics.getHeight(), valueFontMetrics.getHeight()) * values.size()
            + paramValueHeightMargin * (values.size() - 1);
    for (String param : params) {
        final String value = values.get(index++);
        final int paramStringWidth = paramFontMetrics.stringWidth(param + ":") + paramValueWidthMargin
                + valueFontMetrics.stringWidth(value);
        if (maxInfoWidth < paramStringWidth) {
            maxInfoWidth = paramStringWidth;
        }
    }

    if (indicatorValues.size() > 0) {
        totalInfoHeight += infoIndicatorHeightMargin;
        totalInfoHeight += Math.max(paramFontMetrics.getHeight(), valueFontMetrics.getHeight())
                * indicatorValues.size() + paramValueHeightMargin * (indicatorValues.size() - 1);
        index = 0;
        for (String indicatorParam : indicatorParams) {
            final String indicatorValue = indicatorValues.get(index++);
            final int paramStringWidth = paramFontMetrics.stringWidth(indicatorParam + ":")
                    + paramValueWidthMargin + valueFontMetrics.stringWidth(indicatorValue);
            if (maxInfoWidth < paramStringWidth) {
                maxInfoWidth = paramStringWidth;
            }
        }
    }

    final Date date = new Date(chartData.timestamp);

    // Date formats are not synchronized. It is recommended to create separate format instances for each thread.
    // If multiple threads access a format concurrently, it must be synchronized externally.
    final SimpleDateFormat simpleDateFormat = this.simpleDataFormatThreadLocal.get();
    final String dateString = simpleDateFormat.format(date);
    final int dateStringWidth = dateFontMetrics.stringWidth(dateString);
    final int dateStringHeight = dateFontMetrics.getHeight();
    // We want to avoid information box from keep changing its width while
    // user moves along the mouse. This will prevent user from feeling,
    // information box is flickering, which is uncomfortable to user's eye.
    final int maxStringWidth = Math.max(dateFontMetrics.stringWidth(longDateString),
            Math.max(this.maxWidth, Math.max(dateStringWidth, maxInfoWidth)));
    if (maxStringWidth > this.maxWidth) {
        this.maxWidth = maxStringWidth;
    }
    final int dateInfoHeightMargin = 5;
    final int maxStringHeight = dateStringHeight + dateInfoHeightMargin + totalInfoHeight;

    final int padding = 5;
    final int boxPointMargin = 8;
    final int width = maxStringWidth + (padding << 1);
    final int height = maxStringHeight + (padding << 1);

    /* Get Border Rect Information. */
    /*
    fillRect(1, 1, 1, 1);   // O is rect pixel
            
    xxx
    xOx
    xxx
            
    drawRect(0, 0, 2, 2);   // O is rect pixel
            
    OOO
    OxO
    OOO
     */
    final int borderWidth = width + 2;
    final int borderHeight = height + 2;
    // On left side of the ball.
    final double suggestedBorderX = this.mainTraceInfo.getPoint().getX() - borderWidth - boxPointMargin;
    final double suggestedBorderY = this.mainTraceInfo.getPoint().getY() - (borderHeight >> 1);
    double bestBorderX = 0;
    double bestBorderY = 0;
    if (JStock.instance().getJStockOptions()
            .getYellowInformationBoxOption() == JStockOptions.YellowInformationBoxOption.Stay) {
        if (this.mainTraceInfo.getPoint()
                .getX() > ((int) (this.mainDrawArea.getX() + this.mainDrawArea.getWidth() + 0.5) >> 1)) {
            bestBorderX = this.mainDrawArea.getX();
            bestBorderY = this.mainDrawArea.getY();
        } else {
            bestBorderX = this.mainDrawArea.getX() + this.mainDrawArea.getWidth() - borderWidth;
            bestBorderY = this.mainDrawArea.getY();
        }
    } else {
        assert (JStock.instance().getJStockOptions()
                .getYellowInformationBoxOption() == JStockOptions.YellowInformationBoxOption.Follow);
        bestBorderX = suggestedBorderX > this.mainDrawArea.getX()
                ? (suggestedBorderX + borderWidth) < (this.mainDrawArea.getX() + this.mainDrawArea.getWidth())
                        ? suggestedBorderX
                        : this.mainDrawArea.getX() + this.mainDrawArea.getWidth() - borderWidth - boxPointMargin
                : this.mainTraceInfo.getPoint().getX() + boxPointMargin;
        bestBorderY = suggestedBorderY > this.mainDrawArea.getY()
                ? (suggestedBorderY + borderHeight) < (this.mainDrawArea.getY() + this.mainDrawArea.getHeight())
                        ? suggestedBorderY
                        : this.mainDrawArea.getY() + this.mainDrawArea.getHeight() - borderHeight
                                - boxPointMargin
                : this.mainDrawArea.getY() + boxPointMargin;
    }

    final double x = bestBorderX + 1;
    final double y = bestBorderY + 1;

    final Object oldValueAntiAlias = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
    final Composite oldComposite = g2.getComposite();
    final Color oldColor = g2.getColor();

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setColor(COLOR_BORDER);
    g2.drawRoundRect((int) (bestBorderX + 0.5), (int) (bestBorderY + 0.5), borderWidth - 1, borderHeight - 1,
            15, 15);
    g2.setColor(COLOR_BACKGROUND);
    g2.setComposite(Utils.makeComposite(0.75f));
    g2.fillRoundRect((int) (x + 0.5), (int) (y + 0.5), width, height, 15, 15);
    g2.setComposite(oldComposite);
    g2.setColor(oldColor);

    int yy = (int) (y + padding + dateFontMetrics.getAscent() + 0.5);
    g2.setFont(dateFont);
    g2.setColor(COLOR_BLUE);
    g2.drawString(dateString, (int) (((width - dateFontMetrics.stringWidth(dateString)) >> 1) + x + 0.5), yy);

    index = 0;
    yy += dateFontMetrics.getDescent() + dateInfoHeightMargin + valueFontMetrics.getAscent();
    final String CLOSE_STR = GUIBundle.getString("StockHistory_Close");
    for (String param : params) {
        final String value = values.get(index++);
        g2.setColor(Color.BLACK);
        if (param.equals(CLOSE_STR)) {
            // It is common to use OHLC for chat, instead of using PrevPrice.
            final double changePrice = chartData.lastPrice - chartData.openPrice;
            if (changePrice > 0.0) {
                g2.setColor(JStockOptions.DEFAULT_HIGHER_NUMERICAL_VALUE_FOREGROUND_COLOR);
            } else if (changePrice < 0.0) {
                g2.setColor(JStockOptions.DEFAULT_LOWER_NUMERICAL_VALUE_FOREGROUND_COLOR);
            }
        }
        g2.setFont(paramFont);
        g2.drawString(param + ":", (int) (padding + x + 0.5), yy);
        g2.setFont(valueFont);
        g2.drawString(value, (int) (width - padding - valueFontMetrics.stringWidth(value) + x + 0.5), yy);
        // Same as yy += valueFontMetrics.getDescent() + paramValueHeightMargin + valueFontMetrics.getAscent()
        yy += paramValueHeightMargin + valueFontMetrics.getHeight();
    }

    g2.setColor(Color.BLACK);
    yy -= paramValueHeightMargin;
    yy += infoIndicatorHeightMargin;

    index = 0;
    for (String indicatorParam : indicatorParams) {
        final String indicatorValue = indicatorValues.get(index++);
        g2.setFont(paramFont);
        g2.drawString(indicatorParam + ":", (int) (padding + x + 0.5), yy);
        g2.setFont(valueFont);
        g2.drawString(indicatorValue,
                (int) (width - padding - valueFontMetrics.stringWidth(indicatorValue) + x + 0.5), yy);
        // Same as yy += valueFontMetrics.getDescent() + paramValueHeightMargin + valueFontMetrics.getAscent()
        yy += paramValueHeightMargin + valueFontMetrics.getHeight();
    }

    g2.setColor(oldColor);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldValueAntiAlias);
    g2.setFont(oldFont);
}

From source file:fr.fg.server.core.TerritoryManager.java

private static BufferedImage createTerritoryMap(int idSector) {
    List<Area> areas = new ArrayList<Area>(DataAccess.getAreasBySector(idSector));

    float[][] points = new float[areas.size()][2];
    int[] dominatingAllies = new int[areas.size()];
    int i = 0;/*ww  w.jav  a2 s.  c  o m*/
    for (Area area : areas) {
        points[i][0] = area.getX() * MAP_SCALE;
        points[i][1] = area.getY() * MAP_SCALE;
        dominatingAllies[i] = area.getIdDominatingAlly();
        i++;
    }

    Hull hull = new Hull(points);
    MPolygon hullPolygon = hull.getRegion();
    float[][] newPoints = new float[points.length + hullPolygon.count()][2];
    System.arraycopy(points, 0, newPoints, 0, points.length);

    float[][] hullCoords = hullPolygon.getCoords();

    for (i = 0; i < hullPolygon.count(); i++) {
        double angle = Math.atan2(hullCoords[i][1], hullCoords[i][0]);
        double length = Math.sqrt(hullCoords[i][0] * hullCoords[i][0] + hullCoords[i][1] * hullCoords[i][1]);

        newPoints[i + points.length][0] = (float) (Math.cos(angle) * (length + 8 * MAP_SCALE));
        newPoints[i + points.length][1] = (float) (Math.sin(angle) * (length + 8 * MAP_SCALE));
    }

    points = newPoints;

    Voronoi voronoi = new Voronoi(points);
    Delaunay delaunay = new Delaunay(points);

    // Dcoupage en rgions
    MPolygon[] regions = voronoi.getRegions();

    // Calcule le rayon de la galaxie
    int radius = 0;

    for (Area area : areas) {
        radius = Math.max(radius, area.getX() * area.getX() + area.getY() * area.getY());
    }

    radius = (int) Math.floor(Math.sqrt(radius) * MAP_SCALE) + 10 * MAP_SCALE;
    int diameter = 2 * radius + 1;

    // Construit l'image avec les quadrants
    BufferedImage territoriesImage = new BufferedImage(diameter, diameter, BufferedImage.TYPE_INT_ARGB);

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

    // Affecte une couleur  chaque alliance
    HashMap<Integer, Color> alliesColors = new HashMap<Integer, Color>();

    for (Area area : areas) {
        int idDominatingAlly = area.getIdDominatingAlly();
        if (idDominatingAlly != 0)
            alliesColors.put(idDominatingAlly,
                    Ally.TERRITORY_COLORS[DataAccess.getAllyById(idDominatingAlly).getColor()]);
    }

    Polygon[] polygons = new Polygon[regions.length];
    for (i = 0; i < areas.size(); i++) {
        if (dominatingAllies[i] != 0) {
            polygons[i] = createPolygon(regions[i].getCoords(), radius + 1, 3);
        }
    }

    // Dessine tous les secteurs
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);

    for (i = 0; i < areas.size(); i++) {
        if (dominatingAllies[i] == 0)
            continue;

        Polygon p = polygons[i];

        // Dessine le polygone
        g.setColor(alliesColors.get(dominatingAllies[i]));
        g.fill(p);

        // Rempli les espaces entre les polygones adjacents qui
        // correspondent au territoire d'une mme alliance
        int[] linkedRegions = delaunay.getLinked(i);
        for (int j = 0; j < linkedRegions.length; j++) {
            int linkedRegion = linkedRegions[j];

            if (linkedRegion >= areas.size())
                continue;

            if (dominatingAllies[i] == dominatingAllies[linkedRegion]) {
                if (linkedRegion <= i)
                    continue;

                float[][] coords1 = regions[i].getCoords();
                float[][] coords2 = regions[linkedRegion].getCoords();

                int junctionIndex = 0;
                int[][] junctions = new int[2][2];

                search: for (int k = 0; k < coords1.length; k++) {
                    for (int l = 0; l < coords2.length; l++) {
                        if (coords1[k][0] == coords2[l][0] && coords1[k][1] == coords2[l][1]) {
                            junctions[junctionIndex][0] = k;
                            junctions[junctionIndex][1] = l;

                            junctionIndex++;

                            if (junctionIndex == 2) {
                                int[] xpts = new int[] { polygons[i].xpoints[junctions[0][0]],
                                        polygons[linkedRegion].xpoints[junctions[0][1]],
                                        polygons[linkedRegion].xpoints[junctions[1][1]],
                                        polygons[i].xpoints[junctions[1][0]], };
                                int[] ypts = new int[] { polygons[i].ypoints[junctions[0][0]],
                                        polygons[linkedRegion].ypoints[junctions[0][1]],
                                        polygons[linkedRegion].ypoints[junctions[1][1]],
                                        polygons[i].ypoints[junctions[1][0]], };

                                Polygon border = new Polygon(xpts, ypts, 4);
                                g.setStroke(new BasicStroke(2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
                                g.fill(border);
                                g.draw(border);
                                break search;
                            }
                            break;
                        }
                    }
                }
            }
        }
    }

    // Dessine des lignes de contours des territoires
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    for (i = 0; i < areas.size(); i++) {
        if (dominatingAllies[i] == 0)
            continue;

        g.setStroke(new BasicStroke(1.5f));
        g.setColor(alliesColors.get(dominatingAllies[i]).brighter().brighter());

        float[][] coords1 = regions[i].getCoords();

        lines: for (int j = 0; j < coords1.length; j++) {
            int[] linkedRegions = delaunay.getLinked(i);
            for (int k = 0; k < linkedRegions.length; k++) {
                int linkedRegion = linkedRegions[k];

                if (linkedRegion >= areas.size())
                    continue;

                if (dominatingAllies[i] == dominatingAllies[linkedRegion]) {
                    float[][] coords2 = regions[linkedRegion].getCoords();

                    for (int m = 0; m < coords2.length; m++) {
                        if (coords1[j][0] == coords2[m][0] && coords1[j][1] == coords2[m][1]
                                && ((coords1[(j + 1) % coords1.length][0] == coords2[(m + 1)
                                        % coords2.length][0]
                                        && coords1[(j + 1) % coords1.length][1] == coords2[(m + 1)
                                                % coords2.length][1])
                                        || (coords1[(j + 1)
                                                % coords1.length][0] == coords2[(m - 1 + coords2.length)
                                                        % coords2.length][0]
                                                && coords1[(j + 1)
                                                        % coords1.length][1] == coords2[(m - 1 + coords2.length)
                                                                % coords2.length][1]))) {
                            continue lines;
                        }
                    }
                }
            }

            g.drawLine(Math.round(polygons[i].xpoints[j]), Math.round(polygons[i].ypoints[j]),
                    Math.round(polygons[i].xpoints[(j + 1) % coords1.length]),
                    Math.round(polygons[i].ypoints[(j + 1) % coords1.length]));
        }

        for (int j = 0; j < coords1.length; j++) {
            int neighbours = 0;
            int lastNeighbourRegion = -1;
            int neighbourCoordsIndex = -1;

            int[] linkedRegions = delaunay.getLinked(i);
            for (int k = 0; k < linkedRegions.length; k++) {
                int linkedRegion = linkedRegions[k];

                if (linkedRegion >= areas.size())
                    continue;

                if (dominatingAllies[i] == dominatingAllies[linkedRegion]) {
                    float[][] coords2 = regions[linkedRegion].getCoords();

                    for (int m = 0; m < coords2.length; m++) {
                        if (coords1[j][0] == coords2[m][0] && coords1[j][1] == coords2[m][1]) {
                            neighbours++;
                            lastNeighbourRegion = linkedRegion;
                            neighbourCoordsIndex = m;
                            break;
                        }
                    }
                }
            }

            if (neighbours == 1) {
                g.drawLine(Math.round(polygons[i].xpoints[j]), Math.round(polygons[i].ypoints[j]),
                        Math.round(polygons[lastNeighbourRegion].xpoints[neighbourCoordsIndex]),
                        Math.round(polygons[lastNeighbourRegion].ypoints[neighbourCoordsIndex]));
            }
        }
    }

    BufferedImage finalImage = new BufferedImage(diameter, diameter, BufferedImage.TYPE_INT_ARGB);

    g = (Graphics2D) finalImage.getGraphics();

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, .15f));
    g.drawImage(territoriesImage, 0, 0, null);
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, .5f));

    // Charge la police pour afficher le nom des alliances
    try {
        Font textFont = Font.createFont(Font.TRUETYPE_FONT,
                Action.class.getClassLoader().getResourceAsStream("fr/fg/server/resources/TinDog.ttf"));
        textFont = textFont.deriveFont(12f).deriveFont(Font.BOLD);
        g.setFont(textFont);
    } catch (Exception e) {
        LoggingSystem.getServerLogger().warn("Could not load quadrant map font.", e);
    }
    FontMetrics fm = g.getFontMetrics();

    ArrayList<Integer> closedRegions = new ArrayList<Integer>();

    for (i = 0; i < areas.size(); i++) {
        if (dominatingAllies[i] == 0 || closedRegions.contains(i))
            continue;

        ArrayList<Integer> allyRegions = new ArrayList<Integer>();
        ArrayList<Integer> openRegions = new ArrayList<Integer>();

        openRegions.add(i);

        while (openRegions.size() > 0) {
            int currentRegion = openRegions.remove(0);
            allyRegions.add(currentRegion);
            closedRegions.add(currentRegion);

            int[] linkedRegions = delaunay.getLinked(currentRegion);

            for (int k = 0; k < linkedRegions.length; k++) {
                int linkedRegion = linkedRegions[k];

                if (linkedRegion >= areas.size() || openRegions.contains(linkedRegion)
                        || allyRegions.contains(linkedRegion))
                    continue;

                if (dominatingAllies[i] == dominatingAllies[linkedRegion])
                    openRegions.add(linkedRegion);
            }
        }

        Area area = areas.get(i);
        long xsum = 0;
        long ysum = 0;

        for (int k = 0; k < allyRegions.size(); k++) {
            int allyRegion = allyRegions.get(k);
            area = areas.get(allyRegion);

            xsum += area.getX();
            ysum += area.getY();
        }

        int x = (int) (xsum / allyRegions.size()) * MAP_SCALE + radius + 1;
        int y = (int) (-ysum / allyRegions.size()) * MAP_SCALE + radius + 1;
        ;

        Point point = new Point(x, y);
        boolean validLocation = false;
        for (int k = 0; k < allyRegions.size(); k++) {
            int allyRegion = allyRegions.get(k);

            if (polygons[allyRegion].contains(point)) {
                validLocation = true;
                break;
            }
        }

        if (validLocation) {
            if (allyRegions.size() == 1)
                y -= 14;
        } else {
            int xmid = (int) (xsum / allyRegions.size());
            int ymid = (int) (ysum / allyRegions.size());

            area = areas.get(i);
            int dx = area.getX() - xmid;
            int dy = area.getY() - ymid;
            int distance = dx * dx + dy * dy;

            int nearestAreaIndex = i;
            int nearestDistance = distance;

            for (int k = 0; k < allyRegions.size(); k++) {
                int allyRegion = allyRegions.get(k);

                area = areas.get(allyRegion);
                dx = area.getX() - xmid;
                dy = area.getY() - ymid;
                distance = dx * dx + dy * dy;

                if (distance < nearestDistance) {
                    nearestAreaIndex = allyRegion;
                    nearestDistance = distance;
                }
            }

            area = areas.get(nearestAreaIndex);
            x = area.getX() * MAP_SCALE + radius + 1;
            y = -area.getY() * MAP_SCALE + radius - 13;
        }

        // Dessine le tag de l'alliance
        String allyTag = "[ " + DataAccess.getAllyById(dominatingAllies[i]).getTag() + " ]";
        g.setColor(Color.BLACK);
        g.drawString(allyTag, x - fm.stringWidth(allyTag) / 2 + 1, y);
        g.setColor(alliesColors.get(dominatingAllies[i]));
        g.drawString(allyTag, x - fm.stringWidth(allyTag) / 2, y);
    }

    return finalImage;
}