Search in sources :

Example 1 with DrawStringMultiLine

use of course_generator.utils.DrawStringMultiLine in project Course_Generator by patrovite.

the class PanelProfilMRB method paintComponent.

/**
 * Refresh the panel
 */
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    // private void RefreshProfil(bool WithHighlight)
    int nbtag = 0;
    double[] stepval = { 1, 2, 2.5, 5 };
    double[] stepval2 = { 0.1, 0.1, 0.2, 0.1 };
    String s;
    double resx = 0.0;
    double resy = 0.0;
    // double tf = 0;
    // boolean first = false;
    int[] TabY = new int[width];
    for (int i = 0; i < width; i++) TabY[i] = 0;
    // Calculate the profile width
    wp = width - offx - rMargin;
    PixelPerHUnit = wp / ((int) (track.getTotalDistance(settings.Unit) / 1000.0));
    // Calculate the profile height
    Double dElev = track.getMaxElev(settings.Unit) - track.getMinElev(settings.Unit);
    if (dElev < 100) {
        dElev = Math.ceil(track.getMaxElev(settings.Unit) / 10.0) * 10.0;
    } else {
        dElev = (Math.ceil(track.getMaxElev(settings.Unit) / 100.0) * 100.0) - (Math.floor(track.getMinElev((settings.Unit)) / 100.0) * 100.0);
    }
    hp = height - offy - track.TopMargin;
    PixelPerVUnit = dElev / hp;
    // Font FontSmall = new Font("ARIAL", Font.PLAIN, 7);
    Font FontGraduation = new Font("ARIAL", Font.PLAIN, 10);
    // Font FontLabel = new Font("ARIAL", Font.PLAIN, 11);
    // CalcLineResult res = new CalcLineResult();
    // -- Main frame display with white background
    g2d.setPaint(Color.WHITE);
    g2d.fillRect(0, 0, width, height);
    g2d.setStroke(PenBlackS);
    g2d.setPaint(Color.BLACK);
    g2d.drawRect(0, 0, width - 1, height - 1);
    // -- Some tests in order to be sure that we have data
    if (data == null)
        return;
    if (settings == null)
        return;
    if (track == null)
        return;
    if (track.data.isEmpty())
        return;
    // -- Vertical axis drawing
    // Position of the arrow compare to the top of the profile
    int YPosArrow = 15;
    g2d.setStroke(PenBlackS);
    g2d.drawLine(offx, track.TopMargin - YPosArrow, offx, height - offy + 3);
    // -- End of the vertical axis drawing
    g2d.setPaint(Color.BLACK);
    int[] x = { offx - 5, offx, offx + 5 };
    int[] y = { track.TopMargin - YPosArrow - 1, track.TopMargin - YPosArrow - 10, track.TopMargin - YPosArrow - 1 };
    g2d.fillPolygon(x, y, 3);
    // -- Vertical unit drawing
    s = settings.getShortElevationUnitString();
    Dimension d = Utils.StringDimension(g2d, s);
    g2d.drawString(s, offx - d.width - 8, track.TopMargin - YPosArrow);
    // -- Horizontal axis drawing
    g2d.drawLine(offx - 3, height - offy, width - 10, height - offy);
    // -- End of the horizontal axis drawing
    int[] x1 = { width - 15, width - 7, width - 15 };
    int[] y1 = { height - 20, height - 25, height - 30 };
    g2d.fillPolygon(x1, y1, 3);
    // -- Horizontal unit drawing
    s = settings.getShortDistanceUnitString();
    d = Utils.StringDimension(g2d, s);
    g2d.drawString(s, width - d.width - 2, height - offy - 10);
    Double ymin = 0.0;
    Double ymax = 0.0;
    Double xmin = 0.0;
    Double xmax = 0.0;
    Double stepx = 0.0;
    Double stepy = 0.0;
    Double stepx2 = 0.0;
    Double stepy2 = 0.0;
    int NbGradX = 0;
    int NbGradY = 0;
    // -- Min and max of the X axis
    xmin = 0.0;
    xmax = track.getTotalDistance(settings.Unit);
    // -- X axis ticks calculation
    int ctab = 0;
    int cmul = 0;
    double p = 1.0;
    boolean ok = false;
    while (!ok) {
        ctab = 0;
        while (ctab < 4 && !ok) {
            cmul = 5;
            while (cmul <= 10 && !ok) {
                if (xmin + p * stepval[ctab] * cmul > xmax)
                    ok = true;
                else
                    cmul++;
            }
            if (!ok)
                ctab++;
        }
        if (!ok)
            p = p * 10;
    }
    stepx = stepval[ctab] * p;
    stepx2 = stepval2[ctab] * stepx;
    NbGradX = cmul;
    xmax = xmin + stepx * NbGradX;
    // -- Min and max of the y axis calculation
    if (track.getMaxElev(settings.Unit) - track.getMinElev(settings.Unit) < 100)
        ymin = 0.0;
    else
        ymin = Math.floor(track.getMinElev(settings.Unit) / 100.0) * 100.0;
    ymax = track.getMaxElev(settings.Unit);
    // -- Y axis ticks calculation
    ctab = 0;
    cmul = 0;
    p = 1.0;
    ok = false;
    while (!ok) {
        ctab = 0;
        while (ctab < 4 && !ok) {
            cmul = 5;
            while (cmul <= 10 && !ok) {
                if (ymin + p * stepval[ctab] * cmul > ymax)
                    ok = true;
                else
                    cmul++;
            }
            if (!ok)
                ctab++;
        }
        if (!ok)
            p = p * 10;
    }
    stepy = stepval[ctab] * p;
    stepy2 = stepval2[ctab] * stepy;
    NbGradY = cmul;
    ymax = ymin + stepy * NbGradY;
    resx = wp / (xmax - xmin);
    resy = hp / (ymax - ymin);
    // -- Night background
    if (track.bNightCoeff && track.bShowNightDay) {
        g2d.setStroke(PenBlackS);
        g2d.setPaint(Color.LIGHT_GRAY);
        int fs = 0;
        DateTime hour = new DateTime();
        Double total = 0.0;
        for (CgData r : track.data) {
            hour = r.getHour();
            total = r.getTotal(settings.Unit);
            int ts_val = hour.getSecondOfDay();
            int ts_start = track.StartNightTime.getSecondOfDay();
            int ts_end = track.EndNightTime.getSecondOfDay();
            if ((ts_val > ts_start) || (ts_val < ts_end)) {
                if (fs == 0)
                    fs = (int) Math.round((offx + 1 + ((total - xmin) * resx)));
            } else {
                if (fs != 0) {
                    int tx = (int) Math.round((offx + 1 + ((total - xmin) * resx)));
                    int ty = height - offy;
                    g2d.fillRect(fs, 0, tx - fs, ty);
                    fs = 0;
                }
            }
        }
        if (fs != 0) {
            int tx = (int) Math.round((offx + 1 + ((total - xmin) * resx)));
            int ty = height - offy;
            g2d.fillRect(fs, 0, tx - fs, ty);
            fs = 0;
        }
    }
    // -- Horizontal line --
    // -- Vertical grid drawing --
    g2d.setStroke(PenBlackS);
    g2d.setPaint(Color.BLACK);
    for (double i = xmin; i <= xmax; i += stepx2) {
        g2d.drawLine(offx + (int) (i * resx), height - offy + 2, offx + (int) (i * resx), height - offy);
    }
    for (double i = xmin; i <= xmax; i += stepx) {
        g2d.setStroke(myPenDot);
        g2d.drawLine(offx + (int) (i * resx), height - offy, offx + (int) (i * resx), height - offy - hp);
        g2d.setStroke(PenBlackS);
        g2d.drawLine(offx + (int) (i * resx), height - offy + 4, offx + (int) (i * resx), height - offy);
    }
    // -- Horizontal grid drawing --
    g2d.setStroke(PenBlackS);
    for (double i = ymin; i <= ymax; i += stepy2) {
        g.drawLine(offx - 2, height - offy - (int) ((i - ymin) * resy), offx, height - offy - (int) ((i - ymin) * resy));
    }
    for (double i = ymin; i <= ymax; i += stepy) {
        g2d.setStroke(myPenDot);
        g.drawLine(offx, height - offy - (int) ((i - ymin) * resy), offx + wp, height - offy - (int) ((i - ymin) * resy));
        g2d.setStroke(PenBlackS);
        g.drawLine(offx - 4, height - offy - (int) ((i - ymin) * resy), offx, height - offy - (int) ((i - ymin) * resy));
    }
    // -- Y axis value drawing
    g2d.setFont(FontGraduation);
    g2d.setPaint(Color.BLACK);
    for (int c = 0; c <= NbGradY; c++) {
        s = String.format("%1.0f", (ymin + c * stepy));
        d = Utils.StringDimension(g2d, s);
        g2d.drawString(s, offx - d.width - 5, (int) ((height - offy) - (c * stepy * resy)) + (d.height / 2));
    }
    // -- Draw the value of the X axis
    for (int c = 0; c <= NbGradX; c++) {
        s = String.format("%1.0f", (xmin + c * stepx) / 1000);
        // stringSize = g.MeasureString(s, FontAvr);
        d = Utils.StringDimension(g2d, s);
        g2d.drawString(s, (int) (offx + (c * stepx * resx) - (d.width / 2)), height - offy + 4 + d.height);
    }
    // -- Simple profile drawing --
    if (ProfileType == 0)
        DrawSimpleProfile(g2d, xmin, ymin, resx, resy, TabY);
    else // -- Road/path profile drawing --
    if (ProfileType == 1)
        DrawRoadPathProfile(g2d, xmin, ymin, resx, resy, TabY);
    else // -- Slope profile drawing--
    if (ProfileType == 2)
        DrawSlopeProfile(g2d, xmin, ymin, resx, resy, TabY);
    // -- Vertical line drawing --
    double dist = 0;
    int posx = 0;
    int posy = 0;
    int posy_s = 0;
    g2d.setStroke(myPenMoy);
    g2d.setColor(Color.BLACK);
    for (CgData r : data.data) {
        if ((r.OptionMiniRoadbook & CgConst.MRBOPT_SEL) != 0) {
            dist = r.getTotal(settings.Unit);
            posx = (int) (dist * resx);
            posy = height - offy - (hp + track.TopMargin - 7 - r.VPosMiniRoadbook);
            if (track.LabelToBottom)
                posy_s = height - offy;
            else
                posy_s = TabY[offx + posx];
            g2d.drawLine(offx + posx, posy_s, offx + posx, posy);
        }
    }
    // -- Text box drawing --
    int miniRoadBookGridIndex = 0;
    DrawStringMultiLine drawStringMulti = new DrawStringMultiLine();
    for (CgData r : data.data) {
        if ((r.OptionMiniRoadbook & CgConst.MRBOPT_SEL) != 0) {
            // -- Calc the number of tag
            nbtag = NbTag(r.getTag());
            g2d.setFont(new Font("ARIAL", Font.PLAIN, r.FontSizeMiniRoadbook));
            dist = r.getTotal(settings.Unit);
            posx = (int) (dist * resx);
            posy = hp + track.TopMargin - 0 - r.VPosMiniRoadbook;
            // -- Generate the text to display
            // s = "Les Contamines\n1083m - km:100\n10h23";
            // s = GenLabel("%N\n%Am - km:%D\n%H", r);
            // s = GenLabel(r.FmtLbMiniRoadbook, r);
            // s = Utils.GenLabel(r.FmtLbMiniRoadbook, r, track);
            // s = Utils.GenLabel("%N%L%Am - km:%D\n%H", r, track, settings);
            s = Utils.GenLabel(r.FmtLbMiniRoadbook, r, track, settings);
            drawStringMulti.setText(g2d, s);
            // -- Width calculation
            int w = drawStringMulti.getWidth() + 4 + 2;
            int w1 = (nbtag * 16) + ((nbtag - 1) * 2) + 4;
            if (w1 > w)
                w = w1;
            // -- Text height
            int htext = drawStringMulti.getHeight();
            // Left nothing change
            if ((r.OptionMiniRoadbook & CgConst.MRBOPT_CENTER) != 0) {
                posx = posx - (w + 4) / 2;
            }
            if ((r.OptionMiniRoadbook & CgConst.MRBOPT_RIGHT) != 0) {
                posx = posx - (w + 1);
            }
            // -- Size of the tags (with or without tags)
            int htags = 16;
            if (((r.OptionMiniRoadbook & CgConst.MRBOPT_SHOWTAGS) == 0) || (nbtag == 0))
                htags = 0;
            // -- Draw rectangle With or without selection
            if ((miniRoadBookGridIndex == SelLine) && (WithHighlight)) {
                g2d.setColor(new Color(0xFF, 0xEF, 0xBB));
                g2d.fillRect(offx + posx, height - offy - posy, w, drawStringMulti.getHeight() + htags + 4);
            } else {
                // myBrushWhite
                g2d.setColor(Color.WHITE);
                g2d.fillRect(offx + posx, height - offy - posy, w, drawStringMulti.getHeight() + htags + 4);
            }
            // PenBlackS
            g2d.setColor(Color.BLACK);
            g2d.drawRect(offx + posx, height - offy - posy, w, drawStringMulti.getHeight() + htags + 4);
            g2d.setClip(offx + posx, height - offy - posy, w, drawStringMulti.getHeight() + htags + 4);
            if (miniRoadBookGridIndex == SelLine) {
                xSel1 = offx + posx;
                ySel1 = height - offy - posy;
                xSel2 = xSel1 + w;
                ySel2 = ySel2 + htext + htags + 4;
            }
            // -- Draw the text
            g2d.setColor(Color.BLACK);
            // +
            drawStringMulti.draw(g2d, s, offx + posx + 2, height - offy - posy);
            // htext);
            // -- Set the clipping to the whole panel
            g2d.setClip(0, 0, width, height);
            if ((r.OptionMiniRoadbook & CgConst.MRBOPT_SHOWTAGS) != 0) {
                int m = 0;
                int lt = (int) ((w - w1) / 2);
                if ((r.getTag() & CgConst.TAG_HIGH_PT) != 0) {
                    Image img = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/course_generator/images/high_point.png"));
                    g2d.drawImage(img, (offx + posx + 2 + m + lt), height - offy - posy + 1 + htext, this);
                    m += 18;
                }
                if ((r.getTag() & CgConst.TAG_LOW_PT) != 0) {
                    Image img = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/course_generator/images/low_point.png"));
                    g2d.drawImage(img, (offx + posx + 2 + m + lt), height - offy - posy + 1 + htext, this);
                    m += 18;
                }
                if ((r.getTag() & CgConst.TAG_EAT_PT) != 0) {
                    Image img = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/course_generator/images/eat.png"));
                    g2d.drawImage(img, (offx + posx + 2 + m + lt), height - offy - posy + 1 + htext, this);
                    m += 18;
                }
                if ((r.getTag() & CgConst.TAG_WATER_PT) != 0) {
                    Image img = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/course_generator/images/drink.png"));
                    g2d.drawImage(img, (offx + posx + 2 + m + lt), height - offy - posy + 1 + htext, this);
                    m += 18;
                }
                if ((r.getTag() & CgConst.TAG_COOL_PT) != 0) {
                    Image img = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/course_generator/images/photo.png"));
                    g2d.drawImage(img, (offx + posx + 2 + m + lt), height - offy - posy + 1 + htext, this);
                    m += 18;
                }
                if ((r.getTag() & CgConst.TAG_NOTE) != 0) {
                    Image img = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/course_generator/images/note.png"));
                    g2d.drawImage(img, (offx + posx + 2 + m + lt), height - offy - posy + 1 + htext, this);
                    m += 18;
                }
                if ((r.getTag() & CgConst.TAG_INFO) != 0) {
                    Image img = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/course_generator/images/info.png"));
                    g2d.drawImage(img, (offx + posx + 2 + m + lt), height - offy - posy + 1 + htext, this);
                    m += 18;
                }
            }
        }
        miniRoadBookGridIndex++;
    }
    // -- Copyright --
    Image img = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/course_generator/images/copyright.png"));
    g2d.drawImage(img, width - 15, (height - offy - img.getHeight(null)) / 2, this);
    g2d.dispose();
}
Also used : Color(java.awt.Color) Dimension(java.awt.Dimension) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) Font(java.awt.Font) DateTime(org.joda.time.DateTime) CgData(course_generator.CgData) Graphics2D(java.awt.Graphics2D) DrawStringMultiLine(course_generator.utils.DrawStringMultiLine)

Example 2 with DrawStringMultiLine

use of course_generator.utils.DrawStringMultiLine in project Course_Generator by patrovite.

the class PanelPreviewLabel method paintComponent.

/**
 * Refresh the panel
 */
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    int width = this.getWidth();
    int height = this.getHeight();
    String s;
    // -- Some tests in order to be sure that we have data
    if (data == null)
        return;
    if (settings == null)
        return;
    if (track == null)
        return;
    if (track.data.isEmpty())
        return;
    // -- Text box drawing --
    DrawStringMultiLine drawStringMulti = new DrawStringMultiLine();
    g2d.setFont(new Font("ARIAL", Font.PLAIN, data.FontSizeMiniRoadbook));
    int posx = 0;
    int posy = 5;
    // -- Generate the text to display
    s = Utils.GenLabel(data.FmtLbMiniRoadbook, data, track, settings);
    drawStringMulti.setText(g2d, s);
    // -- Width calculation
    int w = drawStringMulti.getWidth() + 4 + 2;
    // -- Text height
    g2d.setColor(Color.WHITE);
    g2d.fillRect(posx, posy, w, drawStringMulti.getHeight() + 4);
    g2d.setColor(Color.BLACK);
    g2d.drawRect(posx, posy, w, drawStringMulti.getHeight() + 4);
    g2d.setClip(posx, posy, w, drawStringMulti.getHeight() + 4);
    // -- Draw the text
    g2d.setColor(Color.BLACK);
    drawStringMulti.draw(g2d, s, posx + 2, posy);
    // -- Set the clipping to the whole panel
    g2d.setClip(0, 0, width, height);
    g2d.dispose();
}
Also used : DrawStringMultiLine(course_generator.utils.DrawStringMultiLine) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D)

Aggregations

DrawStringMultiLine (course_generator.utils.DrawStringMultiLine)2 Font (java.awt.Font)2 Graphics2D (java.awt.Graphics2D)2 CgData (course_generator.CgData)1 Color (java.awt.Color)1 Dimension (java.awt.Dimension)1 Image (java.awt.Image)1 BufferedImage (java.awt.image.BufferedImage)1 DateTime (org.joda.time.DateTime)1