Example usage for java.awt Graphics2D drawRoundRect

List of usage examples for java.awt Graphics2D drawRoundRect

Introduction

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

Prototype

public abstract void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight);

Source Link

Document

Draws an outlined round-cornered rectangle using this graphics context's current color.

Usage

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

From source file: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  .  ja  va2  s.c  o 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:net.sqs2.omr.session.logic.PageImageRenderer.java

private static void drawFormAreas(int pageIndex, float densityThreshold, FormMaster master,
        PageTaskResult pageTaskResult, Graphics2D g, MarkRecognitionConfig markRecognizationConfig,
        DeskewedImageSource pageSource, int focusedColumnIndex, Rectangle scope) {
    int formAreaIndexInPage = 0;

    int minX = Integer.MAX_VALUE;
    int minY = Integer.MAX_VALUE;
    int maxX = Integer.MIN_VALUE;
    int maxY = Integer.MIN_VALUE;

    for (FormArea formArea : master.getFormAreaListByPageIndex(pageIndex)) {
        FormAreaResult result = (FormAreaResult) pageTaskResult.getPageAreaResultList()
                .get(formAreaIndexInPage);
        if (formArea.isMarkArea()) {
            if (focusedColumnIndex == formArea.getQuestionIndex()) {

                Rectangle rect = formArea.getRect();

                Point2D p1 = pageSource.getPoint((int) rect.getX(), (int) rect.getY());
                Point2D p2 = pageSource.getPoint((int) (rect.getX() + rect.getWidth()),
                        (int) (rect.getY() + rect.getHeight()));

                minX = Math.min(minX, (int) p1.getX());
                minY = Math.min(minY, (int) p1.getY());
                maxX = Math.max(maxX, (int) p2.getX());
                maxY = Math.max(maxY, (int) p2.getY());

                if (result.getDensity() < densityThreshold) {
                    g.setColor(FOCUSED_MARKED_COLOR);
                } else {
                    g.setColor(FOCUSED_NO_MARKED_COLOR);
                }/*ww w  .j av a  2 s  . c om*/

            } else {
                if (result.getDensity() < densityThreshold) {
                    g.setColor(MARKED_COLOR);
                } else {
                    g.setColor(NO_MARKED_COLOR);
                }
            }

            g.fillPolygon(pageSource.createRectPolygon(
                    getExtendedRectangle(formArea.getRect(), markRecognizationConfig.getHorizontalMargin(),
                            markRecognizationConfig.getVerticalMargin())));
            g.drawPolygon(pageSource.createRectPolygon(
                    getExtendedRectangle(formArea.getRect(), markRecognizationConfig.getHorizontalMargin() + 3,
                            markRecognizationConfig.getVerticalMargin() + 3)));

        } else {
            g.setColor(TEXTAREA_COLOR);
            g.fillPolygon(pageSource.createRectPolygon(formArea.getRect()));
        }
        formAreaIndexInPage++;
    }

    if (scope != null) {
        int borderMarginX = 20;
        int borderMarginY = 3;
        int margin = 40;

        int x = minX - borderMarginX;
        int y = minY - borderMarginY;
        int width = maxX - minX + borderMarginX * 2;
        int height = maxY - minY + borderMarginY * 2;

        scope.x = minX - margin;
        scope.y = minY - margin;
        scope.width = maxX - minX + margin * 2;
        scope.height = maxY - minY + margin * 2;

        Stroke stroke = new BasicStroke(4.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND, 2.0f,
                new float[] { 4.0f, 8.0f }, 0.0f);
        g.setStroke(stroke);
        g.setColor(FOCUSED_SCOPE_COLOR);
        g.drawRoundRect(x, y, width, height, 20, 20);
    }

}

From source file:org.squidy.designer.components.versioning.Versioning.java

@Override
protected void paintShape(PPaintContext paintContext) {
    super.paintShape(paintContext);

    Graphics2D g = paintContext.getGraphics();

    PBounds bounds = getBoundsReference();
    double x = bounds.getX();
    double y = bounds.getY();
    double width = bounds.getWidth();
    double height = bounds.getHeight();

    //      g.setColor(Color.RED);
    //      g.draw(bounds);

    g.setClip(bounds);//from ww w  .ja va  2s . c  o  m

    for (int i = 0; i < 7; i++) {
        PAffineTransform transform = new PAffineTransform();
        transform.scale(0.12, 0.12);
        transform.translate(i * 122, 0);

        paintContext.pushTransform(transform);

        if (!isRenderPrimitive()) {
            versionNode.fullPaint(paintContext);
        }

        if (i == 6) {
            g.setStroke(StrokeUtils.getBasicStroke(5f));
            g.setColor(Color.GRAY);
            if (isRenderPrimitiveRect())
                g.drawRect((int) x, (int) y, 100, (int) (height / 0.12));
            else
                g.drawRoundRect((int) x, (int) y, 100, (int) (height / 0.12), 15, 15);

            g.setColor(COLOR_FILL);
            if (isRenderPrimitiveRect())
                g.fillRect((int) x, (int) y, 100, (int) (height / 0.12));
            else
                g.fillRoundRect((int) x, (int) y, 100, (int) (height / 0.12), 15, 15);
        }

        paintContext.popTransform(transform);
    }

    g.setClip(null);
}

From source file:org.squidy.designer.zoom.impl.DataTypeShape.java

@Override
protected void paintShapeZoomedIn(PPaintContext paintContext) {

    Graphics2D g = paintContext.getGraphics();

    paintContext.pushTransform(translation);

    PBounds bounds = getBoundsReference();
    // Rectangle rectBounds = roundedRectangleShape.getBounds();

    int x = (int) bounds.x - 3;
    int y = (int) bounds.y - 3;
    int width = (int) bounds.width + 6;
    int height = (int) bounds.height;

    g.setColor(Color.WHITE);/* w ww .j  a  va  2s  .  c o  m*/
    g.fillRoundRect(x - 40, y, width + 80, height, 825, 825);
    g.setStroke(STROKE_SHAPE);
    g.setColor(Color.BLACK);
    g.drawRoundRect(x - 40, y, width + 80, height, 825, 825);

    paintContext.popTransform(translation);

    // 1st vertical
    g.drawLine(1000, 100, 1000, 220);

    // 1st horizontal
    g.drawLine(230, 220, 1780, 220);

    // 2nd (left) vertical
    g.drawLine(230, 220, 230, 700);
    // 2nd (left-middle) vertical
    g.drawLine(740, 220, 740, 960);
    // 2d (right-middle) vertical
    g.drawLine(1260, 220, 1260, 700);
    // 2nd (right) vertical
    g.drawLine(1780, 220, 1780, 700);
}

From source file:org.squidy.designer.zoom.NavigationShape.java

@Override
protected void paintShapeZoomedIn(PPaintContext paintContext) {
    super.paintShapeZoomedIn(paintContext);

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

    if (showNavigation) {// && !isHierarchicalZoomInProgress()) {

        PBounds bounds = getBoundsReference();

        int x = (int) bounds.getX();
        int y = (int) bounds.getY();
        int width = (int) bounds.getWidth();
        int height = (int) bounds.getHeight();

        g.setColor(Constants.Color.COLOR_SHAPE_BACKGROUND);
        if (isRenderPrimitiveRect())
            g.fillRect(x, y, width, 60);
        else {//w w w  .ja va  2  s  .  c  om
            g.clearRect(x, y, width, 60);
            g.fillRoundRect(x, y, width, 60, 25, 25);
        }

        g.setColor(Constants.Color.COLOR_SHAPE_BORDER);
        if (isRenderPrimitiveRect())
            g.drawRect(x, y, width, 60);
        else
            g.drawRoundRect(x, y, width, 60, 25, 25);

        g.setFont(fontBreadcrumb);

        if (titleBounds == null) {
            FontMetrics fm = g.getFontMetrics();
            titleBounds = new Rectangle2D.Double(bounds.getX() + 455 + titleGap,
                    bounds.getY() + 25 - fm.getHeight(), FontUtils.getWidthOfText(fm, getTitle()) + 10,
                    fm.getHeight() + 5);
        }

        // Font font = internalFont.deriveFont(3.2f);
        for (int i = 0; i < 3; i++) {
            if (isRenderPrimitiveRect())
                g.fillRect((int) (bounds.getX() + 430), (int) bounds.getY() + i * 15 + 10, 5, 10);
            else
                g.fillOval((int) (bounds.getX() + 430), (int) bounds.getY() + i * 15 + 10, 5, 10);
        }

        if (!ShapeUtils.isApparent(titleInputWrapper)) {
            g.drawString(getTitle(), (int) (bounds.getX() + 460 + titleGap), (int) (bounds.getY() + 25));
        }

        if (croppedBreadcrumb == null) {
            croppedBreadcrumb = FontUtils.createCroppedLabelIfNecessary(g.getFontMetrics(), getBreadcrumb(),
                    (int) bounds.getWidth() * 10 - 450);
        }
        g.drawString(croppedBreadcrumb, (int) (bounds.getX() + 460), (int) (bounds.getY() + 50));
    }
}

From source file:org.uva.itast.blended.omr.scanners.BarcodeScanner.java

/**
 * @param campo//from   w  w w. j  a  v a  2s.  c  o  m
 */
public void markBarcode(Field campo) {
    try {
        //get bbox in pixels
        Rectangle rect = pageImage.toPixels(campo.getBBox());
        // expand the area for some tolerance
        Rectangle2D expandedArea = getExpandedArea(campo.getBBox(), (float) BARCODE_AREA_PERCENT);
        Rectangle expandedRect = pageImage.toPixels(expandedArea);

        Graphics2D g = pageImage.getReportingGraphics();
        AffineTransform t = g.getTransform();
        g.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND, 1,
                new float[] { (float) (3 / t.getScaleX()), (float) (6 / t.getScaleY()) }, 0));
        if (lastResult != null)
            g.setColor(Color.BLUE);
        else
            g.setColor(Color.RED);

        g.drawRoundRect(rect.x, rect.y, rect.width, rect.height, 3, 3);
        g.drawRoundRect(expandedRect.x, expandedRect.y, expandedRect.width, expandedRect.height, 3, 3);

        g.setFont(new Font("Arial", Font.BOLD, (int) (12 / t.getScaleX())));
        String message;
        if (lastResult != null)
            message = ((Result) lastResult.getResult()).getBarcodeFormat().toString() + "="
                    + getParsedCode(lastResult);
        else
            message = "UNRECOGNIZED!";
        g.drawString(message, rect.x, rect.y);

    } catch (Exception e) {
        logger.error("Unexpected errr while logging the image:", e);
    }

}