Example usage for java.awt Graphics2D setStroke

List of usage examples for java.awt Graphics2D setStroke

Introduction

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

Prototype

public abstract void setStroke(Stroke s);

Source Link

Document

Sets the Stroke for the Graphics2D context.

Usage

From source file:aprofplot.jfreechart.SamplingXYLineAndShapeRenderer.java

protected void drawSeriesLine(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea,
        PlotRenderingInfo info, XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset,
        int series, CrosshairState crosshairState, int pass) {

    State s = (State) state;
    PlotOrientation orientation = plot.getOrientation();
    RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
    double lowestVisibleX = domainAxis.getLowerBound();
    double highestVisibleX = domainAxis.getUpperBound();
    double width = (orientation == PlotOrientation.HORIZONTAL) ? dataArea.getHeight() : dataArea.getWidth();
    double dX = (highestVisibleX - lowestVisibleX) / width * lineWidth;
    double lowestVisibleY = rangeAxis.getLowerBound();
    double highestVisibleY = rangeAxis.getUpperBound();

    double lastX = Double.NEGATIVE_INFINITY;
    double lastY = 0.0;
    double highY = 0.0;
    double lowY = 0.0;
    double closeY = 0.0;
    boolean lastIntervalDone = false;
    boolean currentPointVisible = false;
    boolean lastPointVisible = false;
    boolean lastPointGood = false;
    boolean lastPointInInterval = false;
    int intervalCount = 0;
    int badPoints = 0;
    for (int itemIndex = state.getFirstItemIndex(); itemIndex <= state.getLastItemIndex(); itemIndex++) {
        double x = dataset.getXValue(series, itemIndex);
        double y = dataset.getYValue(series, itemIndex);
        if (!Double.isNaN(x) && !Double.isNaN(y)) {
            if ((Math.abs(x - lastX) > dX)) {
                //System.out.println("Breakpoint 1: leaving interval");
                //in any case, add the interval that we are about to leave to the intervalPath
                float intervalStartX = 0.0f;
                float intervalEndX = 0.0f;
                float intervalStartY = 0.0f;
                float intervalEndY = 0.0f;
                float currentX = 0.0f;
                float currentY = 0.0f;
                float lastFX = 0.0f;
                float lastFY = 0.0f;

                //first set some variables
                if (orientation == PlotOrientation.VERTICAL) {
                    intervalStartX = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
                    intervalEndX = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
                    intervalStartY = (float) rangeAxis.valueToJava2D(lowY, dataArea, yAxisLocation);
                    intervalEndY = (float) rangeAxis.valueToJava2D(highY, dataArea, yAxisLocation);
                    currentX = (float) domainAxis.valueToJava2D(x, dataArea, xAxisLocation);
                    lastFX = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
                    currentY = (float) rangeAxis.valueToJava2D(y, dataArea, yAxisLocation);
                    lastFY = (float) rangeAxis.valueToJava2D(closeY, dataArea, yAxisLocation);
                } else {
                    intervalStartX = (float) rangeAxis.valueToJava2D(lowY, dataArea, yAxisLocation);
                    intervalEndX = (float) rangeAxis.valueToJava2D(highY, dataArea, yAxisLocation);
                    intervalStartY = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
                    intervalEndY = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
                    currentX = (float) rangeAxis.valueToJava2D(y, dataArea, yAxisLocation);
                    lastFX = (float) rangeAxis.valueToJava2D(closeY, dataArea, yAxisLocation);
                    currentY = (float) domainAxis.valueToJava2D(x, dataArea, xAxisLocation);
                    lastFY = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
                }/*from w ww .j  a  va 2 s .  co m*/
                if ((lowY - highY) < 1) {
                    //System.out.println("Breakpoint 2: setting lastPointInInterval");
                    lastPointInInterval = false;
                }
                //System.out.println("Breakpoint 3: lastPointInInterval: " +lastPointInInterval);
                if ((lowY < highY)) {
                    intervalCount++;
                    //System.out.println("Breakpoint 4: adding segment to interval path:" );
                    //System.out.println("xStart" + intervalStartX + ", yStart " + intervalStartY + ", xEnd " + intervalEndX + ", yEnd " + intervalEndY);
                    s.intervalPath.moveTo(intervalStartX, intervalStartY);
                    s.intervalPath.lineTo(intervalEndX, intervalEndY);
                    lastIntervalDone = true;
                }

                //now the series path
                currentPointVisible = ((x >= lowestVisibleX) && (x <= highestVisibleX) && (y >= lowestVisibleY)
                        && (y <= highestVisibleY));
                if (!lastPointGood) {//last point not valid --
                    badPoints++;
                    if (currentPointVisible) {//--> if the current position is visible move seriesPath cursor to the current position
                        s.seriesPath.moveTo(currentX, currentY);
                    }
                } else {//last point valid
                    //if the last point was visible and not part of an interval,
                    //we have already moved the seriesPath cursor to the last point, either with or without drawingh a line
                    //thus we only need to draw a line to the current position
                    if (lastPointVisible && !lastPointInInterval) {
                        s.seriesPath.lineTo(currentX, currentY);
                    } //if the last point was not visible or part of an interval, we have just stored the y values of the last point
                      //and not yet moved the seriesPath cursor. Thus, we need to move the cursor to the last point without drawing
                      //and draw a line to the current position.
                    else {
                        s.seriesPath.moveTo(lastFX, lastFY);
                        s.seriesPath.lineTo(currentX, currentY);
                    }
                }
                lastPointVisible = currentPointVisible;
                lastX = x;
                lastY = y;
                highY = y;
                lowY = y;
                closeY = y;
                lastPointInInterval = false;
            } else {
                lastIntervalDone = false;
                lastPointInInterval = true;
                highY = Math.max(highY, y);
                lowY = Math.min(lowY, y);
                closeY = y;
            }
            lastPointGood = true;
        } else {
            lastPointGood = false;
        }
    }
    // if this is the last item, draw the path ...
    // draw path, but first check whether we need to complete an interval
    if (!lastIntervalDone) {
        if (lowY < highY) {
            float intervalStartX = 0.0f;
            float intervalEndX = 0.0f;
            float intervalStartY = 0.0f;
            float intervalEndY = 0.0f;
            if (orientation == PlotOrientation.VERTICAL) {
                intervalStartX = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
                intervalEndX = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
                intervalStartY = (float) rangeAxis.valueToJava2D(lowY, dataArea, yAxisLocation);
                intervalEndY = (float) rangeAxis.valueToJava2D(highY, dataArea, yAxisLocation);
            } else {
                intervalStartX = (float) rangeAxis.valueToJava2D(lowY, dataArea, yAxisLocation);
                intervalEndX = (float) rangeAxis.valueToJava2D(highY, dataArea, yAxisLocation);
                intervalStartY = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
                intervalEndY = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
            }
            intervalCount++;
            s.intervalPath.moveTo(intervalStartX, intervalStartY);
            s.intervalPath.lineTo(intervalEndX, intervalEndY);
        }
    }
    PathIterator pi = s.seriesPath.getPathIterator(null);
    g2.setStroke(getItemStroke(series, 0));
    g2.setPaint(getItemPaint(series, 0));
    g2.draw(s.seriesPath);
    g2.draw(s.intervalPath);
    //System.out.println("Interval count " + intervalCount);
    //System.out.println("Bad points " + badPoints);
}

From source file:org.objectweb.proactive.extensions.timitspmd.util.charts.renderer.HierarchicalBarRenderer.java

/**
 * Draws the bar for one item in the dataset.
 *
 * @param g2/*w  w w .j ava  2  s .c  o  m*/
 *            the graphics device.
 * @param state
 *            the renderer state.
 * @param dataArea
 *            the plot area.
 * @param plot
 *            the plot.
 * @param domainAxis
 *            the domain (category) axis.
 * @param rangeAxis
 *            the range (value) axis.
 * @param data
 *            the data.
 * @param row
 *            the row index (zero-based).
 * @param column
 *            the column index (zero-based).
 * @param pass
 *            the pass index.
 */
@Override
public void drawItem(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea, CategoryPlot plot,
        CategoryAxis domainAxis, ValueAxis rangeAxis, CategoryDataset data, int row, int column, int pass) {
    // nothing is drawn for null values...
    Number dataValue = data.getValue(row, column);
    if (dataValue == null) {
        return;
    }

    // BAR X
    double rectX = domainAxis.getCategoryMiddle(column, this.getColumnCount(), dataArea,
            plot.getDomainAxisEdge()) - (state.getBarWidth() / 2.0);

    int seriesCount = this.getRowCount();

    // BAR Y
    double value = dataValue.doubleValue();
    double base = 0.0;
    double lclip = this.getLowerClip();
    double uclip = this.getUpperClip();

    if (uclip <= 0.0) { // cases 1, 2, 3 and 4
        if (value >= uclip) {
            return; // bar is not visible
        }
        base = uclip;
        if (value <= lclip) {
            value = lclip;
        }
    } else if (lclip <= 0.0) { // cases 5, 6, 7 and 8
        if (value >= uclip) {
            value = uclip;
        } else {
            if (value <= lclip) {
                value = lclip;
            }
        }
    } else { // cases 9, 10, 11 and 12
        if (value <= lclip) {
            return; // bar is not visible
        }
        base = this.getLowerClip();
        if (value >= uclip) {
            value = uclip;
        }
    }

    RectangleEdge edge = plot.getRangeAxisEdge();
    double transY1 = rangeAxis.valueToJava2D(base, dataArea, edge);
    double transY2 = rangeAxis.valueToJava2D(value, dataArea, edge);
    double rectY = Math.min(transY2, transY1);

    double rectWidth = state.getBarWidth();
    double rectHeight = Math.abs(transY2 - transY1);

    // draw the bar...
    double shift = 0.0;
    rectWidth = 0.0;
    double widthFactor = 1.0;
    double seriesBarWidth = this.getSeriesBarWidth(row);
    if (!Double.isNaN(seriesBarWidth)) {
        widthFactor = seriesBarWidth;
    }
    rectWidth = widthFactor * state.getBarWidth();
    rectX = rectX + (((1 - widthFactor) * state.getBarWidth()) / 2.0);
    if (seriesCount > 1) {
        // needs to be improved !!!
        shift = (rectWidth * 0.20) / (seriesCount - 1);
    }

    Rectangle2D bar = new Rectangle2D.Double((rectX + ((seriesCount - 1 - row) * shift)), rectY,
            (rectWidth - ((seriesCount - 1 - row) * shift * 2)), rectHeight);

    double rrX;
    double rrY;
    double rrW;
    double rrH;
    if (row == 0) {
        @SuppressWarnings("unchecked")
        Iterator it = this.datasetTree[column].getDescendants();
        int numElement = -1;
        while (it.hasNext()) {
            try {
                Element elt = (Element) it.next();
                numElement++;
                String name = elt.getAttributeValue("name");
                dataValue = Double.valueOf(elt.getAttributeValue("avg"));
                // System.out.println("["+column+"] "+name+" \t-->
                // "+dataValue);
                // BAR X
                rectX = domainAxis.getCategoryMiddle(column, this.getColumnCount(), dataArea,
                        plot.getDomainAxisEdge()) - (state.getBarWidth() / 2.0);

                seriesCount = this.getRowCount();

                // BAR Y
                value = dataValue.doubleValue();
                base = 0.0;
                lclip = this.getLowerClip();
                uclip = this.getUpperClip();

                if (uclip <= 0.0) { // cases 1, 2, 3 and 4
                    if (value >= uclip) {
                        return; // bar is not visible
                    }
                    base = uclip;
                    if (value <= lclip) {
                        value = lclip;
                    }
                } else if (lclip <= 0.0) { // cases 5, 6, 7 and 8
                    if (value >= uclip) {
                        value = uclip;
                    } else {
                        if (value <= lclip) {
                            value = lclip;
                        }
                    }
                } else { // cases 9, 10, 11 and 12
                    if (value <= lclip) {
                        return; // bar is not visible
                    }
                    base = this.getLowerClip();
                    if (value >= uclip) {
                        value = uclip;
                    }
                }

                edge = plot.getRangeAxisEdge();
                transY1 = rangeAxis.valueToJava2D(base, dataArea, edge);
                transY2 = rangeAxis.valueToJava2D(value, dataArea, edge);
                rectY = Math.min(transY2, transY1);

                rectWidth = state.getBarWidth();
                rectHeight = Math.abs(transY2 - transY1);

                // draw the bar...
                shift = 0.0;
                rectWidth = 0.0;
                widthFactor = 1.0;
                seriesBarWidth = this.getSeriesBarWidth(row);
                if (!Double.isNaN(seriesBarWidth)) {
                    widthFactor = seriesBarWidth;
                }
                rectWidth = widthFactor * state.getBarWidth();
                rectX = rectX + (((1 - widthFactor) * state.getBarWidth()) / 2.0);
                if (seriesCount > 1) {
                    // needs to be improved !!!
                    shift = (rectWidth * 0.20) / (seriesCount - 1);
                }
                rrX = (rectX + ((seriesCount - 1 - row) * shift));
                rrY = rectY;
                rrW = (rectWidth - ((seriesCount - 1 - row) * shift * 2));
                rrH = rectHeight;

                // IMPORTANT NOTE :
                // dev attribute is used to save width of the element
                // min attribute is used to save X position of the element
                // max attribute is used to save the number of child already
                // managed
                if (numElement == 0) {
                    elt.setAttribute("dev", "" + rrW);
                    elt.setAttribute("min", "" + rrX);
                    elt.setAttribute("max", "0");
                } else {
                    Element parent = elt.getParentElement();

                    // System.out.println(" Parent
                    // "+parent.getAttributeValue("name")
                    // + " rrX/rrW/child -> "
                    // + parent.getAttributeValue("min")+"/"
                    // + parent.getAttributeValue("dev")+"/"
                    // + parent.getAttributeValue("max") );
                    double pW = Double.valueOf(parent.getAttributeValue("dev"));
                    double pX = Double.valueOf(parent.getAttributeValue("min"));
                    int numChild = Integer.valueOf(parent.getAttributeValue("max"));

                    rrW = pW / parent.getChildren().size();
                    rrX = pX + (rrW * numChild);
                    rrX += HierarchicalBarRenderer.INCLUSION_MARGIN;
                    rrW -= (HierarchicalBarRenderer.INCLUSION_MARGIN * 2);
                    elt.setAttribute("dev", "" + rrW);
                    elt.setAttribute("min", "" + rrX);
                    parent.setAttribute("max", "" + (numChild + 1));
                }

                RoundRectangle2D rbar = new RoundRectangle2D.Double(rrX, rrY, rrW, rrH,
                        HierarchicalBarRenderer.CORNER, HierarchicalBarRenderer.CORNER);

                Rectangle2D childSumLine = null;
                double childSum = Double.valueOf(elt.getAttributeValue("sum"));
                transY1 = rangeAxis.valueToJava2D(base, dataArea, edge);
                transY2 = rangeAxis.valueToJava2D(childSum, dataArea, edge);
                rectY = Math.min(transY2, transY1);

                childSum = (childSum / dataValue.doubleValue()) * rrH;
                if ((childSum < rrH) && (childSum > 0) && ((childSum / rrH) < 0.95)) {
                    childSumLine = new Rectangle2D.Double(rrX, rectY, rrW, 1);
                }
                Paint itemPaint = this.getItemPaintFromName(name, this.series, column);
                GradientPaintTransformer t = this.getGradientPaintTransformer();
                if ((t != null) && itemPaint instanceof GradientPaint) {
                    itemPaint = t.transform((GradientPaint) itemPaint, bar);
                }
                g2.setPaint(itemPaint);

                Color c = g2.getColor();
                g2.setColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), this.alpha));
                g2.fill(rbar);
                g2.setColor(Color.DARK_GRAY);
                if (childSumLine != null) {
                    g2.fill(childSumLine);
                }

                // draw the outline...
                if (this.isDrawBarOutline()
                        && (state.getBarWidth() > BarRenderer.BAR_OUTLINE_WIDTH_THRESHOLD)) {
                    Stroke stroke = this.getItemOutlineStroke(row, column);
                    Paint paint = this.getItemOutlinePaint(row, column);
                    if ((stroke != null) && (paint != null)) {
                        g2.setStroke(stroke);
                        g2.setPaint(paint);
                        g2.draw(rbar);
                    }
                }
            } catch (ClassCastException e) {
                continue;
            }
        }
    }

    // ////////////////////////////

    // draw the item labels if there are any...
    double transX1 = rangeAxis.valueToJava2D(base, dataArea, edge);
    double transX2 = rangeAxis.valueToJava2D(value, dataArea, edge);

    CategoryItemLabelGenerator generator = this.getItemLabelGenerator(row, column);
    if ((generator != null) && this.isItemLabelVisible(row, column)) {
        this.drawItemLabel(g2, data, row, column, plot, generator, bar, (transX1 > transX2));
    }

    // collect entity and tool tip information...
    if (state.getInfo() != null) {
        EntityCollection entities = state.getEntityCollection();
        if (entities != null) {
            String tip = null;
            CategoryToolTipGenerator tipster = this.getToolTipGenerator(row, column);
            if (tipster != null) {
                tip = tipster.generateToolTip(data, row, column);
            }
            String url = null;
            if (this.getItemURLGenerator(row, column) != null) {
                url = this.getItemURLGenerator(row, column).generateURL(data, row, column);
            }
            CategoryItemEntity entity = new CategoryItemEntity(bar, tip, url, data, row,
                    data.getColumnKey(column), column);
            entities.add(entity);
        }
    }
}

From source file:lu.fisch.unimozer.Diagram.java

@Override
public void paint(Graphics graphics) {
    super.paint(graphics);
    Graphics2D g = (Graphics2D) graphics;
    // set anti-aliasing rendering
    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g.setFont(new Font(g.getFont().getFontName(), Font.PLAIN, Unimozer.DRAW_FONT_SIZE));

    // clear background
    g.setColor(Color.WHITE);//from  w w w .j av a2s  . com
    g.fillRect(0, 0, getWidth() + 1, getHeight() + 1);
    g.setColor(Color.BLACK);

    /*Set<String> set;
    Iterator<String> itr;
    // draw classes a first time
    for(MyClass clas : classes.values())
    {
      clas.setEnabled(this.isEnabled());
      clas.draw(graphics,showFields,showMethods);
    }*/

    /* let's try this one ... */
    for (Entry<String, MyClass> entry : classes.entrySet()) {
        // get the actual class ...
        MyClass clas = entry.getValue();
        clas.setEnabled(this.isEnabled());
        clas.draw(graphics, showFields, showMethods);
    }

    // draw packages
    packages.clear();
    for (MyClass myClass : classes.values()) {
        if (myClass.isDisplayUML()) {
            Package myPackage = null;
            if (!packages.containsKey(myClass.getPackagename())) {
                myPackage = new Package(myClass.getPackagename(), myClass.getPosition().y,
                        myClass.getPosition().x, myClass.getWidth(), myClass.getHeight());
                packages.put(myPackage.getName(), myPackage);
            } else
                myPackage = packages.get(myClass.getPackagename());

            if (myClass.getPosition().x + myClass.getWidth() > myPackage.getRight())
                myPackage.setRight(myClass.getPosition().x + myClass.getWidth());
            if (myClass.getPosition().y + myClass.getHeight() > myPackage.getBottom())
                myPackage.setBottom(myClass.getPosition().y + myClass.getHeight());

            if (myClass.getPosition().x < myPackage.getLeft())
                myPackage.setLeft(myClass.getPosition().x);
            if (myClass.getPosition().y < myPackage.getTop())
                myPackage.setTop(myClass.getPosition().y);
        }
    }

    // draw classes
    /*
    set = classes.keySet();
    itr = set.iterator();
    while (itr.hasNext())
    {
      String str = itr.next();
      classes.get(str).draw(graphics);
    }/**/

    mostRight = 0;
    mostBottom = 0;

    // ??
    /*
    set = classes.keySet();
    itr = set.iterator();
    while (itr.hasNext())
    {
      String str = itr.next();
      MyClass thisClass = classes.get(str);
    }
    */

    // init topLeft & bottomRight
    topLeft = new Point(this.getWidth(), this.getHeight());
    bottomRight = new Point(0, 0);

    // draw packages
    if (packages.size() > 0)
        if ((packages.size() == 1 && packages.get(Package.DEFAULT) == null) || packages.size() > 1)
            for (Package pack : packages.values()) {
                pack.draw(graphics);
                // push outer box
                if (pack.getTopAbs() < topLeft.y)
                    topLeft.y = pack.getTopAbs();
                if (pack.getLeftAbs() < topLeft.x)
                    topLeft.x = pack.getLeftAbs();
                if (pack.getBottomAbs() > bottomRight.y)
                    bottomRight.y = pack.getBottomAbs();
                if (pack.getRightAbs() > bottomRight.x)
                    bottomRight.x = pack.getRightAbs();
            }

    // draw implmementations
    if (isShowHeritage()) {
        Stroke oldStroke = g.getStroke();
        g.setStroke(dashed);

        /*itr = set.iterator();
        while (itr.hasNext())
        {
          String str = itr.next();
        */

        /* let's try this one ... */
        for (Entry<String, MyClass> entry : classes.entrySet()) {
            // get the actual class ...
            String str = entry.getKey();

            MyClass thisClass = classes.get(str);

            if (thisClass.getPosition().x + thisClass.getWidth() > mostRight)
                mostRight = thisClass.getPosition().x + thisClass.getWidth();
            if (thisClass.getPosition().y + thisClass.getHeight() > mostBottom)
                mostBottom = thisClass.getPosition().y + thisClass.getHeight();

            if (thisClass.getImplements().size() > 0)
                for (String extendsClass : thisClass.getImplements()) {
                    MyClass otherClass = classes.get(extendsClass);
                    if (otherClass == null)
                        otherClass = findByShortName(extendsClass);
                    //if(otherClass==null) System.err.println(extendsClass+" not found (1)");
                    //if (otherClass==null) otherClass=findByShortName(extendsClass);
                    //if(otherClass==null) System.err.println(extendsClass+" not found (2)");
                    if (otherClass != null && thisClass.isDisplayUML() && otherClass.isDisplayUML()) {
                        thisClass.setExtendsMyClass(otherClass);
                        // draw arrow from thisClass to otherClass

                        // get the center point of each class
                        Point fromP = new Point(thisClass.getPosition().x + thisClass.getWidth() / 2,
                                thisClass.getPosition().y + thisClass.getHeight() / 2);
                        Point toP = new Point(otherClass.getPosition().x + otherClass.getWidth() / 2,
                                otherClass.getPosition().y + otherClass.getHeight() / 2);

                        // get the corner 4 points of the desstination class
                        // (outer margin = 4)
                        Point toP1 = new Point(otherClass.getPosition().x - 4, otherClass.getPosition().y - 4);
                        Point toP2 = new Point(otherClass.getPosition().x + otherClass.getWidth() + 4,
                                otherClass.getPosition().y - 4);
                        Point toP3 = new Point(otherClass.getPosition().x + otherClass.getWidth() + 4,
                                otherClass.getPosition().y + otherClass.getHeight() + 4);
                        Point toP4 = new Point(otherClass.getPosition().x - 4,
                                otherClass.getPosition().y + otherClass.getHeight() + 4);

                        // get the intersection with the center line an one of the
                        // sedis of the destination class
                        Point2D toDraw = getIntersection(fromP, toP, toP1, toP2);
                        if (toDraw == null)
                            toDraw = getIntersection(fromP, toP, toP2, toP3);
                        if (toDraw == null)
                            toDraw = getIntersection(fromP, toP, toP3, toP4);
                        if (toDraw == null)
                            toDraw = getIntersection(fromP, toP, toP4, toP1);

                        // draw the arrowed line
                        if (toDraw != null)
                            drawExtends(g, fromP, new Point((int) toDraw.getX(), (int) toDraw.getY()));

                    }
                }

        }
        g.setStroke(oldStroke);
    }

    // draw inheritance
    if (isShowHeritage()) {
        /*itr = set.iterator();
        while (itr.hasNext())
        {
          String str = itr.next();
        */

        /* let's try this one ... */
        for (Entry<String, MyClass> entry : classes.entrySet()) {
            // get the actual class ...
            String str = entry.getKey();

            MyClass thisClass = classes.get(str);

            if (thisClass.getPosition().x + thisClass.getWidth() > mostRight)
                mostRight = thisClass.getPosition().x + thisClass.getWidth();
            if (thisClass.getPosition().y + thisClass.getHeight() > mostBottom)
                mostBottom = thisClass.getPosition().y + thisClass.getHeight();

            String extendsClass = thisClass.getExtendsClass();
            //System.out.println(thisClass.getFullName()+" extends "+extendsClass);
            if (!extendsClass.equals("") && thisClass.isDisplayUML()) {
                MyClass otherClass = classes.get(extendsClass);
                if (otherClass == null)
                    otherClass = findByShortName(extendsClass);
                //if(otherClass==null) System.err.println(extendsClass+" not found (1)");
                //if (otherClass==null) otherClass=findByShortName(extendsClass);
                //if(otherClass==null) System.err.println(extendsClass+" not found (2)");
                if (otherClass != null) {
                    if (otherClass != thisClass) {
                        thisClass.setExtendsMyClass(otherClass);
                        // draw arrow from thisClass to otherClass

                        // get the center point of each class
                        Point fromP = new Point(thisClass.getPosition().x + thisClass.getWidth() / 2,
                                thisClass.getPosition().y + thisClass.getHeight() / 2);
                        Point toP = new Point(otherClass.getPosition().x + otherClass.getWidth() / 2,
                                otherClass.getPosition().y + otherClass.getHeight() / 2);

                        // get the corner 4 points of the desstination class
                        // (outer margin = 4)
                        Point toP1 = new Point(otherClass.getPosition().x - 4, otherClass.getPosition().y - 4);
                        Point toP2 = new Point(otherClass.getPosition().x + otherClass.getWidth() + 4,
                                otherClass.getPosition().y - 4);
                        Point toP3 = new Point(otherClass.getPosition().x + otherClass.getWidth() + 4,
                                otherClass.getPosition().y + otherClass.getHeight() + 4);
                        Point toP4 = new Point(otherClass.getPosition().x - 4,
                                otherClass.getPosition().y + otherClass.getHeight() + 4);

                        // get the intersection with the center line an one of the
                        // sedis of the destination class
                        Point2D toDraw = getIntersection(fromP, toP, toP1, toP2);
                        if (toDraw == null)
                            toDraw = getIntersection(fromP, toP, toP2, toP3);
                        if (toDraw == null)
                            toDraw = getIntersection(fromP, toP, toP3, toP4);
                        if (toDraw == null)
                            toDraw = getIntersection(fromP, toP, toP4, toP1);

                        // draw in red if there is a cclic inheritance problem
                        if (thisClass.hasCyclicInheritance()) {
                            ((Graphics2D) graphics).setStroke(new BasicStroke(2));
                            graphics.setColor(Color.RED);
                        }

                        // draw the arrowed line
                        if (toDraw != null)
                            drawExtends((Graphics2D) graphics, fromP,
                                    new Point((int) toDraw.getX(), (int) toDraw.getY()));

                    } else {
                        ((Graphics2D) graphics).setStroke(new BasicStroke(2));
                        graphics.setColor(Color.RED);

                        // line
                        graphics.drawLine(thisClass.getPosition().x + thisClass.getWidth() / 2,
                                thisClass.getPosition().y, thisClass.getPosition().x + thisClass.getWidth() / 2,
                                thisClass.getPosition().y - 32);
                        graphics.drawLine(thisClass.getPosition().x + thisClass.getWidth() / 2,
                                thisClass.getPosition().y - 32,
                                thisClass.getPosition().x + thisClass.getWidth() + 32,
                                thisClass.getPosition().y - 32);
                        graphics.drawLine(thisClass.getPosition().x + thisClass.getWidth() + 32,
                                thisClass.getPosition().y - 32,
                                thisClass.getPosition().x + thisClass.getWidth() + 32,
                                thisClass.getPosition().y + thisClass.getHeight() + 32);
                        graphics.drawLine(thisClass.getPosition().x + thisClass.getWidth() + 32,
                                thisClass.getPosition().y + thisClass.getHeight() + 32,
                                thisClass.getPosition().x + thisClass.getWidth() / 2,
                                thisClass.getPosition().y + thisClass.getHeight() + 32);
                        drawExtends((Graphics2D) graphics,
                                new Point(thisClass.getPosition().x + thisClass.getWidth() / 2,
                                        thisClass.getPosition().y + thisClass.getHeight() + 32),
                                new Point(thisClass.getPosition().x + thisClass.getWidth() / 2,
                                        thisClass.getPosition().y + thisClass.getHeight()));
                    }

                    // reset the stroke and the color
                    ((Graphics2D) graphics).setStroke(new BasicStroke(1));
                    graphics.setColor(Color.BLACK);
                }
            }
        }
    }

    // setup a hastable to store the relations
    //Hashtable<String,StringList> classUsage = new Hashtable<String,StringList>();

    // store compositions
    Hashtable<MyClass, Vector<MyClass>> classCompositions = new Hashtable<MyClass, Vector<MyClass>>();
    // store aggregations
    Hashtable<MyClass, Vector<MyClass>> classAggregations = new Hashtable<MyClass, Vector<MyClass>>();
    // store all relations
    Hashtable<MyClass, Vector<MyClass>> classUsings = new Hashtable<MyClass, Vector<MyClass>>();

    /*
    // iterate through all classes to find compositions
    itr = set.iterator();
    while (itr.hasNext())
    {
      // get the actual classname
      String str = itr.next();
    */

    /* let's try this one ... */
    for (Entry<String, MyClass> entry : classes.entrySet()) {
        // get the actual class ...
        String str = entry.getKey();

        // get the corresponding "MyClass" object
        MyClass thisClass = classes.get(str);
        // setup a list to store the relations with this class
        Vector<MyClass> theseCompositions = new Vector<MyClass>();

        // get all fields of this class
        StringList uses = thisClass.getFieldTypes();
        for (int u = 0; u < uses.count(); u++) {
            // try to find the other (used) class
            MyClass otherClass = classes.get(uses.get(u));
            if (otherClass == null)
                otherClass = findByShortName(uses.get(u));
            if (otherClass != null) // means this class uses the other ones
            {
                // add the other class to the list
                theseCompositions.add(otherClass);
            }
        }

        // add the list of used classes to the MyClass object
        thisClass.setUsesMyClass(theseCompositions);
        // store the composition in the general list
        classCompositions.put(thisClass, theseCompositions);
        // store the compositions int eh global relation list
        classUsings.put(thisClass, new Vector<MyClass>(theseCompositions));
        //                        ^^^^^^^^^^^^^^^^^^^^
        //    important !! => create a new vector, otherwise the list
        //                    are the same ...
    }

    /*
    // iterate through all classes to find aggregations
    itr = set.iterator();
    while (itr.hasNext())
    {
      // get the actual class
      String str = itr.next();
    */

    /* let's try this one ... */
    for (Entry<String, MyClass> entry : classes.entrySet()) {
        // get the actual class ...
        String str = entry.getKey();

        // get the corresponding "MyClass" object
        MyClass thisClass = classes.get(str);
        // we need a list to store the aggragations with this class
        Vector<MyClass> theseAggregations = new Vector<MyClass>();
        // try to get the list of compositions for this class
        // init if not present
        Vector<MyClass> theseCompositions = classCompositions.get(thisClass);
        if (theseCompositions == null)
            theseCompositions = new Vector<MyClass>();
        // try to get the list of all relations for this class
        // init if not present
        Vector<MyClass> theseClasses = classUsings.get(thisClass);
        if (theseClasses == null)
            theseClasses = new Vector<MyClass>();

        // get the names of the classes that thisclass uses
        StringList foundUsage = thisClass.getUsesWho();
        // go through the list an check to find a corresponding MyClass
        for (int f = 0; f < foundUsage.count(); f++) {
            // get the name of the used class
            String usedClass = foundUsage.get(f);

            MyClass otherClass = classes.get(usedClass);
            if (otherClass == null)
                otherClass = findByShortName(usedClass);
            if (otherClass != null && thisClass != otherClass)
            // meanint "otherClass" is a class used by thisClass
            {
                if (!theseCompositions.contains(otherClass))
                    theseAggregations.add(otherClass);
                if (!theseClasses.contains(otherClass))
                    theseClasses.add(otherClass);
            }
        }

        // get all method types of this class
        StringList uses = thisClass.getMethodTypes();
        for (int u = 0; u < uses.count(); u++) {
            // try to find the other (used) class
            MyClass otherClass = classes.get(uses.get(u));
            if (otherClass == null)
                otherClass = findByShortName(uses.get(u));
            if (otherClass != null) // means this class uses the other ones
            {
                // add the other class to the list
                theseAggregations.add(otherClass);
            }
        }

        // store the relations to the class
        thisClass.setUsesMyClass(theseClasses);
        // store the aggregation to the global list
        classAggregations.put(thisClass, theseAggregations);
        // store all relations to the global list
        classUsings.put(thisClass, theseClasses);
    }

    if (isShowComposition()) {
        /*Set<MyClass> set2 = classCompositions.keySet();
        Iterator<MyClass> itr2 = set2.iterator();
        while (itr2.hasNext())
        {
          MyClass thisClass = itr2.next();
        */

        /* let's try this one ... */
        for (Entry<MyClass, Vector<MyClass>> entry : classCompositions.entrySet()) {
            // get the actual class ...
            MyClass thisClass = entry.getKey();
            if (thisClass.isDisplayUML()) {
                Vector<MyClass> otherClasses = classCompositions.get(thisClass);
                for (MyClass otherClass : otherClasses)
                    drawComposition(g, thisClass, otherClass, classUsings);
            }
        }
    }

    if (isShowAggregation()) {
        /*Set<MyClass> set2 = classAggregations.keySet();
        Iterator<MyClass> itr2 = set2.iterator();
        while (itr2.hasNext())
        {
          MyClass thisClass = itr2.next();
        */

        /* let's try this one ... */
        for (Entry<MyClass, Vector<MyClass>> entry : classAggregations.entrySet()) {
            // get the actual class ...
            MyClass thisClass = entry.getKey();
            if (thisClass.isDisplayUML()) {
                Vector<MyClass> otherClasses = classAggregations.get(thisClass);
                for (MyClass otherClass : otherClasses)
                    drawAggregation(g, thisClass, otherClass, classUsings);
            }
        }
    }

    // draw classes again to put them on top
    // of the arrows
    /*set = classes.keySet();
    itr = set.iterator();
    while (itr.hasNext())
    {
      String str = itr.next();
    */

    /* let's try this one ... */
    for (Entry<String, MyClass> entry : classes.entrySet()) {
        // get the actual class ...
        String str = entry.getKey();

        classes.get(str).setEnabled(this.isEnabled());
        classes.get(str).draw(graphics, showFields, showMethods);

        // push outer box
        MyClass thisClass = classes.get(str);
        if (thisClass.getPosition().y < topLeft.y)
            topLeft.y = thisClass.getPosition().y;
        if (thisClass.getPosition().x < topLeft.x)
            topLeft.x = thisClass.getPosition().x;
        if (thisClass.getPosition().y + thisClass.getHeight() > bottomRight.y)
            bottomRight.y = thisClass.getPosition().y + thisClass.getHeight();
        if (thisClass.getPosition().x + thisClass.getWidth() > bottomRight.x)
            bottomRight.x = thisClass.getPosition().x + thisClass.getWidth();

    }

    // comments
    if (commentString != null) {
        String fontName = g.getFont().getName();
        g.setFont(new Font("Courier", g.getFont().getStyle(), Unimozer.DRAW_FONT_SIZE));

        if (!commentString.trim().equals("")) {
            String myCommentString = new String(commentString);
            Point myCommentPoint = new Point(commentPoint);
            //System.out.println(myCommentString);

            // adjust comment
            myCommentString = myCommentString.trim();
            // adjust position
            myCommentPoint.y = myCommentPoint.y + 16;

            // explode comment
            StringList sl = StringList.explode(myCommentString, "\n");
            // calculate totals
            int totalHeight = 0;
            int totalWidth = 0;
            for (int i = 0; i < sl.count(); i++) {
                String line = sl.get(i).trim();
                int h = (int) g.getFont().getStringBounds(line, g.getFontRenderContext()).getHeight();
                int w = (int) g.getFont().getStringBounds(line, g.getFontRenderContext()).getWidth();
                totalHeight += h;
                totalWidth = Math.max(totalWidth, w);
            }

            // get comment size
            // draw background
            g.setColor(new Color(255, 255, 128, 255));
            g.fillRoundRect(myCommentPoint.x, myCommentPoint.y, totalWidth + 8, totalHeight + 8, 4, 4);
            // draw border
            g.setColor(Color.BLACK);
            g.drawRoundRect(myCommentPoint.x, myCommentPoint.y, totalWidth + 8, totalHeight + 8, 4, 4);

            // draw text
            totalHeight = 0;
            for (int i = 0; i < sl.count(); i++) {
                String line = sl.get(i).trim();
                int h = (int) g.getFont().getStringBounds(myCommentString, g.getFontRenderContext())
                        .getHeight();
                g.drawString(line, myCommentPoint.x + 4, myCommentPoint.y + h + 2 + totalHeight);
                totalHeight += h;
            }

        }

        g.setFont(new Font(fontName, Font.PLAIN, Unimozer.DRAW_FONT_SIZE));

    }

    /*
    if(!isEnabled())
    {
        g.setColor(new Color(128,128,128,128));
        g.fillRect(0,0,getWidth(),getHeight());
            
    }
    */

    this.setPreferredSize(new Dimension(mostRight + 32, mostBottom + 32));
    // THE NEXT LINE MAKES ALL DIALOGUES DISAPEAR!!
    //this.setSize(mostRight+32, mostBottom+32);
    this.validate();
    ((JScrollPane) this.getParent().getParent()).revalidate();

    if (mode == MODE_EXTENDS && extendsFrom != null && extendsDragPoint != null) {
        graphics.setColor(Color.BLUE);
        ((Graphics2D) graphics).setStroke(new BasicStroke(2));
        drawExtends(g, new Point(extendsFrom.getPosition().x + extendsFrom.getWidth() / 2,
                extendsFrom.getPosition().y + extendsFrom.getHeight() / 2), extendsDragPoint);
        graphics.setColor(Color.BLACK);
        ((Graphics2D) graphics).setStroke(new BasicStroke(1));
    }
}

From source file:com.aurel.track.report.gantt.data.TrackGanttRenderer.java

/**
 * Draws a single task.//from w  ww .ja v a  2 s  .  c o m
 *
 * @param g2  the graphics device.
 * @param state  the renderer state.
 * @param dataArea  the data plot area.
 * @param plot  the plot.
 * @param domainAxis  the domain axis.
 * @param rangeAxis  the range axis.
 * @param dataset  the data.
 * @param row  the row index (zero-based).
 * @param column  the column index (zero-based).
 */
protected void drawTask(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea, CategoryPlot plot,
        CategoryAxis domainAxis, ValueAxis rangeAxis, TrackTaskSeriesCollection dataset, int row, int column,
        int pass) {

    log.debug("Working on item at position - column: " + column + " row: " + row);

    int seriesCount = getRowCount();
    int categoryCount = getColumnCount();

    RectangleEdge domainAxisLocation = plot.getDomainAxisEdge();
    RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge();

    // Y0
    Number value0 = dataset.getEndValue(row, column);
    if (value0 == null) {
        return;
    }
    double java2dValue0 = rangeAxis.valueToJava2D(value0.doubleValue(), dataArea, rangeAxisLocation);

    // Y1
    Number value1 = dataset.getStartValue(row, column);
    if (value1 == null) {
        return;
    }
    double java2dValue1 = rangeAxis.valueToJava2D(value1.doubleValue(), dataArea, rangeAxisLocation);

    if (java2dValue1 < java2dValue0) {
        double temp = java2dValue1;
        java2dValue1 = java2dValue0;
        java2dValue0 = temp;
        Number tempNum = value1;
        value1 = value0;
        value0 = tempNum;
    }

    double rectStart = domainAxis.getCategoryStart(column, getColumnCount(), dataArea, domainAxisLocation);

    // BREADTH
    double rectBreadth = state.getBarWidth();

    // BAR HEIGHT
    double rectLength = Math.abs(java2dValue1 - java2dValue0);

    Rectangle2D bar = null;

    if (seriesCount > 1) {
        double seriesGap = dataArea.getHeight() * getItemMargin() / (categoryCount * (seriesCount - 1));
        rectStart = rectStart + row * (state.getBarWidth() + seriesGap);
    } else {
        rectStart = rectStart + row * state.getBarWidth();
    }

    // correct the start/stop Bars point
    if (startBars > (int) (java2dValue1 - rectLength)) {
        this.startBars = (int) (java2dValue1 - rectLength);
    }

    if (stopBars < (int) java2dValue1) {
        stopBars = (int) java2dValue1;
    }

    // The coordinates for the bar labels
    int startLabelX = 0;
    int startLabelY = 0;

    int endLabelX = 0;
    int endLabelY = 0;

    int barLabelX = 0;
    int barLabelY = 0;

    Polygon outer = null;
    Polygon inner = null;

    boolean isMilestone = dataset.isMilestone(column, row);
    boolean isSupertask = dataset.isSupertask(column, row);

    if (isMilestone) {

        // The start point for the end date label
        endLabelX = (int) (java2dValue1 + rectBreadth / 2);
        endLabelY = (int) (rectStart + (3 * rectBreadth / 4));

        barLabelX = (int) (java2dValue1 - rectBreadth / 2);
        barLabelY = (int) (rectStart + (3 * rectBreadth / 4));

        int breadth = (int) rectBreadth;

        int centerX = (int) java2dValue1;

        int centerY = (int) (rectStart + rectBreadth / 2);

        int[] xPoints = { centerX, centerX - breadth / 2, centerX, centerX + breadth / 2 };
        int[] yPoints = { centerY - breadth / 2, centerY, centerY + breadth / 2, centerY };

        outer = new Polygon(xPoints, yPoints, 4);

        //calculate the difference from the ratio
        int diff = (int) (rectBreadth / (2 * msInfo.getRatio()));

        int[] xPointsI = { centerX, centerX - breadth / 2 + diff, centerX, centerX + breadth / 2 - diff };
        int[] yPointsI = { centerY - breadth / 2 + diff, centerY, centerY + breadth / 2 - diff, centerY };

        inner = new Polygon(xPointsI, yPointsI, 4);

    }

    else if (isSupertask) {

        // The start point for the end date label
        startLabelX = (int) (java2dValue1 - rectLength - rectBreadth / 2);
        startLabelY = (int) (rectStart + rectBreadth * 3 / 4);

        endLabelX = (int) (java2dValue1 + rectBreadth / 2);
        endLabelY = (int) (rectStart + rectBreadth * 3 / 4);

        barLabelX = (int) (java2dValue1 - rectLength + rectBreadth / 3);
        barLabelY = (int) (rectStart + rectBreadth);

        int breadth = (int) rectBreadth;
        int length = (int) rectLength;

        int startX = (int) (java2dValue1 - rectLength);
        int startY = (int) rectStart;

        int[] xPoints = { startX - breadth / 2, startX + length + breadth / 2, startX + length + breadth / 2,
                startX + length, startX + length - breadth / 2, startX + breadth / 2, startX,
                startX - breadth / 2 };
        int[] yPoints = { startY, startY, startY + breadth / 2, startY + breadth, startY + breadth / 2,
                startY + breadth / 2, startY + breadth, startY + breadth / 2 };

        outer = new Polygon(xPoints, yPoints, 8);

        //calculate the difference from the ratio
        int diff = (int) (rectBreadth / (2 * stInfo.getRatio()));

        int[] xPointsI = { startX - breadth / 2 + diff, startX + length + breadth / 2 - diff,
                startX + length + breadth / 2 - diff, startX + length, startX + length - breadth / 2 + diff,
                startX + length - breadth / 2 + diff, startX + breadth / 2 - diff, startX + breadth / 2 - diff,
                startX, startX - breadth / 2 + diff };
        int[] yPointsI = { startY + diff, startY + diff, startY + breadth / 2, startY + breadth / 2 + diff,
                startY + breadth / 2, startY + breadth / 2 - diff, startY + breadth / 2 - diff,
                startY + breadth / 2, startY + breadth / 2 + diff, startY + breadth / 2 };

        inner = new Polygon(xPointsI, yPointsI, 10);
    } else {

        rectBreadth = rectBreadth / 2.0;
        // The start point for the end date label
        startLabelX = (int) (java2dValue1 - rectLength);
        startLabelY = (int) (rectStart + rectBreadth * 3 / 4);

        endLabelX = (int) java2dValue1;
        endLabelY = (int) (rectStart + rectBreadth * 3 / 4);

        barLabelX = (int) (java2dValue1 - rectLength);
        barLabelY = (int) (rectStart); //+ rectBreadth * 4 / 3 );

        bar = new Rectangle2D.Double(java2dValue0, rectStart, rectLength, rectBreadth);
        // percent complete could be obtained in the 
        // ReportBeans, set in the ReportBean and set in the Item by the GanttGenerator
        // currently this is not relevant.
        Rectangle2D completeBar = null;
        Rectangle2D incompleteBar = null;
        Number percent = dataset.getPercentComplete(row, column);
        double start = getStartPercent();
        double end = getEndPercent();
        if (percent != null) {
            double p = percent.doubleValue();
            completeBar = new Rectangle2D.Double(java2dValue0, rectStart + start * rectBreadth, rectLength * p,
                    rectBreadth * (end - start));
            incompleteBar = new Rectangle2D.Double(java2dValue0 + rectLength * p,
                    rectStart + start * rectBreadth, rectLength * (1 - p), rectBreadth * (end - start));
        }

        /** This is for displaying e.g. (german Soll und Ist Termine). This implementation is designed to display only one Series.
         * If you need Soll und Ist, you can generate two gantt charts.
         * --->> Paint seriesPaint = getItemPaint (row, column);
         */

        Paint seriesPaint = tsInfo.getInnerColor();

        g2.setPaint(seriesPaint);
        g2.fill(bar);

        if (tsInfo.isDisplayIDLabel() && (!tsInfo.isDisplaySynopsis())) {
            // draw the tasks ID
            drawLabel(new Integer(dataset.getItem(column, row).getId()).toString(), g2, barLabelX, barLabelY,
                    tsInfo.getLabelFontID(), false, (int) rectLength);
        }

        else if (tsInfo.isDisplaySynopsis()) {
            if (tsInfo.isDisplayDateLabel()) {

                // format start date to the requested format using the current locale
                String label = (new SimpleDateFormat(tsInfo.getDateLabelFormat(), locale))
                        .format(dataset.getItem(column, row).getStart());

                label = dataset.getItem(column, row).getDescription() + " - " + label;
                drawLabel(label, g2, barLabelX, barLabelY, tsInfo.getLabelFont(), true);

                // format end date to the requested format using the current locale
                label = (new SimpleDateFormat(tsInfo.getDateLabelFormat(), locale))
                        .format(dataset.getItem(column, row).getStop());

                // draw the supertasks end date
                drawLabel(label, g2, endLabelX, endLabelY, tsInfo.getLabelFont(), false);

            } else {
                drawLabel(dataset.getItem(column, row).getDescription(), g2, startLabelX, startLabelY,
                        tsInfo.getLabelFont(), true);
            }
        }

        if (tsInfo.isDisplayDateLabel() && (!tsInfo.isDisplaySynopsis())) {

            // format start date to the requested format using the current locale
            String label = (new SimpleDateFormat(tsInfo.getDateLabelFormat(), locale))
                    .format(dataset.getItem(column, row).getStart());

            // draw the tasks start date
            drawLabel(label, g2, startLabelX, startLabelY, tsInfo.getLabelFont(), true);

            // format end date to the requested format using the current locale
            label = (new SimpleDateFormat(tsInfo.getDateLabelFormat(), locale))
                    .format(dataset.getItem(column, row).getStop());
            // draw the tasks end date
            drawLabel(label, g2, endLabelX, endLabelY, tsInfo.getLabelFont(), false);
        }

        if (completeBar != null) {
            g2.setPaint(getCompletePaint());
            g2.fill(completeBar);
        }
        if (incompleteBar != null) {
            g2.setPaint(getIncompletePaint());
            g2.fill(incompleteBar);
        }

        // draw the outline...
        if (state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) {

            Stroke stroke = getItemOutlineStroke(row, column);
            Paint paint = getItemOutlinePaint(row, column);
            if (stroke != null && paint != null) {
                g2.setStroke(stroke);
                g2.setPaint(paint);

                g2.draw(bar);
            }
        }
    }

    // Draw milestone or Supertask
    if (isMilestone) {

        g2.setPaint(msInfo.getOuterColor());
        g2.fill(outer);
        g2.draw(outer);

        g2.setPaint(msInfo.getInnerColor());
        g2.fill(inner);
        g2.draw(inner);

        Stroke stroke = getItemOutlineStroke(row, column);
        Paint paint = getItemOutlinePaint(row, column);
        if (stroke != null && paint != null) {
            g2.setStroke(stroke);
            g2.setPaint(paint);
            g2.draw(outer);
        }

        // draw the milestones due date
        if (msInfo.isDisplayDateLabel()) {

            // format milestone date to the requested format using the current locale
            String label = (new SimpleDateFormat(msInfo.getDateLabelFormat(), locale))
                    .format(dataset.getItem(column, row).getStart());

            drawLabel(label, g2, endLabelX, endLabelY, msInfo.getLabelFont(), false);
        }

        if (msInfo.isDisplayIDLabel() && (!msInfo.isDisplaySynopsis())) {
            // for milestones the ID is aligned at the left side.
            drawLabel(new Integer(dataset.getItem(column, row).getId()).toString(), g2, barLabelX, barLabelY,
                    msInfo.getLabelFontID(), true, (int) rectLength);
        } else if (msInfo.isDisplaySynopsis()) {
            drawLabel(dataset.getItem(column, row).getDescription(), g2, barLabelX, barLabelY,
                    msInfo.getLabelFont(), true, (int) rectLength);
        }

    } else if (isSupertask) {

        g2.setPaint(stInfo.getOuterColor());
        g2.fill(outer);
        g2.draw(outer);

        g2.setPaint(stInfo.getInnerColor());
        g2.fill(inner);
        g2.draw(inner);

        Stroke stroke = getItemOutlineStroke(row, column);
        Paint paint = getItemOutlinePaint(row, column);
        if (stroke != null && paint != null) {
            g2.setStroke(stroke);
            g2.setPaint(paint);
            g2.draw(outer);
        }

        // draw the supertasks ID
        if (stInfo.isDisplayIDLabel() && (!stInfo.isDisplaySynopsis())) {
            drawLabel(new Integer(dataset.getItem(column, row).getId()).toString(), g2, barLabelX, barLabelY,
                    stInfo.getLabelFontID(), false, (int) rectLength);
        } else if (stInfo.isDisplaySynopsis()) {
            if (stInfo.isDisplayDateLabel()) {

                // format start date to the requested format using the current locale
                String label = (new SimpleDateFormat(stInfo.getDateLabelFormat(), locale))
                        .format(dataset.getItem(column, row).getStart());

                label = dataset.getItem(column, row).getDescription() + " - " + label;
                drawLabel(label, g2, startLabelX, startLabelY, stInfo.getLabelFont(), true);

                // format end date to the requested format using the current locale
                label = (new SimpleDateFormat(stInfo.getDateLabelFormat(), locale))
                        .format(dataset.getItem(column, row).getStop());

                // draw the supertasks end date
                drawLabel(label, g2, endLabelX, endLabelY, stInfo.getLabelFont(), false);

            } else {
                drawLabel(dataset.getItem(column, row).getDescription(), g2, startLabelX, startLabelY,
                        stInfo.getLabelFont(), true);
            }
        }
        if (stInfo.isDisplayDateLabel() && (!tsInfo.isDisplaySynopsis())) {

            // format start date to the requested format using the current locale
            String label = (new SimpleDateFormat(stInfo.getDateLabelFormat(), locale))
                    .format(dataset.getItem(column, row).getStart());

            // draw the supertasks start date
            drawLabel(label, g2, startLabelX, startLabelY, stInfo.getLabelFont(), true);

            // format end date to the requested format using the current locale
            label = (new SimpleDateFormat(stInfo.getDateLabelFormat(), locale))
                    .format(dataset.getItem(column, row).getStop());

            // draw the supertasks end date
            drawLabel(label, g2, endLabelX, endLabelY, stInfo.getLabelFont(), false);
        }
    }
}

From source file:lu.fisch.unimozer.Diagram.java

private void drawCompoAggregation2(Graphics2D g, MyClass thisClass, MyClass otherClass,
        Hashtable<MyClass, Vector<MyClass>> classUsings, boolean isComposition) {
    if (thisClass != otherClass) {
        Point thisTop = new Point(thisClass.getX() + thisClass.getWidth() / 2, thisClass.getY());
        Point thisBottom = new Point(thisClass.getX() + thisClass.getWidth() / 2,
                thisClass.getY() + thisClass.getHeight());
        Point thisLeft = new Point(thisClass.getX(), thisClass.getY() + thisClass.getHeight() / 2);
        Point thisRight = new Point(thisClass.getX() + thisClass.getWidth(),
                thisClass.getY() + thisClass.getHeight() / 2);
        Point[] thisPoints = { thisTop, thisBottom, thisLeft, thisRight };

        Point otherTop = new Point(otherClass.getX() + otherClass.getWidth() / 2, otherClass.getY());
        Point otherBottom = new Point(otherClass.getX() + otherClass.getWidth() / 2,
                otherClass.getY() + otherClass.getHeight());
        Point otherLeft = new Point(otherClass.getX(), otherClass.getY() + otherClass.getHeight() / 2);
        Point otherRight = new Point(otherClass.getX() + otherClass.getWidth(),
                otherClass.getY() + otherClass.getHeight() / 2);
        Point[] otherPoints = { otherTop, otherBottom, otherLeft, otherRight };

        double min = Double.MAX_VALUE;
        Point thisPoint = null;//from w  ww .  jav a 2 s  . com
        Point otherPoint = null;
        double thisMin;

        // determine closest middelst
        for (int i = 0; i < thisPoints.length; i++)
            for (int j = 0; j < otherPoints.length; j++) {
                thisMin = thisPoints[i].distance(otherPoints[j]);
                if (thisMin < min) {
                    min = thisMin;
                    thisPoint = thisPoints[i];
                    otherPoint = otherPoints[j];
                }
            }

        //Vector<MyClass> others = classUsings.get(thisClass);
        Vector<MyClass> usingsThisClass = classUsings.get(thisClass);
        // iterate through all usages
        /*Set<MyClass> set = classUsings.keySet();
        Iterator<MyClass> itr = set.iterator();
        while (itr.hasNext())
        {
        // get the actual class ...
        MyClass actual = itr.next();
        // ... and the list of classes it uses
        Vector<MyClass> actualUses = classUsings.get(actual);
        // iterate through that list
        for(MyClass used : actualUses)
        {
            // add the actual class if
            // - it usesd the "otherClass"
            // - and the actual class has not yet been captured
            if(used==thisClass && !usingsThisClass.contains(actual)) usingsThisClass.add(actual);
        }
        }*/

        /* let's try this one ... */
        for (Entry<MyClass, Vector<MyClass>> entry : classUsings.entrySet()) {
            // get the actual class ...
            MyClass actual = entry.getKey();
            // ... and the list of classes it uses
            Vector<MyClass> actualUses = classUsings.get(actual);
            // iterate through that list
            for (MyClass used : actualUses) {
                // add the actual class if
                // - it usesd the "otherClass"
                // - and the actual class has not yet been captured
                if (used == thisClass && !usingsThisClass.contains(actual))
                    usingsThisClass.add(actual);
            }
        }

        Stroke oldStroke = g.getStroke();

        if (thisPoint == thisTop) {
            // init number of connectionx
            int thisCon = 1;
            // init the direction into which to move
            int thisDir = 1;
            if (thisPoint.x > otherPoint.x)
                thisDir = -1;
            // loop through others to determine position
            for (MyClass other : usingsThisClass) {
                // check goto right
                if ((other.getCenter().y < thisClass.getCenter().y)
                        && (other.getCenter().x >= thisClass.getCenter().x)
                        && (other.getCenter().y > otherClass.getCenter().y) && (thisDir == 1))
                    thisCon++;
                // check goto left
                if ((other.getCenter().y < thisClass.getCenter().y)
                        && (other.getCenter().x < thisClass.getCenter().x)
                        && (other.getCenter().y > otherClass.getCenter().y) && (thisDir == -1))
                    thisCon++;
            }
            int con = thisCon;
            thisCon = (int) ((thisCon - 0.5) * (12 * thisDir));

            Polygon p = new Polygon();
            p.addPoint(thisCon + thisClass.getPosition().x + thisClass.getWidth() / 2,
                    thisClass.getPosition().y);
            p.addPoint(thisCon + thisClass.getPosition().x + thisClass.getWidth() / 2 - 4,
                    thisClass.getPosition().y - 8);
            p.addPoint(thisCon + thisClass.getPosition().x + thisClass.getWidth() / 2,
                    thisClass.getPosition().y - 16);
            p.addPoint(thisCon + thisClass.getPosition().x + thisClass.getWidth() / 2 + 4,
                    thisClass.getPosition().y - 8);
            if (isComposition)
                g.fillPolygon(p);
            else
                g.drawPolygon(p);

            thisPoint.y -= 15;
            thisPoint.x += thisCon;

            Point movePoint = new Point(thisPoint);
            movePoint.y -= (usingsThisClass.size() - con) * 8;
            g.setStroke(dashed);
            drawLine(g, thisPoint, movePoint);
            thisPoint = movePoint;
        } else if (thisPoint == thisBottom) {
            // init number of connectionx
            int thisCon = 1;
            // init the direction into which to move
            int thisDir = 1;
            if (thisPoint.x > otherPoint.x)
                thisDir = -1;
            // loop through others to determine position
            for (MyClass other : usingsThisClass) {
                // check goto right
                if ((other.getCenter().y >= thisClass.getCenter().y)
                        && (other.getCenter().x >= thisClass.getCenter().x)
                        && (other.getCenter().y > otherClass.getCenter().y) && (thisDir == 1))
                    thisCon++;
                // check goto left
                if ((other.getCenter().y >= thisClass.getCenter().y)
                        && (other.getCenter().x < thisClass.getCenter().x)
                        && (other.getCenter().y > otherClass.getCenter().y) && (thisDir == -1))
                    thisCon++;
            }
            int con = thisCon;
            thisCon = (int) ((thisCon - 0.5) * (12 * thisDir));

            // bottom
            Polygon p = new Polygon();
            p.addPoint(thisCon + thisClass.getPosition().x + thisClass.getWidth() / 2,
                    thisClass.getPosition().y + thisClass.getHeight());
            p.addPoint(thisCon + thisClass.getPosition().x + thisClass.getWidth() / 2 - 4,
                    thisClass.getPosition().y + thisClass.getHeight() + 8);
            p.addPoint(thisCon + thisClass.getPosition().x + thisClass.getWidth() / 2,
                    thisClass.getPosition().y + thisClass.getHeight() + 16);
            p.addPoint(thisCon + thisClass.getPosition().x + thisClass.getWidth() / 2 + 4,
                    thisClass.getPosition().y + thisClass.getHeight() + 8);
            if (isComposition)
                g.fillPolygon(p);
            else
                g.drawPolygon(p);

            thisPoint.y += 15;
            thisPoint.x += thisCon;

            Point movePoint = new Point(thisPoint);
            movePoint.y += (usingsThisClass.size() - con) * 8;
            g.setStroke(dashed);
            drawLine(g, thisPoint, movePoint);
            thisPoint = movePoint;
        } else if (thisPoint == thisRight) {
            // init number of connectionx
            int thisCon = 1;
            // init the direction into which to move
            int thisDir = 1;
            if (thisPoint.y > otherPoint.y)
                thisDir = -1;
            // loop through others to determine position
            for (MyClass other : usingsThisClass) {
                // check goto up
                if ((other.getCenter().x >= thisClass.getCenter().x)
                        && (other.getCenter().y >= thisClass.getCenter().y)
                        && (other.getCenter().x > otherClass.getCenter().x) && (thisDir == 1))
                    thisCon++;
                // check goto down
                if ((other.getCenter().x >= thisClass.getCenter().x)
                        && (other.getCenter().y < thisClass.getCenter().y)
                        && (other.getCenter().x > otherClass.getCenter().x) && (thisDir == -1))
                    thisCon++;
            }
            int con = thisCon;
            thisCon = (int) ((thisCon - 0.5) * (12 * thisDir));

            // right
            Polygon p = new Polygon();
            //thisCON = thisClass.getConnector().getNewBottom(otherDIR);
            p.addPoint(thisClass.getPosition().x + thisClass.getWidth(),
                    thisCon + thisClass.getPosition().y + thisClass.getHeight() / 2);
            p.addPoint(thisClass.getPosition().x + thisClass.getWidth() + 8,
                    thisCon + thisClass.getPosition().y + thisClass.getHeight() / 2 - 4);
            p.addPoint(thisClass.getPosition().x + thisClass.getWidth() + 16,
                    thisCon + thisClass.getPosition().y + thisClass.getHeight() / 2);
            p.addPoint(thisClass.getPosition().x + thisClass.getWidth() + 8,
                    thisCon + thisClass.getPosition().y + thisClass.getHeight() / 2 + 4);
            if (isComposition)
                g.fillPolygon(p);
            else
                g.drawPolygon(p);

            thisPoint.x += 15;
            thisPoint.y += thisCon;

            Point movePoint = new Point(thisPoint);
            movePoint.x += (usingsThisClass.size() - con) * 8;
            g.setStroke(dashed);
            drawLine(g, thisPoint, movePoint);
            thisPoint = movePoint;
        } else // left
        {
            // init number of connectionx
            int thisCon = 1;
            // init the direction into which to move
            int thisDir = 1;
            if (thisPoint.y > otherPoint.y)
                thisDir = -1;
            // loop through others to determine position
            for (MyClass other : usingsThisClass) {
                // check goto up
                if ((other.getCenter().x < thisClass.getCenter().x)
                        && (other.getCenter().y >= thisClass.getCenter().y)
                        && (other.getCenter().x > otherClass.getCenter().x) && (thisDir == 1))
                    thisCon++;
                // check goto down
                if ((other.getCenter().x < thisClass.getCenter().x)
                        && (other.getCenter().y < thisClass.getCenter().y)
                        && (other.getCenter().x > otherClass.getCenter().x) && (thisDir == -1))
                    thisCon++;
            }
            int con = thisCon;
            thisCon = (int) ((thisCon - 0.5) * (12 * thisDir));

            Polygon p = new Polygon();
            p.addPoint(thisClass.getPosition().x,
                    thisCon + thisClass.getPosition().y + thisClass.getHeight() / 2);
            p.addPoint(thisClass.getPosition().x - 8,
                    thisCon + thisClass.getPosition().y + thisClass.getHeight() / 2 - 4);
            p.addPoint(thisClass.getPosition().x - 16,
                    thisCon + thisClass.getPosition().y + thisClass.getHeight() / 2);
            p.addPoint(thisClass.getPosition().x - 8,
                    thisCon + thisClass.getPosition().y + thisClass.getHeight() / 2 + 4);
            if (isComposition)
                g.fillPolygon(p);
            else
                g.drawPolygon(p);

            thisPoint.y += thisCon;
            thisPoint.x -= 15;

            Point movePoint = new Point(thisPoint);
            movePoint.x -= (usingsThisClass.size() - con) * 8;
            g.setStroke(dashed);
            drawLine(g, thisPoint, movePoint);
            thisPoint = movePoint;
        }

        //Vector<MyClass> others = classUsings.get(otherClass);
        Vector<MyClass> usingsOtherClass = classUsings.get(otherClass);
        /*
        // iterate through all usages
        set = classUsings.keySet();
        itr = set.iterator();
        while (itr.hasNext())
        {
        // get the actual class ...
        MyClass actual = itr.next();
        // ... and the list of classes it uses
        Vector<MyClass> actualUses = classUsings.get(actual);
        // iterate through that list
        for(MyClass used : actualUses)
        {
            // add the actual class if
            // - it usesd the "otherClass"
            // - and the actual class has not yet been captured
            if(used==otherClass && !usingsOtherClass.contains(actual)) usingsOtherClass.add(actual);
        }
        }
        */

        /* let's try this one ... */
        for (Entry<MyClass, Vector<MyClass>> entry : classUsings.entrySet()) {
            // get the actual class ...
            MyClass actual = entry.getKey();
            // ... and the list of classes it uses
            Vector<MyClass> actualUses = classUsings.get(actual);
            // iterate through that list
            for (MyClass used : actualUses) {
                // add the actual class if
                // - it usesd the "otherClass"
                // - and the actual class has not yet been captured
                if (used == otherClass && !usingsOtherClass.contains(actual))
                    usingsOtherClass.add(actual);
            }
        }

        Point stopUp;
        Point stopDown;
        Point stopOut;
        Point step;
        Point start = thisPoint;
        Point stop;

        if (otherPoint == otherTop) {
            // init number of connectionx
            int otherCon = 1;
            // init the direction into which to move
            int otherDir = 1;
            if (otherPoint.x > thisPoint.x)
                otherDir = -1;
            // loop through others to determine position
            for (MyClass other : usingsOtherClass) {
                // check goto right
                if ((other.getCenter().y < otherClass.getCenter().y)
                        && (other.getCenter().x >= otherClass.getCenter().x)
                        && (other.getCenter().y < thisClass.getCenter().y) && (otherDir == 1))
                    otherCon++;
                // check goto left
                if ((other.getCenter().y < otherClass.getCenter().y)
                        && (other.getCenter().x < otherClass.getCenter().x)
                        && (other.getCenter().y < thisClass.getCenter().y) && (otherDir == -1))
                    otherCon++;
            }
            otherCon = (int) ((otherCon - 0.5) * (12 * otherDir));

            otherPoint.x += otherCon;

            stopUp = new Point(otherPoint.x - 4, otherPoint.y - 8);
            stopDown = new Point(otherPoint.x + 4, otherPoint.y - 8);
            stopOut = new Point(otherPoint.x, otherPoint.y - 8);
            stop = stopOut;
            step = new Point(stop.x, start.y);
        } else if (otherPoint == otherBottom) {
            // init number of connectionx
            int otherCon = 1;
            // init the direction into which to move
            int otherDir = 1;
            if (otherPoint.x > thisPoint.x)
                otherDir = -1;
            // loop through others to determine position
            for (MyClass other : usingsOtherClass) {
                // check goto right
                if ((other.getCenter().y >= otherClass.getCenter().y)
                        && (other.getCenter().x >= otherClass.getCenter().x)
                        && (other.getCenter().y > thisClass.getCenter().y) && (otherDir == 1))
                    otherCon++;
                // check goto left
                if ((other.getCenter().y >= otherClass.getCenter().y)
                        && (other.getCenter().x < otherClass.getCenter().x)
                        && (other.getCenter().y > thisClass.getCenter().y) && (otherDir == -1))
                    otherCon++;
            }
            otherCon = (int) ((otherCon - 0.5) * (12 * otherDir));

            otherPoint.x += otherCon;

            stopUp = new Point(otherPoint.x - 4, otherPoint.y + 8);
            stopDown = new Point(otherPoint.x + 4, otherPoint.y + 8);
            stopOut = new Point(otherPoint.x, otherPoint.y + 8);
            stop = stopOut;
            step = new Point(stop.x, start.y);
        } else if (otherPoint == otherRight) {
            // init number of connectionx
            int otherCon = 1;
            // init the direction into which to move
            int otherDir = 1;
            if (otherPoint.y > thisPoint.y)
                otherDir = -1;
            // loop through others to determine position
            for (MyClass other : usingsOtherClass) {
                // check goto up
                if ((other.getCenter().x >= otherClass.getCenter().x)
                        && (other.getCenter().y >= otherClass.getCenter().y)
                        && (other.getCenter().x > thisClass.getCenter().x) && (otherDir == 1))
                    otherCon++;
                // check goto down
                if ((other.getCenter().x >= otherClass.getCenter().x)
                        && (other.getCenter().y < otherClass.getCenter().y)
                        && (other.getCenter().x > thisClass.getCenter().x) && (otherDir == -1))
                    otherCon++;
            }
            otherCon = (int) ((otherCon - 0.5) * (12 * otherDir));

            otherPoint.y += otherCon;

            stopUp = new Point(otherPoint.x + 8, otherPoint.y - 4);
            stopDown = new Point(otherPoint.x + 8, otherPoint.y + 4);
            stopOut = new Point(otherPoint.x + 8, otherPoint.y);
            stop = stopOut;
            step = new Point(start.x, stop.y);
        } else // left
        {
            // init number of connectionx
            int otherCon = 1;
            // init the direction into which to move
            int otherDir = 1;
            if (otherPoint.y > thisPoint.y)
                otherDir = -1;
            // loop through others to determine position
            for (MyClass other : usingsOtherClass) {
                // check goto up
                if ((other.getCenter().x < otherClass.getCenter().x)
                        && (other.getCenter().y >= otherClass.getCenter().y)
                        && (other.getCenter().x < thisClass.getCenter().x) && (otherDir == 1))
                    otherCon++;
                // check goto down
                if ((other.getCenter().x < otherClass.getCenter().x)
                        && (other.getCenter().y < otherClass.getCenter().y)
                        && (other.getCenter().x < thisClass.getCenter().x) && (otherDir == -1))
                    otherCon++;
            }
            otherCon = (int) ((otherCon - 0.5) * (12 * otherDir));

            otherPoint.y += otherCon;

            stopUp = new Point(otherPoint.x - 8, otherPoint.y - 4);
            stopDown = new Point(otherPoint.x - 8, otherPoint.y + 4);
            stopOut = new Point(otherPoint.x + 8, otherPoint.y);
            stop = stopOut;
            step = new Point(start.x, stop.y);
        }

        //            drawLine(g,thisPoint,otherPoint);

        boolean inter = false;

        /*
                
        if(otherClass.getPosition().y+otherClass.getHeight()/2 < thisClass.getPosition().y+thisClass.getHeight()/2)
        { // top
        if(stop.y>start.y)
        {
            step = new Point(start.x,thisClass.getPosition().y);
            inter = true;
        }
        else
        {
            step = new Point(start.x,stop.y);
        }
        }
        else
        { // bottom
        if(stop.y<thisClass.getPosition().y+thisClass.getHeight() || thisClass==otherClass)
        {
            step = new Point(start.x,
                             thisClass.getPosition().y+thisClass.getHeight());
            inter = true;
        }
        else
        {
            step = new Point(start.x,stop.y);
        }
                
        }
                
                
        drawLine(g,start,step);
                
        if(inter==true)
        {
        int middle;
        if(thisClass==otherClass)
        {
            middle = otherClass.getPosition().x+otherClass.getWidth()+16;//-otherCON;
        }
        else if(otherClass.getPosition().x+otherClass.getWidth()/2 > thisClass.getPosition().x+thisClass.getWidth()/2)
        { // left
            middle = (-(thisClass.getPosition().x+thisClass.getWidth())+(otherClass.getPosition().x))/2+thisClass.getPosition().x+thisClass.getWidth();
        }
        else
        { // right
            middle = (-(otherClass.getPosition().x+otherClass.getWidth())+(thisClass.getPosition().x))/2+otherClass.getPosition().x+otherClass.getWidth();
        }
        Point next = new Point(middle,step.y);
        drawLine(g,step,next);
        step = new Point(middle,stop.y);
        drawLine(g,step,next);
        }
        */
        /*
        g.setColor(Color.red);
        drawLine(g,start,step);
        drawLine(g,step,stop);
                
        g.setColor(Color.blue);
        step = new Point(stop.x,start.y);
        drawLine(g,start,step);
        drawLine(g,step,stop);
                
        g.setColor(Color.orange);
        drawLine(g,start,
               new Point(start.x,(start.y+stop.y)/2));
        drawLine(g,new Point(start.x,(start.y+stop.y)/2),
               new Point((start.x+stop.y)/2,(start.y+stop.y)/2));
        drawLine(g,new Point((start.x+stop.y)/2,(start.y+stop.y)/2),
               new Point((start.x+stop.y)/2,stop.y));
        drawLine(g,new Point((start.x+stop.y)/2,stop.y),
               stop);
                
        g.setColor(Color.black);/**/

        drawLine(g, start, step);
        drawLine(g, step, stop);

        drawLine(g, otherPoint, stop);
        g.setStroke(oldStroke);
        drawLine(g, stopUp, otherPoint);
        drawLine(g, stopDown, otherPoint);
    }
}