Example usage for java.awt Graphics translate

List of usage examples for java.awt Graphics translate

Introduction

In this page you can find the example usage for java.awt Graphics translate.

Prototype

public abstract void translate(int x, int y);

Source Link

Document

Translates the origin of the graphics context to the point (xy) in the current coordinate system.

Usage

From source file:MainClass.java

public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
    Insets insets = getBorderInsets(c);
    Color horizontalColor;//from   ww  w  .  j a v  a  2 s. co m
    Color verticalColor;
    if (c.isEnabled()) {
        boolean pressed = false;
        if (c instanceof AbstractButton) {
            ButtonModel model = ((AbstractButton) c).getModel();
            pressed = model.isPressed();
        }
        if (pressed) {
            horizontalColor = Color.WHITE;
            verticalColor = Color.BLACK;
        } else {
            horizontalColor = Color.BLACK;
            verticalColor = Color.WHITE;
        }
    } else {
        horizontalColor = Color.LIGHT_GRAY;
        verticalColor = Color.LIGHT_GRAY;
    }
    g.setColor(horizontalColor);

    g.translate(x, y);

    // top
    g.fillRect(0, 0, width, insets.top);
    // bottom
    g.fillRect(0, height - insets.bottom, width, insets.bottom);

    g.setColor(verticalColor);
    // left
    g.fillRect(0, insets.top, insets.left, height - insets.top - insets.bottom);
    // right
    g.fillRect(width - insets.right, insets.top, insets.right, height - insets.top - insets.bottom);
    g.translate(-x, -y);
}

From source file:web.diva.server.model.SomClustering.SomClustImgGenerator.java

private void drawScale(Graphics scale, Point st, int width, int height) {
    Rectangle r = new Rectangle(st.x, st.y, width, height);
    if (width < 50 || height < 25) {
        return;//  w w w. ja v a 2s . c om
    }
    ScaleAndAxis sc = new ScaleAndAxis();
    sc.setColorFactory(colors);
    scale.translate(st.x, st.y);
    Rectangle bac = scale.getClipBounds();
    sc.setLocation(r.x, r.y);
    sc.setSize(r.width, r.height);
    sc.paintComponent(scale);
    scale.setClip(bac);
    scale.translate(-st.x, -st.y);
}

From source file:web.diva.server.model.SomClustering.SomClustImgGenerator.java

private void drawSquares(Graphics squares, Point start, Rectangle bounds, Dataset dataset,
        boolean clusterColumns) {
    //        ColorFactory colors = ColorFactoryList.getInstance().getActiveColorFactory(dataset);
    colors = colorFactory.getActiveColorFactory(dataset);
    Rectangle view = getSquaresBounds(dataset);
    squares.translate(start.x, start.y);
    int rows = this.countgenes(this.rowNode);
    int counter = 0;
    double[] gengenscalevals = null;
    int[] upperArrangement = null;
    if (clusterColumns) {
        upperArrangement = upperTree.arrangement;
    } else {// w ww .j a va 2 s  . com
        upperArrangement = new int[dataset.getColumnIds().length];
        for (int x = 0; x < dataset.getColumnIds().length; x++)
            upperArrangement[x] = x;
    }
    double[][] dat = null;
    dat = dataset.getData();
    if (sideTree == null) {
        return;
    }
    for (int i = 0; i < sideTree.arrangement.length; i++) {
        double v = 0;
        Rectangle sqr = new Rectangle(0, 0, squareW, squareL);
        for (int j = 0; j < upperArrangement.length; j++) {
            if (bounds == null || bounds.intersects((j * squareW), (i * squareL), squareW, squareL)) {

                if (upperTree != null) {

                    sqr.setLocation((j * squareW), (i * squareL));
                    if (!view.intersects(sqr)) {
                        continue;
                    }

                    if (sideTree.arrangement[i] != -1 && upperArrangement[j] != -1) {

                        if (dataset.isMissing(sideTree.arrangement[i], upperArrangement[j])) {
                            squares.setColor(colors.getMissing());
                        } else {
                            if (!gengenscale) {
                                v = dat[sideTree.arrangement[i]][upperArrangement[j]];
                                squares.setColor(colors.getColor(v));
                            } else {
                                v = gengenscalevals[upperArrangement[j]];
                                squares.setColor(colors.getColor(v));
                            }
                        }
                        squares.fillRect((j * squareW), (i * squareL), squareW, squareL);
                    }
                } else {
                    sqr.setLocation((j * squareW), (i * squareL));
                    if (!view.intersects(sqr)) {
                        continue;
                    }

                    v = dat[sideTree.arrangement[i]][upperArrangement[j]];

                    if (dataset.isMissing(sideTree.arrangement[i], upperArrangement[j])) {
                        squares.setColor(colors.getMissing());
                    } else {
                        squares.setColor(colors.getColor(v));
                    }

                    squares.fillRect((j * squareW), (i * squareL), squareW, squareL);
                }
            }
        }
        counter++;
        if (counter == rows) {
            break;
        }
    }
    counter = 0;
    if (true) {
        squares.setColor(GridCol);
        for (int i = 0; i < sideTree.arrangement.length + 1; i++) {
            if (bounds == null
                    || bounds.intersects(0, i * squareL, upperArrangement.length * squareW, i * squareL)) {
                squares.drawLine(0, i * squareL, (upperArrangement.length * squareW) + 0, i * squareL);
            }
            counter++;
            if (counter > rows) {
                break;
            }
        }
        for (int j = 0; j < upperArrangement.length; j++) {
            if (bounds == null || bounds.intersects(j * squareW, 0, j * squareW, rows * squareL)) {
                squares.drawLine(j * squareW, 0, j * squareW, rows * squareL);
            }
        }

        if (bounds == null || bounds.intersects(upperArrangement.length * squareW, 0,
                upperArrangement.length * squareW, rows * squareL)) {
            squares.drawLine(upperArrangement.length * squareW, 0, upperArrangement.length * squareW,
                    rows * squareL);
        }

    }
    squares.translate(-start.x, -start.y);
}

From source file:ucar.unidata.idv.flythrough.Flythrough.java

/**
 * _more_/* ww  w.j  a v a2 s.c  om*/
 *
 * @param g2 _more_
 * @param comp _more_
 * @param ptsIdx _more_
 * @param ul _more_
 *
 * @return _more_
 */
private Rectangle drawDial(Graphics g2, JComponent comp, int ptsIdx, Point ul) {
    if (ptsIdx >= dialPts.length) {
        return null;
    }
    int w = comp.getWidth();
    int h = comp.getHeight();
    boolean useUpperLeft = false;
    if (comp instanceof ChartPanel) {
        int desiredWidth = dialPts[ptsIdx][2];
        double scale = w / (double) desiredWidth;
        if (scale != 0) {
            h = (int) (h / scale);
        }
        comp.setSize(new Dimension(desiredWidth, h));
    } else if (comp instanceof JLabel) {
        //Don't set size for labels
        useUpperLeft = true;
    } else {
        comp.setSize(new Dimension(dialPts[ptsIdx][2], dialPts[ptsIdx][3]));
    }
    try {
        int x = ul.x + dialPts[ptsIdx][0] - (useUpperLeft ? 0 : dialPts[ptsIdx][2] / 2);
        int y = ul.y + dialPts[ptsIdx][1] - (useUpperLeft ? 0 : dialPts[ptsIdx][3] / 2);
        Image image = ImageUtils.getImage(comp);
        g2.translate(x, y);
        g2.drawImage(image, 0, 0, null);
        return new Rectangle(x, y, comp.getWidth(), comp.getHeight());

    } catch (Exception exc) {
        exc.printStackTrace();
    }
    return null;

}

From source file:web.diva.server.model.SomClustering.SomClustImgGenerator.java

public void drawTable(Graphics gr, Point UL, Dataset dataset, int[] selection, Graphics navgGr,
        int countNavUnit) {

    Font f = getTableFont(squareL - 1);
    AnnotationManager annManager = AnnotationManager.getAnnotationManager();
    String[] rowIds = dataset.getRowIds();

    Set<String> annotations = dataset.getRowAnnotationNamesInUse();
    if (annotations == null) {
        annotations = annManager.getManagedRowAnnotationNames();
    }/*  w  w w  .j  a v a 2s.c  o  m*/

    String[][] inf; // row annotation matrix
    String[] headers; // header of the row annotation matrix
    if (annotations.isEmpty()) {
        inf = new String[dataset.getDataLength()][1];
        for (int i = 0; i < inf.length; i++) {
            inf[i][0] = rowIds[i];
        }
        headers = new String[] { "Row ID" };
    } else {
        headers = annotations.toArray(new String[annotations.size()]);
        inf = new String[dataset.getDataLength()][annotations.size()];
        for (int i = 0; i < headers.length; i++) {
            //ann manager need to re implemeinted?
            AnnotationLibrary anns = annManager.getRowAnnotations(headers[i]);
            for (int j = 0; j < inf.length; j++) {
                inf[j][i] = rowIds[j];//anns.getAnnotation(rowIds[j]);//
            }
        }
    }

    Graphics2D g2d = (Graphics2D) gr;
    Graphics2D g2dNav = (Graphics2D) navgGr;
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2dNav.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    int X = UL.x;
    int Y = UL.y;
    //        int H = squareL;

    int L = dataset.getDataLength();
    int W = headers.length;

    JLabel l = new JLabel("    ");
    JLabel lNav = new JLabel(" ");
    //        l.setFont(f);
    //        l.setIconTextGap(2);
    javax.swing.border.Border UB = javax.swing.BorderFactory.createMatteBorder(0, 0, 0, 0, Color.WHITE);
    javax.swing.border.Border LB = javax.swing.BorderFactory.createMatteBorder(0, 0, 0, 0, Color.WHITE);

    //          Color borderColor = hex2Rgb("#e3e3e3");
    javax.swing.border.Border navBorder = javax.swing.BorderFactory.createMatteBorder(2, 0, 0, 0, Color.WHITE);

    l.setMaximumSize(new Dimension(200, squareL));
    lNav.setSize(new Dimension(2, 5));
    lNav.setBorder(navBorder);

    boolean drawTableHeader = false;

    //if there is not enough room for a header.. skip header.
    //        if (UL.y < squareL) {
    //            drawTableHeader = false;
    //        }

    if (Wd == null) {
        Wd = new int[inf[0].length];
        WdSUM = new int[inf[0].length];

        if (drawTableHeader) {
            for (int i = 0; i < headers.length; i++) {
                l.setText(headers[i]);
                l.validate();
                if (l.getPreferredSize().width > Wd[i]) {
                    Wd[i] = l.getPreferredSize().width + 16;
                }
            }
        }
        for (String[] inf1 : inf) {
            for (int j = 0; j < Wd.length; j++) {
                if (squareL < 6) {
                    Wd[j] = 5;
                    continue;
                }
                l.setText(inf1[j]);
                l.validate();
                if (l.getPreferredSize().width > Wd[j]) {
                    Wd[j] = l.getPreferredSize().width + 16;
                }
            }
        }

        WdSUM[0] = 0;

        for (int i = 0; i < Wd.length; i++) {
            WdSUM[i] = -1;
            for (int j = 0; j < i; j++) {
                WdSUM[i] += Wd[j] + 3;
            }
        }
    }

    Rectangle BNDS = new Rectangle();

    l.setBackground(Color.WHITE);
    l.setOpaque(true);

    lNav.setBackground(Color.WHITE);
    lNav.setOpaque(true);

    if (sideTree == null) {
        return;
    }

    f = getTableFont(squareL - 1);
    l.setFont(f);

    int[] LArr = sideTree.arrangement;
    int Rindex = 0;

    //draw the table header.. (if wanted)
    //        if (drawTableHeader) {
    //
    //            l.setBackground(Color.WHITE);
    //            l.setForeground(Color.white);
    //
    //            for (int j = 0; j < W; j++) {
    //                X = UL.x + WdSUM[j];
    //                Y = UL.y;
    //                BNDS.setBounds(X, Y, Wd[j], squareL + 1);
    //
    //                if (gr.getClipBounds() != null && !gr.getClipBounds().intersects(BNDS)) {
    //                    continue;
    //                }
    //                gr.translate(X, Y);
    //                l.setBounds(0, 0, Wd[j] + 1, squareL + 1);
    //                l.setBorder(LB);
    //
    //                if (squareL >= 6) {
    //                    l.setText(headers[j]);
    //                }
    //                l.validate();
    //                l.paint(gr);
    //                gr.translate(-X, -Y);
    //            }
    //        }
    l.setForeground(Color.WHITE);

    boolean[] sel = selectedRows((selection == null ? null : selection), dataset);
    boolean coloredNav = false;
    int navCounter = 0;
    for (int i = 0; i < L; i++) {

        Rindex = LArr[i];
        for (int j = 0; j < W; j++) {
            X = UL.x + WdSUM[j];
            Y = UL.y + (squareL * (i + 1));

            BNDS.setBounds(X, Y, Wd[j], squareL + 1);

            if (gr.getClipBounds() != null && !gr.getClipBounds().intersects(BNDS)) {
                continue;
            }

            if (sel[LArr[i]]) {

                for (Group group : dataset.getRowGroups()) {
                    if (group.isActive()) {
                        if (group.hasMember(Rindex)) {
                            l.setBackground(group.getColor());
                            if (!coloredNav) {
                                lNav.setBackground(Color.RED);
                                lNav.setForeground(Color.RED);
                                coloredNav = true;
                            }

                            break;

                        }
                    }

                }

                //                    l.setBackground(new Color(225, 225, 255));
            } else {
                //                   
                //                    if (!coloredNav) {
                //                                    lNav.setBackground(Color.WHITE);
                //                                    lNav.setForeground(Color.WHITE);                                  
                //                                }

                l.setBackground(Color.WHITE);
            }
            if (i != 0)
                gr.translate(X, Y);
            l.setBounds(0, 0, Wd[j] + 1, squareL + 1);

            if (i < L - 1) {
                l.setBorder(UB);
            } else {
                l.setBounds(0, 0, Wd[j] + 1, squareL + 1);
                l.setBorder(LB);
            }
            if (squareL >= 6) {
                l.setText(inf[Rindex][j]);
            }
            l.validate();
            l.paint(gr);
            gr.translate(-X, -Y);

        }
        if (navCounter >= countNavUnit) {
            navCounter = 0;
            lNav.validate();
            lNav.paint(navgGr);
            navgGr.translate(2, 0);
            coloredNav = false;
            lNav.setBackground(Color.WHITE);
            lNav.setForeground(Color.WHITE);

        }
        navCounter++;
    }

    //        if (squareL < 6) {
    //            return;
    //        }
    //
    //        l.setBackground(Color.WHITE);
    //        f = getTableFont(squareL - 2);
    //        //f = new Font("Arial",1,squareL-2);
    //        l.setFont(f);
    //
    //
    //        for (int j = 0; j < W; j++) {
    //            X = UL.x + WdSUM[j];
    //            Y = UL.y;
    //
    //            BNDS.setBounds(X, Y, Wd[j], squareL + 1);
    //            if (gr.getClipBounds() != null && !gr.getClipBounds().intersects(BNDS)) {
    //                continue;
    //            }
    //
    //            gr.translate(X, Y);
    //            l.setBounds(0, 0, Wd[j], squareL + 1);
    ////            l.setBorder(javax.swing.BorderFactory.createLineBorder(GridCol));
    //            l.setText(headers[j]);
    //            l.validate();
    //            gr.translate(-X, -Y);
    //        }

}

From source file:org.apache.pdfbox.pdfviewer.PageDrawer.java

/**
 * This will draw the page to the requested context.
 *
 * @param g The graphics context to draw onto.
 * @param p The page to draw.//from  ww  w .  j a va 2  s.  c  o m
 * @param pageDimension The size of the page to draw.
 *
 * @throws IOException If there is an IO error while drawing the page.
 */
public void drawPage(Graphics g, PDPage p, Dimension pageDimension) throws IOException {
    graphics = (Graphics2D) g;
    page = p;
    pageSize = pageDimension;
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    // Only if there is some content, we have to process it. 
    // Otherwise we are done here and we will produce an empty page
    if (page.getContents() != null) {
        PDResources resources = page.findResources();
        processStream(page, resources, page.getContents().getStream());
    }
    List annotations = page.getAnnotations();
    for (int i = 0; i < annotations.size(); i++) {
        PDAnnotation annot = (PDAnnotation) annotations.get(i);
        PDRectangle rect = annot.getRectangle();
        String appearanceName = annot.getAppearanceStream();
        PDAppearanceDictionary appearDictionary = annot.getAppearance();
        if (appearDictionary != null) {
            if (appearanceName == null) {
                appearanceName = "default";
            }
            Map appearanceMap = appearDictionary.getNormalAppearance();
            if (appearanceMap != null) {
                PDAppearanceStream appearance = (PDAppearanceStream) appearanceMap.get(appearanceName);
                if (appearance != null) {
                    g.translate((int) rect.getLowerLeftX(), (int) -rect.getLowerLeftY());
                    processSubStream(page, appearance.getResources(), appearance.getStream());
                    g.translate((int) -rect.getLowerLeftX(), (int) +rect.getLowerLeftY());
                }
            }
        }
    }

}

From source file:org.apache.pdflens.views.pagesview.PageDrawer.java

/**
 * This will draw the page to the requested context.
 *
 * @param g The graphics context to draw onto.
 * @param p The page to draw.//  ww w  . j  av  a  2  s  .c  om
 * @param pageDimension The size of the page to draw.
 *
 * @throws IOException If there is an IO error while drawing the page.
 */
public void drawPage(Graphics g, PDPage p, Dimension pageDimension) throws IOException {
    graphics = (Graphics2D) g;
    page = p;
    pageSize = pageDimension;
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    // Only if there is some content, we have to process it. 
    // Otherwise we are done here and we will produce an empty page
    if (page.getContents() != null) {
        PDResources resources = page.findResources();
        processStream(page, resources, page.getContents().getStream());
    }
    List annotations = page.getAnnotations();
    for (int i = 0; i < annotations.size(); i++) {
        PDAnnotation annot = (PDAnnotation) annotations.get(i);
        PDRectangle rect = annot.getRectangle();
        String appearanceName = annot.getAppearanceStream();
        PDAppearanceDictionary appearDictionary = annot.getAppearance();
        if (appearDictionary != null) {
            if (appearanceName == null) {
                appearanceName = "default";
            }
            Map appearanceMap = appearDictionary.getNormalAppearance();
            PDAppearanceStream appearance = (PDAppearanceStream) appearanceMap.get(appearanceName);
            if (appearance != null) {
                g.translate((int) rect.getLowerLeftX(), (int) -rect.getLowerLeftY());
                processSubStream(page, appearance.getResources(), appearance.getStream());
                g.translate((int) -rect.getLowerLeftX(), (int) +rect.getLowerLeftY());
            }
        }
    }

}

From source file:org.esa.nest.dat.views.polarview.PolarCanvas.java

private void drawColorBar(Graphics g, Axis cAxis) {
    final Dimension cbSize = new Dimension((int) (graphSize.width * 0.03),
            (int) (Math.min(200, graphSize.height * 0.6)));
    final Point at = new Point(20, -100);

    g.translate(at.x, at.y);
    g.drawImage(colorBar, 0, 0, cbSize.width, cbSize.height, this);
    g.drawRect(0, 0, cbSize.width, cbSize.height);
    g.translate(cbSize.width, cbSize.height);
    cAxis.draw(g, cbSize);/*w w w . j av a2  s .co  m*/
    g.drawString(cAxis.getUnit(), 50, 5);
    g.translate(-cbSize.width - at.x, -cbSize.height - at.y);
}

From source file:org.esa.nest.dat.views.polarview.PolarCanvas.java

private static void paintComponents(Container c, Graphics g) {
    if (!c.isShowing())
        return;/*from w  w w . j av a  2  s.  c  om*/

    final int ncomponents = c.getComponentCount();
    final Rectangle clip = g.getClipBounds();

    int i = ncomponents - 1;
    while (i >= 0) {
        final Component component[] = c.getComponents();
        final Component comp = component[i];
        if (comp == null || !comp.isVisible())
            continue;
        final Rectangle bounds = comp.getBounds();
        Rectangle cr;
        if (clip == null)
            cr = new Rectangle(bounds);
        else
            cr = bounds.intersection(clip);
        if (cr.isEmpty())
            continue;

        final Graphics cg = g.create();
        cg.setClip(cr);
        cg.translate(bounds.x, bounds.y);
        try {
            comp.paint(cg);
        } catch (Throwable e) {
            //
        }

        cg.dispose();
        i--;
    }
}

From source file:org.esa.nest.dat.views.polarview.PolarCanvas.java

private void draw(Graphics g, Dimension size) {
    final int annotationHeight = 100;
    final int x = Math.max((int) (size.height * 0.05), 10);
    final int y = Math.max((int) (size.width * 0.05), 10);
    final int bottom = Math.max((int) (size.height * 0.1) + annotationHeight, 20);
    final int right = Math.max((int) (size.width * 0.1), 20);
    final Rectangle r = positionPlot(size, x, y, bottom, right);
    plotRadius = Math.min(r.width / 2, r.height / 2);
    final Dimension quadrantSize = new Dimension(plotRadius, plotRadius);
    g.translate(0, origin.y + r.height);
    if (data != null) {
        loadColorBar(data.getColorScale());
        drawColorBar(g, colourAxis);/*  ww  w . ja  va2  s .c  o  m*/
    }
    g.translate(0, -origin.y - r.height);

    origin.y += r.height / 2;
    origin.x += r.width / 2;
    final Graphics graphics = g.create();
    graphics.translate(origin.x, origin.y);
    radialAxis.setSize(quadrantSize);
    if (data != null)
        data.draw(graphics);
    if (rings != null) {
        int ri = 0;
        for (double ring : rings) {
            final int rad = radialAxis.getScreenPoint(ring);
            final int rad2 = rad + rad;
            graphics.setColor(Color.lightGray);
            graphics.drawOval(-rad, -rad, rad2, rad2);
            if (ringText != null && ringText[ri] != null) {
                graphics.setColor(Color.black);
                graphics.drawString(ringText[ri], 0, -rad);
            }
            ++ri;
        }
    } else {
        radialAxis.draw(graphics);
    }

    // draw wind direction & speed
    if (showWindDirection)
        drawWindDirection(graphics, plotRadius, windDirection - 90);

    graphics.translate(-origin.x, -origin.y);

    drawAxisLabels(graphics);

    graphics.dispose();
}