Example usage for java.awt Graphics2D getStroke

List of usage examples for java.awt Graphics2D getStroke

Introduction

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

Prototype

public abstract Stroke getStroke();

Source Link

Document

Returns the current Stroke in the Graphics2D context.

Usage

From source file:de.codesourcery.planning.swing.DateAxis.java

public BoundingBox render(ITimelineCallback callback, boolean layoutOnly) {
    final Calendar cal = Calendar.getInstance();
    cal.setTime(startDate);/*from  w  w  w .  j a v a 2 s. c o m*/
    cal.set(Calendar.MILLISECOND, 0);
    Date currentDate = cal.getTime();

    final Date endDate = duration.addTo(startDate);

    BoundingBox lastLabel = null;

    final int labelSpacing = 10;

    final Graphics2D graphics = callback.getGraphics();
    final int fontHeight = graphics.getFontMetrics().getHeight();

    final double scalingFactor = getXScalingFactor(callback.getBoundingBox());
    final BoundingBox box = callback.getBoundingBox();
    double x = callback.getBoundingBox().getX();
    final int tickToLabelSpacing = 2;
    final int tickLength = fontHeight;
    final int axisHeight = fontHeight + tickLength + tickToLabelSpacing;
    final double xIncrement = Math.floor(tickDuration.toSeconds() * scalingFactor);

    final Color oldColor = graphics.getColor();
    //      
    while (currentDate.compareTo(endDate) <= 0) {
        final int currentX = (int) Math.floor(x);
        if (lastLabel == null || lastLabel.getMaxX() < x) {
            final String labelText = callback.getLabelProvider().getTimelineLabel(currentDate);
            if (!StringUtils.isBlank(labelText)) {
                final Rectangle2D stringBounds = callback.getStringBounds(labelText);

                if (!layoutOnly) {
                    graphics.setColor(Color.BLACK);
                    // draw tick
                    final Stroke oldStroke = graphics.getStroke();
                    graphics.setStroke(new BasicStroke(2.0f));
                    graphics.drawLine(currentX, box.getY() + axisHeight, currentX,
                            box.getY() + fontHeight + tickToLabelSpacing);
                    graphics.setStroke(oldStroke);

                    // draw label
                    callback.drawString(Color.BLACK, currentX, box.getY(), labelText);
                }

                final BoundingBox labelBox = new BoundingBox(currentX, box.getY(),
                        currentX + (int) stringBounds.getWidth() + labelSpacing,
                        box.getY() + (int) stringBounds.getHeight());

                if (lastLabel == null) {
                    lastLabel = labelBox;
                } else {
                    lastLabel.add(labelBox);
                }

            }
        } else {
            // draw short tick
            if (!layoutOnly) {

                final int halfTickHeight = (int) Math.floor(tickLength / 2.0d);

                graphics.drawLine(currentX, box.getY() + axisHeight, currentX,
                        box.getY() + axisHeight - halfTickHeight);
            }
        }

        // draw part of axis
        if (!layoutOnly) {
            graphics.drawLine((int) x, box.getY() + axisHeight, (int) (x + xIncrement),
                    box.getY() + axisHeight);
        }
        x += xIncrement;
        currentDate = tickDuration.addTo(currentDate);
    }

    callback.getGraphics().setColor(oldColor);
    final BoundingBox result = lastLabel != null ? lastLabel : new BoundingBox(0, 0, 0, 0);
    result.incHeight(axisHeight);
    return result;
}

From source file:edu.uci.ics.jung.visualization.PluggableRenderer.java

/**
 * Paints the vertex <code>v</code> at the location <code>(x,y)</code>
 * on the graphics context <code>g_gen</code>.  The vertex is painted
 * using the shape returned by this instance's <code>VertexShapeFunction</code>,
 * and the foreground and background (border) colors provided by this
 * instance's <code>VertexColorFunction</code>.  Delegates drawing the
 * label (if any) for this vertex to <code>labelVertex</code>.
 *///from   w  w w  .j  av a2 s.  c  o  m
public void paintVertex(Graphics g, Vertex v, int x, int y) {
    if (!vertexIncludePredicate.evaluate(v))
        return;

    boolean vertexHit = true;
    Rectangle deviceRectangle = null;
    Graphics2D g2d = (Graphics2D) g;
    if (screenDevice != null) {
        Dimension d = screenDevice.getSize();
        if (d.width <= 0 || d.height <= 0) {
            d = screenDevice.getPreferredSize();
        }
        deviceRectangle = new Rectangle(0, 0, d.width, d.height);
    }

    Stroke old_stroke = g2d.getStroke();
    Stroke new_stroke = vertexStrokeFunction.getStroke(v);
    if (new_stroke != null) {
        g2d.setStroke(new_stroke);
    }
    // get the shape to be rendered
    Shape s = vertexShapeFunction.getShape(v);

    // create a transform that translates to the location of
    // the vertex to be rendered
    AffineTransform xform = AffineTransform.getTranslateInstance(x, y);
    // transform the vertex shape with xtransform
    s = xform.createTransformedShape(s);

    vertexHit = viewTransformer.transform(s).intersects(deviceRectangle);

    if (vertexHit) {

        if (vertexIconFunction != null) {
            paintIconForVertex(g2d, v, x, y);
        } else {
            paintShapeForVertex(g2d, v, s);
        }

        if (new_stroke != null) {
            g2d.setStroke(old_stroke);
        }
        String label = vertexStringer.getLabel(v);
        if (label != null) {
            labelVertex(g, v, label, x, y);
        }
    }
}

From source file:com.joey.software.regionSelectionToolkit.controlers.ImageProfileTool.java

@Override
public void draw(Graphics2D g) {
    if (isBlockUpdate()) {
        return;//from www .ja  va  2s.co m
    }
    updateSelectionData();

    float tra = (transparance.getValue() / (float) (transparance.getMaximum()));

    float lineSize = 1f / (float) panel.getScale();
    float crossSize = (float) (pointSize / panel.getScale());

    RenderingHints oldHinds = g.getRenderingHints();
    Composite oldcomp = g.getComposite();
    Stroke oldStroke = g.getStroke();
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, tra));
    g.setStroke(new BasicStroke(lineSize));
    GraphicsToolkit.setRenderingQuality(g, GraphicsToolkit.HIGH_QUALITY);
    Line2D.Double line = new Line2D.Double();

    double x1 = 0;
    double x2 = 0;
    double y1 = 0;
    double y2 = 0;
    for (int i = 0; i < selectionData.length - 1; i++) {
        if (axis == AXIS_X) {
            y1 = view.getImage().getHeight() / (double) (selectionData.length - 1) * i;
            x1 = view.getImage().getWidth() * (1 - getSelectionValue(i));

            y2 = view.getImage().getHeight() / (double) (selectionData.length - 1) * (i + 1);
            x2 = view.getImage().getWidth() * (1 - getSelectionValue(i + 1));
        } else if (axis == AXIS_Y) {
            x1 = view.getImage().getWidth() / (double) (selectionData.length - 1) * i;
            y1 = view.getImage().getHeight() * (1 - getSelectionValue(i));

            x2 = view.getImage().getWidth() / (double) (selectionData.length - 1) * (i + 1);
            y2 = view.getImage().getHeight() * (1 - getSelectionValue(i + 1));
        }

        if (getUseData(i)) {
            g.setColor(getPointColorSelected());
        } else {
            g.setColor(getPointColorNotSelected());
        }
        line.x1 = x1;
        line.x2 = x2;
        line.y1 = y1;
        line.y2 = y2;

        g.draw(line);

        if (showOffset.isSelected()) {
            if (getUseData(i)) {
                g.setColor(getOffsetColor());
                double offset = (Double) this.offset.getValue();

                if (axis == AXIS_X) {
                    line.x1 += offset;
                    line.x2 += offset;

                } else if (axis == AXIS_Y) {
                    line.y1 += offset;
                    line.y2 += offset;
                }
                g.draw(line);
            }
        }
    }

    // Draw each point
    if (drawCrosses) {
        double x = 0;
        double y = 0;
        for (int i = 0; i < value.length; i++) {

            if (axis == AXIS_X) {
                y = view.getImage().getHeight() / (double) (value.length - 1) * i;
                x = view.getImage().getWidth() * (1 - value[i]);
            } else if (axis == AXIS_Y) {
                x = view.getImage().getWidth() / (double) (value.length - 1) * i;
                y = view.getImage().getHeight() * (1 - value[i]);
            }
            DrawTools.drawCross(g, new Point2D.Double(x, y), crossSize, 0, getCrossColor(), lineSize);

        }
    }
    g.setComposite(oldcomp);
    g.setStroke(oldStroke);
    g.setRenderingHints(oldHinds);
}

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

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

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

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

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

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

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

        index = 0;
    }

    state = STATE_POINTS;

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

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

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

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

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

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

            g2.setPaint(paintList[i]);

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

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

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

            stepperList[i].step();
        }

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

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

    }
    state = STATE_FINISHED;
}

From source file: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 . j av  a  2 s .co  m*/
        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);
    }
}

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  www.  j  av a2s  . co m
    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:org.dwfa.ace.graph.AceGraphRenderer.java

/**
 * Paints the vertex <code>v</code> at the location <code>(x,y)</code> on
 * the graphics context <code>g_gen</code>. The vertex is painted
 * using the shape returned by this instance's
 * <code>VertexShapeFunction</code>,
 * and the foreground and background (border) colors provided by this
 * instance's <code>VertexColorFunction</code>. Delegates drawing the
 * label (if any) for this vertex to <code>labelVertex</code>.
 *///from  w w  w.  j  a  va  2s  .c o m
public void paintVertex(Graphics g, Vertex v, int x, int y) {
    if (!vertexIncludePredicate.evaluate(v))
        return;

    boolean vertexHit = true;
    Rectangle deviceRectangle = null;
    Graphics2D g2d = (Graphics2D) g;
    if (screenDevice != null) {
        Dimension d = screenDevice.getSize();
        if (d.width <= 0 || d.height <= 0) {
            d = screenDevice.getPreferredSize();
        }
        deviceRectangle = new Rectangle(0, 0, d.width, d.height);
    }

    Stroke old_stroke = g2d.getStroke();
    Stroke new_stroke = vertexStrokeFunction.getStroke(v);
    if (new_stroke != null) {
        g2d.setStroke(new_stroke);
    }
    // get the shape to be rendered
    Shape s = vertexShapeFunction.getShape(v);

    // create a transform that translates to the location of
    // the vertex to be rendered
    AffineTransform xform = AffineTransform.getTranslateInstance(x, y);
    // transform the vertex shape with xtransform
    s = xform.createTransformedShape(s);

    if (deviceRectangle == null) {
        vertexHit = false;
    } else {
        vertexHit = viewTransformer.transform(s).intersects(deviceRectangle);
    }

    if (vertexHit) {

        if (vertexShapeRendered) {
            if (vertexIconFunction != null) {
                paintIconForVertex(g2d, v, x, y);
            } else {
                paintShapeForVertex(g2d, v, s);
            }
        }

        if (new_stroke != null) {
            g2d.setStroke(old_stroke);
        }
        String label = vertexStringer.getLabel(v);
        if (label != null) {
            labelVertex(g, v, label, x, y);
        }
    }
}

From source file:org.esa.snap.graphbuilder.rcp.dialogs.support.GraphNode.java

/**
 * Draws an arrow head at the correct angle
 *
 * @param g     The Java2D Graphics//from   w  ww . j  a v a2 s  . c  o m
 * @param tailX position X on target node
 * @param tailY position Y on target node
 * @param headX position X on source node
 * @param headY position Y on source node
 */
private static void drawArrow(final Graphics2D g, final int tailX, final int tailY, final int headX,
        final int headY) {

    final double t1 = Math.abs(headY - tailY);
    final double t2 = Math.abs(headX - tailX);
    double theta = Math.atan(t1 / t2);
    if (headX >= tailX) {
        if (headY > tailY)
            theta = Math.PI + theta;
        else
            theta = -(Math.PI + theta);
    } else if (headX < tailX && headY > tailY)
        theta = 2 * Math.PI - theta;
    final double cosTheta = FastMath.cos(theta);
    final double sinTheta = FastMath.sin(theta);

    final Point p2 = new Point(-8, -3);
    final Point p3 = new Point(-8, +3);

    int x = (int) Math.round((cosTheta * p2.x) - (sinTheta * p2.y));
    p2.y = (int) Math.round((sinTheta * p2.x) + (cosTheta * p2.y));
    p2.x = x;
    x = (int) Math.round((cosTheta * p3.x) - (sinTheta * p3.y));
    p3.y = (int) Math.round((sinTheta * p3.x) + (cosTheta * p3.y));
    p3.x = x;

    p2.translate(tailX, tailY);
    p3.translate(tailX, tailY);

    Stroke oldStroke = g.getStroke();
    g.setStroke(new BasicStroke(2));
    g.drawLine(tailX, tailY, headX, headY);
    g.drawLine(tailX, tailY, p2.x, p2.y);
    g.drawLine(p3.x, p3.y, tailX, tailY);
    g.setStroke(oldStroke);
}

From source file:org.kalypso.kalypso1d2d.pjt.map.HydrographManagementWidget.java

private void paintHydrographInMap(final Graphics g) {
    final Graphics2D g2 = (Graphics2D) g;
    final GM_Point point = (GM_Point) m_selectedHydrograph.getLocation();
    final IMapPanel mapPanel = getMapPanel();

    if (mapPanel == null)
        return;/* ww  w. j a  va2 s. c o  m*/

    final GeoTransform projection = mapPanel.getProjection();
    if (projection == null || point == null)
        return;

    final int x = (int) projection.getDestX(point.getX());
    final int y = (int) projection.getDestY(point.getY());

    final int sizeOuter = 16;
    final Color defaultColor = g2.getColor();
    final Color color = new Color(255, 30, 30);
    g2.setColor(color);

    final Stroke defaultStroke = g2.getStroke();
    final BasicStroke stroke = new BasicStroke(3);
    g2.setStroke(stroke);

    g2.drawRect(x - sizeOuter / 2, y - sizeOuter / 2, sizeOuter, sizeOuter);

    g2.setColor(defaultColor);
    g2.setStroke(defaultStroke);
}

From source file:org.photovault.swingui.PhotoCollectionThumbView.java

@Override
public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2 = (Graphics2D) g;
    Rectangle clipRect = g.getClipBounds();
    Dimension compSize = getSize();
    // columnCount = (int)(compSize.getWidth()/columnWidth);

    int photoCount = 0;
    if (photos != null) {
        photoCount = photos.size();/*from   w  ww  .j av a2s  .c o  m*/
    }

    // Determine the grid size based on couln & row count
    columnsToPaint = columnCount;
    // if columnCount is not specified determine it based on row count
    if (columnCount < 0) {
        if (rowCount > 0) {
            columnsToPaint = photoCount / rowCount;
            if (columnsToPaint * rowCount < photoCount) {
                columnsToPaint++;
            }
        } else {
            columnsToPaint = (int) (compSize.getWidth() / columnWidth);
        }
    }

    int col = 0;
    int row = 0;
    Rectangle thumbRect = new Rectangle();

    for (PhotoInfo photo : photos) {
        thumbRect.setBounds(col * columnWidth, row * rowHeight, columnWidth, rowHeight);
        if (thumbRect.intersects(clipRect)) {
            if (photo != null) {
                paintThumbnail(g2, photo, col * columnWidth, row * rowHeight, selection.contains(photo));
            }
        }
        col++;
        if (col >= columnsToPaint) {
            row++;
            col = 0;
        }
    }

    // Paint the selection rectangle if needed
    if (dragSelectionRect != null) {
        Stroke prevStroke = g2.getStroke();
        Color prevColor = g2.getColor();
        g2.setStroke(new BasicStroke(1.0f));
        g2.setColor(Color.BLACK);
        g2.draw(dragSelectionRect);
        g2.setColor(prevColor);
        g2.setStroke(prevStroke);
        lastDragSelectionRect = dragSelectionRect;
    }
}