Example usage for java.awt Graphics drawRect

List of usage examples for java.awt Graphics drawRect

Introduction

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

Prototype

public void drawRect(int x, int y, int width, int height) 

Source Link

Document

Draws the outline of the specified rectangle.

Usage

From source file:ala.soils2sat.DrawingUtils.java

public static Rectangle drawString(Graphics g, Font font, String text, int x, int y, int width, int height,
        int align, boolean wrap) {
    g.setFont(font);// w w w. j a va  2  s  .  c om
    FontMetrics fm = g.getFontMetrics(font);

    setPreferredAliasingMode(g);

    Rectangle ret = new Rectangle(0, 0, 0, 0);

    if (text == null) {
        return ret;
    }
    String[] alines = text.split("\\n");
    ArrayList<String> lines = new ArrayList<String>();
    for (String s : alines) {
        if (wrap && fm.stringWidth(s) > width) {
            // need to split this up into multiple lines...
            List<String> splitLines = wrapString(s, fm, width);
            lines.addAll(splitLines);
        } else {
            lines.add(s);
        }
    }
    int numlines = lines.size();
    while (fm.getHeight() * numlines > height) {
        numlines--;
    }
    if (numlines > 0) {
        int maxwidth = 0;
        int minxoffset = y + width;
        int totalheight = (numlines * fm.getHeight());

        int linestart = ((height / 2) - (totalheight / 2));

        if (!wrap) {
            ret.y = y + linestart;
        } else {
            ret.y = y;
            linestart = 0;
        }
        for (int idx = 0; idx < numlines; ++idx) {
            String line = lines.get(idx);
            int stringWidth = fm.stringWidth(line);
            // the width of the label depends on the font :
            // if the width of the label is larger than the item
            if (stringWidth > 0 && width < stringWidth) {
                // We have to truncate the label
                line = clipString(null, fm, line, width);
                stringWidth = fm.stringWidth(line);
            }

            int xoffset = 0;
            int yoffset = linestart + fm.getHeight() - fm.getDescent();
            if (align == TEXT_ALIGN_RIGHT) {
                xoffset = (width - stringWidth);
            } else if (align == TEXT_ALIGN_CENTER) {
                xoffset = (int) Math.round((double) (width - stringWidth) / (double) 2);
            }

            if (xoffset < minxoffset) {
                minxoffset = xoffset;
            }
            g.drawString(line, x + xoffset, y + yoffset);
            if (stringWidth > maxwidth) {
                maxwidth = stringWidth;
            }
            linestart += fm.getHeight();
        }

        ret.width = maxwidth;
        ret.height = totalheight;
        ret.x = x + minxoffset;

        // Debug only...
        if (DEBUG) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.setStroke(new BasicStroke(1));
            g.setColor(Color.blue);
            g.drawRect(ret.x, ret.y, ret.width, ret.height);
            g.setColor(Color.green);
            g.drawRect(x, y, width, height);
        }

        return ret;
    }
    return ret;
}

From source file:es.emergya.ui.gis.layers.MapViewerLayer.java

@Override
public void paint(Graphics g, MapView mv) {
    parent = (CustomMapView) mv;//from   ww  w . ja v a  2s  .  co m

    int iMove = 0;

    zoom = mv.zoom();
    LatLon cen = Main.proj.eastNorth2latlon(mv.getCenter());

    center = new Point(OsmMercator.LonToX(cen.getX(), zoom), OsmMercator.LatToY(cen.getY(), zoom));

    int tilex = center.x / Tile.SIZE;
    int tiley = center.y / Tile.SIZE;
    int off_x = (center.x % Tile.SIZE);
    int off_y = (center.y % Tile.SIZE);

    int w2 = mv.getWidth() / 2;
    int h2 = mv.getHeight() / 2;
    int posx = w2 - off_x;
    int posy = h2 - off_y;

    int diff_left = off_x;
    int diff_right = Tile.SIZE - off_x;
    int diff_top = off_y;
    int diff_bottom = Tile.SIZE - off_y;

    boolean start_left = diff_left < diff_right;
    boolean start_top = diff_top < diff_bottom;

    if (start_top) {
        if (start_left) {
            iMove = 2;
        } else {
            iMove = 3;
        }
    } else {
        if (start_left) {
            iMove = 1;
        } else {
            iMove = 0;
        }
    } // calculate the visibility borders
    int x_min = -Tile.SIZE;
    int y_min = -Tile.SIZE;
    int x_max = mv.getWidth();
    int y_max = mv.getHeight();

    // paint the tiles in a spiral, starting from center of the map
    boolean painted = true;
    int x = 0;
    while (painted) {
        painted = false;
        for (int i = 0; i < 4; i++) {
            if (i % 2 == 0) {
                x++;
            }
            for (int j = 0; j < x; j++) {
                if (x_min <= posx && posx <= x_max && y_min <= posy && posy <= y_max) {
                    // tile is visible
                    Tile tile = getTile(tilex, tiley, zoom);
                    if (tile != null) {
                        painted = true;
                        tile.paint(g, posx, posy);
                        if (tileGridVisible) {
                            g.drawString(tile.getXtile() + ", " + tile.getYtile(), posx, posy + 12);
                            g.drawRect(posx, posy, Tile.SIZE, Tile.SIZE);
                        }
                    }
                }
                Point p = move[iMove];
                posx += p.x * Tile.SIZE;
                posy += p.y * Tile.SIZE;
                tilex += p.x;
                tiley += p.y;
            }
            iMove = (iMove + 1) % move.length;
        }
    }
    // outer border of the map
    int mapSize = Tile.SIZE << zoom;
    g.drawRect(w2 - center.x, h2 - center.y, mapSize, mapSize);

    if (LOG.isDebugEnabled()) {
        g.setColor(Color.LIGHT_GRAY);
        g.drawString("Tiles in cache: " + tileCache.getTileCount(), 50, 15);
    }

    // g.fillRect(0, 100, 300, 55);
    // g.setColor(Color.BLACK);
    // g.drawString("center: " + center.x + ", " + center.y, 5, 120);
    // g.drawString("zoom: " + zoom + " for scale:" + mv.getScale(), 5,
    // 150);
}

From source file:com.headswilllol.basiclauncher.Launcher.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setFont(font);/*from   w ww . j  a v a  2s .  c o m*/
    if (updateMsg != null) {
        g.drawString("Update Available!", centerText(g, "Update Available!"), 50);
        g.setFont(smallFont);
        g.drawString(updateMsg, centerText(g, updateMsg), 100);
    }

    else if (progress == null)
        g.drawString(NAME + " Launcher", centerText(g, NAME + " Launcher"), 50);

    else {
        g.drawString(progress, centerText(g, progress), height / 2);
        if (fail != null)
            g.drawString(fail, centerText(g, fail), height / 2 + 50);
        else {
            if (aSize != -1 && eSize != -1) {
                String s = (aSize * 8) + "/" + (int) (eSize * 8) + " B";
                if (eSize * 8 >= 1024)
                    if (eSize * 8 >= 1024 * 1024)
                        s = String.format("%.2f", aSize * 8 / 1024 / 1024) + "/"
                                + String.format("%.2f", eSize * 8 / 1024 / 1024) + " MiB";
                    else
                        s = String.format("%.2f", aSize * 8 / 1024) + "/"
                                + String.format("%.2f", eSize * 8 / 1024) + " KiB";
                g.drawString(s, centerText(g, s), height / 2 + 40);
                String sp = "@" + (int) speed + " B/s";
                if (speed >= 1024)
                    if (speed >= 1024 * 1024)
                        sp = "@" + String.format("%.2f", (speed / 1024 / 1024)) + " MiB/s";
                    else
                        sp = "@" + String.format("%.2f", (speed / 1024)) + " KiB/s";
                g.drawString(sp, centerText(g, sp), height / 2 + 80);
                int barWidth = 500;
                int barHeight = 35;
                g.setColor(Color.LIGHT_GRAY);
                g.drawRect(width / 2 - barWidth / 2, height / 2 + 100, barWidth, barHeight);
                g.setColor(Color.GREEN);
                g.fillRect(width / 2 - barWidth / 2 + 1, height / 2 + 100 + 1,
                        (int) ((aSize / eSize) * (double) barWidth - 2), barHeight - 1);
                g.setColor(new Color(.2f, .2f, .2f));
                int percent = (int) (aSize / (double) eSize * 100);
                g.drawString(percent + "%", centerText(g, percent + "%"), height / 2 + 128);
            }
        }
    }
}

From source file:tufts.vue.RichTextBox.java

public void paintComponent(Graphics g) {
    if (TestDebug || DEBUG.TEXT)
        out("paintComponent @ " + getX() + "," + getY() + " parent=" + getParent());

    final MapViewer viewer = (MapViewer) javax.swing.SwingUtilities.getAncestorOfClass(MapViewer.class, this);
    Graphics2D g2d = (Graphics2D) g;
    if (viewer != null) {
        g2d.setRenderingHint(java.awt.RenderingHints.KEY_ANTIALIASING, viewer.AA_ON);
        g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);

    }//from ww w. j ava 2s  . co m
    // turn on anti-aliasing -- the cursor repaint loop doesn't
    // set anti-aliasing, so text goes jiggy around cursor/in selection if we don't do this
    ////  g.clipRect(0, 0,getWidth(), getAdjustedHeight());

    super.paintComponent(g2d);

    //super.paintComponent(g);
    if (true) {
        // draw a border (we don't want to add one because that changes the preferred size at a bad time)
        //  g.setColor(Color.gray);
        g.setClip(null);
        final int xpad = 1;
        final int ypad = 1;
        g.drawRect(-xpad, -ypad, (int) ((getWidth()) + xpad * 2 - 1), (int) ((getHeight()) + ypad * 2 - 1));
    }
}

From source file:org.processmining.analysis.performance.dottedchart.ui.DottedChartPanel.java

/**
 * convenience method for internal use. paints a log item handle
 * visualization.//from  w ww.j a  va2  s .com
 * 
 * @param x
 *            horizontal anchor coordinate of the handle
 * @param y
 *            vertical anchor coordinate of the handle
 * @param g
 *            the Graphics object used for painting
 */
protected void paintItem_buffer(int x, int y, Graphics g, String shape) {
    if (shape.equals(STR_NONE)) {
        return;
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_DOT)) {
        g.fillOval(x - 2, y - 2, 7, 7);
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_BOX)) {
        g.fill3DRect(x - 3, y - 3, 6, 6, false);
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_CIRCLE)) {
        g.fillOval(x - 2, y - 2, 7, 7);
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_RHOMBUS)) {
        int rhombX[] = { x, x - 3, x, x + 3 };
        int rhombY[] = { y - 3, y, y + 3, y };
        g.fillPolygon(rhombX, rhombY, 4);
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_TRIANGLE)) {
        int triX[] = { x, x - 3, x + 3 };
        int triY[] = { y + 3, y - 3, y - 3 };
        g.fillPolygon(triX, triY, 3);
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_ROUND_BOX)) {
        g.fillRoundRect(x - 3, y - 3, 6, 6, 2, 2);
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_DRAW_BOX)) {
        g.drawRect(x - 3, y - 3, 6, 6);
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_DRAW_CIRCLE)) {
        g.drawOval(x - 2, y - 2, 7, 7);
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_DRAW_RHOMBUS)) {
        int rhombX[] = { x, x - 3, x, x + 3 };
        int rhombY[] = { y - 3, y, y + 3, y };
        g.drawPolygon(rhombX, rhombY, 4);
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_DRAW_TRIANGLE)) {
        int triX[] = { x, x - 3, x + 3 };
        int triY[] = { y + 3, y - 3, y - 3 };
        g.drawPolygon(triX, triY, 3);
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_DRAW_ROUND_BOX)) {
        g.drawRoundRect(x - 3, y - 3, 6, 6, 2, 2);
    }
}

From source file:org.processmining.analysis.performance.dottedchart.ui.DottedChartPanel.java

/**
 * convenience method for internal use. paints a log item handle
 * visualization.//from   w  ww.j  av a2  s  .  com
 * 
 * @param x
 *            horizontal anchor coordinate of the handle
 * @param y
 *            vertical anchor coordinate of the handle
 * @param g
 *            the Graphics object used for painting
 */
protected void paintItem(int x, int y, Graphics g, String shape) {
    if (shape.equals(STR_NONE)) {
        return;
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_DOT)) {
        g.fillOval(x - 2, y - 2, 4, 4);
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_BOX)) {
        g.fill3DRect(x - 5, y - 5, 10, 10, false);
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_CIRCLE)) {
        g.fillOval(x - 5, y - 5, 11, 11);
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_RHOMBUS)) {
        int rhombX[] = { x, x - 5, x, x + 5 };
        int rhombY[] = { y - 5, y, y + 5, y };
        g.fillPolygon(rhombX, rhombY, 4);
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_TRIANGLE)) {
        int triX[] = { x, x - 5, x + 5 };
        int triY[] = { y + 5, y - 5, y - 5 };
        g.fillPolygon(triX, triY, 3);
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_ROUND_BOX)) {
        g.fillRoundRect(x - 5, y - 5, 10, 10, 2, 2);
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_DRAW_BOX)) {
        g.drawRect(x - 5, y - 5, 10, 10);
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_DRAW_CIRCLE)) {
        g.drawOval(x - 5, y - 5, 11, 11);
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_DRAW_RHOMBUS)) {
        int rhombX[] = { x, x - 5, x, x + 5 };
        int rhombY[] = { y - 5, y, y + 5, y };
        g.drawPolygon(rhombX, rhombY, 4);
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_DRAW_TRIANGLE)) {
        int triX[] = { x, x - 5, x + 5 };
        int triY[] = { y + 5, y - 5, y - 5 };
        g.drawPolygon(triX, triY, 3);
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_DRAW_ROUND_BOX)) {
        g.drawRoundRect(x - 5, y - 5, 10, 10, 2, 2);
    }
}

From source file:net.bioclipse.model.ScatterPlotMouseHandler.java

@Override
public void mouseDragged(MouseEvent e) {
    super.mouseDragged(e);

    ChartPanel chartPanel = getChartPanel(e);
    JFreeChart selectedChart = chartPanel.getChart();
    ChartDescriptor cd = ChartUtils.getChartDescriptor(selectedChart);
    int[] indices = cd.getSourceIndices();

    XYPlot plot = (XYPlot) chartPanel.getChart().getPlot();

    //Create double buffer
    Image buffer = chartPanel.createImage(chartPanel.getWidth(), chartPanel.getHeight());
    Graphics bufferGraphics = buffer.getGraphics();
    chartPanel.paint(bufferGraphics);//from  w w w  . j a v a  2s  .  co m

    if (lastX == 0 && lastY == 0) {
        lastX = e.getX();
        lastY = e.getY();
    }

    drawRect = new Rectangle();
    int x1 = Math.min(Math.min(e.getX(), lastX), startX);
    int y1 = Math.min(Math.min(e.getY(), lastY), startY);
    int x2 = Math.max(Math.max(e.getX(), lastX), startX);
    int y2 = Math.max(Math.max(e.getY(), lastY), startY);

    drawRect.x = x1;
    drawRect.y = y1;
    drawRect.width = x2 - drawRect.x;
    drawRect.height = y2 - drawRect.y;

    //Create a clipping rectangle
    Rectangle clipRect = new Rectangle(drawRect.x - 100, drawRect.y - 100, drawRect.width + 200,
            drawRect.height + 200);

    //Check for selected points
    for (int j = 0; j < plot.getDataset().getItemCount(plot.getDataset().getSeriesCount() - 1); j++) {
        for (int i = 0; i < plot.getDataset().getSeriesCount(); i++) {
            Number xK = plot.getDataset().getX(i, j);
            Number yK = plot.getDataset().getY(i, j);
            Point2D datasetPoint2D = new Point2D.Double(domainValueTo2D(chartPanel, plot, xK.doubleValue()),
                    rangeValueTo2D(chartPanel, plot, yK.doubleValue()));

            if (drawRect.contains(datasetPoint2D)) {
                PlotPointData cp = new PlotPointData(indices[j], cd.getXLabel(), cd.getYLabel());
                boolean pointAdded = mouseDragSelection.addPoint(cp);
                if (pointAdded) {
                    ((ScatterPlotRenderer) plot.getRenderer()).addMarkedPoint(j, i);
                    selectedChart.plotChanged(new PlotChangeEvent(plot));
                }
            } else if (!mouseDragSelection.isEmpty()) {
                PlotPointData cp = new PlotPointData(indices[j], cd.getXLabel(), cd.getYLabel());
                boolean pointRemoved = mouseDragSelection.removePoint(cp);
                if (pointRemoved) {
                    ((ScatterPlotRenderer) plot.getRenderer()).removeMarkedPoint(new Point(j, i));
                    selectedChart.plotChanged(new PlotChangeEvent(plot));
                }
            }
        }
    }

    Iterator<PlotPointData> iterator = currentSelection.iterator();
    while (iterator.hasNext()) {
        PlotPointData next = iterator.next();
        Point dataPoint = next.getDataPoint();
        ((ScatterPlotRenderer) plot.getRenderer()).addMarkedPoint(dataPoint);
    }

    lastX = e.getX();
    lastY = e.getY();

    Graphics graphics = chartPanel.getGraphics();
    graphics.setClip(clipRect);

    //Draw selection rectangle
    bufferGraphics.drawRect(drawRect.x, drawRect.y, drawRect.width, drawRect.height);

    graphics.drawImage(buffer, 0, 0, chartPanel.getWidth(), chartPanel.getHeight(), null);
}

From source file:ded.ui.DiagramController.java

/** The core of the paint routine, after we decide whether to interpose
  * another buffer. *//*w ww . j a v a2s.co m*/
private void innerPaint(Graphics g) {
    super.paint(g);

    // I do not know the proper way to get a font set automatically
    // in a Graphics object.  Calling JComponent.setFont has gotten
    // me nowhere.  Setting it myself when I first get control
    // seems to work; but note that I have to do this *after*
    // calling super.paint().
    g.setFont(this.dedWindow.diagramFont);

    // Filename label.
    if (this.diagram.drawFileName && !this.fileName.isEmpty()) {
        String name = new File(this.fileName).getName();
        FontMetrics fm = g.getFontMetrics();
        LineMetrics lm = fm.getLineMetrics(name, g);
        int x = fileNameLabelMargin;
        int y = fileNameLabelMargin + (int) lm.getAscent();
        g.drawString(name, x, y);
        y += (int) lm.getUnderlineOffset() + 1 /*...*/;
        g.drawLine(x, y, x + fm.stringWidth(name), y);
    }

    // Controllers.
    for (Controller c : this.controllers) {
        if (c.isSelected()) {
            c.paintSelectionBackground(g);
        }
        c.paint(g);
    }

    // Lasso rectangle.
    if (this.mode == Mode.DCM_RECT_LASSO) {
        Rectangle r = this.getLassoRect();
        g.drawRect(r.x, r.y, r.width, r.height);
    }

    // Current focused Component.
    if (debugFocus) {
        KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        Component fo = kfm.getFocusOwner();
        g.drawString("Focus: " + fo, 3, this.getHeight() - 22);
    }

    // Mode label.
    if (this.mode != Mode.DCM_SELECT) {
        g.drawString("Mode: " + this.mode.description, 3, this.getHeight() - 4);
    } else if (this.fpsMeasurementMode) {
        this.fpsFrameCount++;
        long current = System.currentTimeMillis();
        long millis = current - this.fpsStartMillis;
        if (millis > 1000) {
            // Update the FPS measurement with the results for this
            // interval.
            this.fpsSampleCount++;
            this.fpsMeasurement = "FPS: " + this.fpsFrameCount + " (millis=" + millis + ", samples="
                    + this.fpsSampleCount + ")";

            // Reset the counters.
            this.fpsStartMillis = current;
            this.fpsFrameCount = 0;
        }
        g.drawString(this.fpsMeasurement + " (Ctrl+G to stop)", 3, this.getHeight() - 4);
    }
}

From source file:stainingestimation.StainingEstimation.java

/**
 * Performs nucleus finding on the given color chanel image imp (heatmap) with the given parameters. Nuclei are drawn on the image.
 * @param imp The gray scaled color channel image after color deconvolution. It is altered by the method.
 * @param blur The blur parameter before nucleus detection.
 * @param tolerance The tolerance parameter of the local maximum method.
 * @param t The minimum value of a local maximum.
 *///  ww  w  .  j  a v a  2 s. c o  m
void calculateNucleiPreview(ImagePlus imp, double blur, int tolerance, int t) {
    List<TMApoint> tps = StainingEstimation.find_nucleus_lm(null, imp.duplicate(), 0, 0, blur, tolerance, t,
            TMALabel.STAINING_0, true);
    new ImageConverter(imp).convertToRGB();
    Graphics g = imp.getImage().getGraphics();
    g.setColor(Color.RED);
    for (TMApoint tp : tps) {
        g.drawRect(tp.x - 1, tp.y - 1, 3, 3);
    }
}