Example usage for java.awt Graphics fillOval

List of usage examples for java.awt Graphics fillOval

Introduction

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

Prototype

public abstract void fillOval(int x, int y, int width, int height);

Source Link

Document

Fills an oval bounded by the specified rectangle with the current color.

Usage

From source file:es.emergya.ui.gis.CustomMapView.java

/**
 * Dibuja una brujula apuntando al norte
 * //  ww w  . j  av  a 2s  .  c  o  m
 * @param g
 */
@SuppressWarnings("unused")
private void drawCompass(Graphics g) {
    int x = 50, y = getHeight() - 50;
    // ((Graphics2D)g).rotate(Math.PI + lastFollowAngle, x, y);
    ((Graphics2D) g).rotate(-PI_MEDIO - getAngle(), x, y);
    g.setColor(Color.LIGHT_GRAY);
    g.fillOval(0, getHeight() - 100, 100, 100);
    g.setColor(Color.BLACK);
    g.fillRect(x - 3, y - 3, 50, 6);
    g.setColor(Color.RED);
    g.fillRect(x + 42, y - 3, 6, 6);
}

From source file:ScaleTest_2008.java

/**
 * Paints the test image that will be downscaled and timed by the various
 * scaling methods. A different image is rendered into each of the four
 * quadrants of this image: RGB stripes, a picture, vector art, and 
 * a black and white grid./*from w  w  w  . j  a  v  a 2  s  . c  o m*/
 */
private void paintOriginalImage() {
    Graphics g = originalImage.getGraphics();
    // Erase to black
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, FULL_SIZE, FULL_SIZE);

    // RGB quadrant
    for (int i = 0; i < QUAD_SIZE; i += 3) {
        int x = i;
        g.setColor(Color.RED);
        g.drawLine(x, 0, x, QUAD_SIZE);
        x++;
        g.setColor(Color.GREEN);
        g.drawLine(x, 0, x, QUAD_SIZE);
        x++;
        g.setColor(Color.BLUE);
        g.drawLine(x, 0, x, QUAD_SIZE);
    }

    // Picture quadrant
    try {
        URL url = getClass().getResource("BBGrayscale.png");
        BufferedImage picture = ImageIO.read(url);
        // Center picture in quadrant area
        int xDiff = QUAD_SIZE - picture.getWidth();
        int yDiff = QUAD_SIZE - picture.getHeight();
        g.drawImage(picture, QUAD_SIZE + xDiff / 2, yDiff / 2, null);
    } catch (Exception e) {
        System.out.println("Problem reading image file: " + e);
    }

    // Vector drawing quadrant
    g.setColor(Color.WHITE);
    g.fillRect(0, QUAD_SIZE, QUAD_SIZE, QUAD_SIZE);
    g.setColor(Color.BLACK);
    g.drawOval(2, QUAD_SIZE + 2, QUAD_SIZE - 4, QUAD_SIZE - 4);
    g.drawArc(20, QUAD_SIZE + 20, (QUAD_SIZE - 40), QUAD_SIZE - 40, 190, 160);
    int eyeSize = 7;
    int eyePos = 30 - (eyeSize / 2);
    g.fillOval(eyePos, QUAD_SIZE + eyePos, eyeSize, eyeSize);
    g.fillOval(QUAD_SIZE - eyePos - eyeSize, QUAD_SIZE + eyePos, eyeSize, eyeSize);

    // B&W grid
    g.setColor(Color.WHITE);
    g.fillRect(QUAD_SIZE + 1, QUAD_SIZE + 1, QUAD_SIZE, QUAD_SIZE);
    g.setColor(Color.BLACK);
    for (int i = 0; i < QUAD_SIZE; i += 4) {
        int pos = QUAD_SIZE + i;
        g.drawLine(pos, QUAD_SIZE + 1, pos, FULL_SIZE);
        g.drawLine(QUAD_SIZE + 1, pos, FULL_SIZE, pos);
    }

    originalImagePainted = true;
}

From source file:rod_design_compute.ShowPanel.java

private void drawBasePoint(Point point, Graphics g) {
    int x = toScreenX(point.X);
    int y = toScreenY(point.Y);

    int lengthTri = 4 * radiusBase;
    int x1 = (int) (x - lengthTri * Math.sin(Math.toRadians(30)));
    int y1 = (int) (y + lengthTri * Math.cos(Math.toRadians(30)));
    int x2 = (int) (x + lengthTri * Math.sin(Math.toRadians(30)));
    int y2 = (int) (y + lengthTri * Math.cos(Math.toRadians(30)));

    g.drawLine(x, y, x1, y1);//from  www .  ja  va  2  s.  co m
    g.drawLine(x, y, x2, y2);
    g.drawLine(x1 - radiusBase, y1, x2 + radiusBase, y2);

    g.drawLine(x1, y1, x1 - radiusBase, y1 + radiusBase);
    g.drawLine(x1 + radiusBase, y1, x1 + radiusBase - radiusBase, y1 + radiusBase);
    g.drawLine(x1 + 2 * radiusBase, y1, x1 + 2 * radiusBase - radiusBase, y1 + radiusBase);
    g.drawLine(x1 + 3 * radiusBase, y1, x1 + 3 * radiusBase - radiusBase, y1 + radiusBase);
    g.drawLine(x1 + 4 * radiusBase, y1, x1 + 4 * radiusBase - radiusBase, y1 + radiusBase);

    g.setColor(Color.white);
    g.fillOval(x - radiusBase, y - radiusBase, radiusBase * 2, radiusBase * 2);
    g.setColor(Color.black);
    g.drawOval(x - radiusBase, y - radiusBase, radiusBase * 2, radiusBase * 2);
}

From source file:edu.upf.bioevo.manhattanPlotter.QQPlot.java

void drawPlot() {
    int dotRadius;
    int xIncrement = (IMAGE_WIDTH - (xMargin * 2)) / (int) (maxExpectedLogValue + 1);
    int yIncrement = (IMAGE_HEIGHT - (yMargin * 2)) / (int) (maxObservedLogValue + 1);
    Graphics g = image.getGraphics();

    // Cleans image
    g.setColor(Color.WHITE);//from   ww  w .  j  a v  a  2  s. c  o  m
    g.fillRect(0, 0, image.getWidth(), image.getHeight());

    // Draws axis lines
    g.setColor(Color.black);
    g.drawLine(xMargin, IMAGE_HEIGHT - yMargin, IMAGE_WIDTH - xMargin, IMAGE_HEIGHT - yMargin);
    g.drawLine(xMargin, IMAGE_HEIGHT - yMargin, xMargin, yMargin);

    // Draws confidence interval
    if (ic95Option) {
        g.setColor(Color.lightGray);
        for (int i = 0; i < upperCI.length - 1; i++) {
            int[] xPoints = { xMargin + (int) (orderedLog10ExpectedValues[i] * xIncrement),
                    xMargin + (int) (orderedLog10ExpectedValues[i] * xIncrement),
                    xMargin + (int) (orderedLog10ExpectedValues[i + 1] * xIncrement),
                    xMargin + (int) (orderedLog10ExpectedValues[i + 1] * xIncrement) };
            int[] yPoints = { IMAGE_HEIGHT - (yMargin + ((int) (upperCI[i] * yIncrement))),
                    IMAGE_HEIGHT - (yMargin + ((int) (lowerCI[i] * yIncrement))),
                    IMAGE_HEIGHT - (yMargin + ((int) (lowerCI[i + 1] * yIncrement))),
                    IMAGE_HEIGHT - (yMargin + ((int) (upperCI[i + 1] * yIncrement))) };

            g.fillPolygon(xPoints, yPoints, 4);
        }
    }

    // Draws dots
    dotRadius = 4;
    g.setColor(Color.black);
    for (int i = 0; i < orderedLog10ObservedValues.length; i++) {
        int xPosition = xMargin + ((int) (orderedLog10ExpectedValues[i] * xIncrement));
        int yPosition = IMAGE_HEIGHT - (yMargin + ((int) (orderedLog10ObservedValues[i] * yIncrement)));
        g.fillOval(xPosition - dotRadius, yPosition - dotRadius, dotRadius * 2, dotRadius * 2);
    }

    // --draw y axis scale
    // ----Calculates interval between labels in y axis
    int yInterval = 1;
    int verticalMaxValue = (int) maxObservedLogValue + 1;

    if (verticalMaxValue > 20) {
        yInterval = 5;
    }
    if (verticalMaxValue > 100) {
        yInterval = 50;
    }
    if (verticalMaxValue > 1000) {
        yInterval = verticalMaxValue;
    }

    g.setColor(Color.BLACK);
    for (int i = 0; i <= verticalMaxValue; i = i + yInterval) {
        int yPos = IMAGE_HEIGHT
                - (yMargin + (int) (((float) i / verticalMaxValue) * (IMAGE_HEIGHT - 2 * yMargin)));
        g.fillRect(45, yPos, 15, 3);
        g.setFont(new Font("courier", 1, 30));
        String value = String.valueOf(i);
        if (value.length() == 1) {
            value = " " + value;
        }
        g.drawString(value, 5, yPos + 10);
    }

    // --draw x axis scale
    // ----Calculates interval between labels in x axis
    int xInterval = 1;
    int horizontalMaxValue = (int) maxExpectedLogValue + 1;

    if (horizontalMaxValue > 20) {
        xInterval = 5;
    }
    if (horizontalMaxValue > 100) {
        xInterval = 50;
    }
    if (horizontalMaxValue > 1000) {
        xInterval = verticalMaxValue;
    }

    g.setColor(Color.BLACK);
    for (int i = 0; i <= horizontalMaxValue; i = i + xInterval) {
        int xPos = (xMargin + (int) (((float) i / horizontalMaxValue) * (IMAGE_WIDTH - 2 * xMargin)));
        g.fillRect(xPos, IMAGE_HEIGHT - yMargin, 3, 15);
        g.setFont(new Font("courier", 1, 30));
        String value = String.valueOf(i);
        if (value.length() == 1) {
            value = " " + value;
        }
        g.drawString(value, xPos - 15, IMAGE_HEIGHT - yMargin + 45);
    }

    // Draws identity line
    g.setColor(Color.RED);
    int endLineValue = horizontalMaxValue < verticalMaxValue ? horizontalMaxValue : verticalMaxValue;
    g.drawLine(xMargin, IMAGE_HEIGHT - yMargin, xMargin + ((int) (endLineValue * xIncrement)),
            IMAGE_HEIGHT - (yMargin + ((int) (endLineValue * yIncrement))));

    // -- title
    g.setColor(Color.blue);
    g.setFont(new Font("arial", 1, 35));
    g.drawString(label, IMAGE_WIDTH / 7, 40);
}

From source file:com.cburch.logisim.circuit.CircuitWires.java

void draw(ComponentDrawContext context, Collection<Component> hidden) {
    boolean showState = context.getShowState();
    CircuitState state = context.getCircuitState();
    Graphics g = context.getGraphics();
    g.setColor(Color.BLACK);// w ww.  j ava 2  s  .c om
    GraphicsUtil.switchToWidth(g, Wire.WIDTH);
    WireSet highlighted = context.getHighlightedWires();

    BundleMap bmap = getBundleMap();
    boolean isValid = bmap.isValid();
    if (hidden == null || hidden.size() == 0) {
        for (Wire w : wires) {
            Location s = w.e0;
            Location t = w.e1;
            WireBundle wb = bmap.getBundleAt(s);
            if (!wb.isValid()) {
                g.setColor(Value.WIDTH_ERROR_COLOR);
            } else if (showState) {
                if (!isValid)
                    g.setColor(Value.NIL_COLOR);
                else
                    g.setColor(state.getValue(s).getColor());
            } else {
                g.setColor(Color.BLACK);
            }
            if (highlighted.containsWire(w)) {
                GraphicsUtil.switchToWidth(g, Wire.WIDTH + 2);
                g.drawLine(s.getX(), s.getY(), t.getX(), t.getY());
                GraphicsUtil.switchToWidth(g, Wire.WIDTH);
            } else {
                g.drawLine(s.getX(), s.getY(), t.getX(), t.getY());
            }
        }

        for (Location loc : points.getSplitLocations()) {
            if (points.getComponentCount(loc) > 2) {
                WireBundle wb = bmap.getBundleAt(loc);
                if (wb != null) {
                    if (!wb.isValid()) {
                        g.setColor(Value.WIDTH_ERROR_COLOR);
                    } else if (showState) {
                        if (!isValid)
                            g.setColor(Value.NIL_COLOR);
                        else
                            g.setColor(state.getValue(loc).getColor());
                    } else {
                        g.setColor(Color.BLACK);
                    }
                    if (highlighted.containsLocation(loc)) {
                        g.fillOval(loc.getX() - 5, loc.getY() - 5, 10, 10);
                    } else {
                        g.fillOval(loc.getX() - 4, loc.getY() - 4, 8, 8);
                    }
                }
            }
        }
    } else {
        for (Wire w : wires) {
            if (!hidden.contains(w)) {
                Location s = w.e0;
                Location t = w.e1;
                WireBundle wb = bmap.getBundleAt(s);
                if (!wb.isValid()) {
                    g.setColor(Value.WIDTH_ERROR_COLOR);
                } else if (showState) {
                    if (!isValid)
                        g.setColor(Value.NIL_COLOR);
                    else
                        g.setColor(state.getValue(s).getColor());
                } else {
                    g.setColor(Color.BLACK);
                }
                if (highlighted.containsWire(w)) {
                    GraphicsUtil.switchToWidth(g, Wire.WIDTH + 2);
                    g.drawLine(s.getX(), s.getY(), t.getX(), t.getY());
                    GraphicsUtil.switchToWidth(g, Wire.WIDTH);
                } else {
                    g.drawLine(s.getX(), s.getY(), t.getX(), t.getY());
                }
            }
        }

        // this is just an approximation, but it's good enough since
        // the problem is minor, and hidden only exists for a short
        // while at a time anway.
        for (Location loc : points.getSplitLocations()) {
            if (points.getComponentCount(loc) > 2) {
                int icount = 0;
                for (Component comp : points.getComponents(loc)) {
                    if (!hidden.contains(comp))
                        ++icount;
                }
                if (icount > 2) {
                    WireBundle wb = bmap.getBundleAt(loc);
                    if (wb != null) {
                        if (!wb.isValid()) {
                            g.setColor(Value.WIDTH_ERROR_COLOR);
                        } else if (showState) {
                            if (!isValid)
                                g.setColor(Value.NIL_COLOR);
                            else
                                g.setColor(state.getValue(loc).getColor());
                        } else {
                            g.setColor(Color.BLACK);
                        }
                        if (highlighted.containsLocation(loc)) {
                            g.fillOval(loc.getX() - 5, loc.getY() - 5, 10, 10);
                        } else {
                            g.fillOval(loc.getX() - 4, loc.getY() - 4, 8, 8);
                        }
                    }
                }
            }
        }
    }
}

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

/**
 * convenience method for internal use. paints a log item handle
 * visualization.//from  w  w  w  . j a v a2  s .c  om
 * 
 * @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:org.processmining.analysis.performance.dottedchart.ui.DottedChartPanel.java

/**
 * convenience method for internal use. paints a log item handle
 * visualization./*  w  w w  .  java  2s .c  o m*/
 * 
 * @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 paintHighligtedItem(int x, int y, Graphics g, String shape) {
    Color color = g.getColor();
    if (shape.equals(STR_NONE)) {
        return;
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_DOT)) {
        if (!color.equals(Color.red))
            g.setColor(Color.red);
        else
            g.setColor(Color.black);
        g.fillOval(x - 3, y - 3, 6, 6);
        g.setColor(color);
        g.fillOval(x - 2, y - 2, 4, 4);
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_BOX)) {
        if (!color.equals(Color.black))
            g.setColor(Color.black);
        else
            g.setColor(Color.red);
        g.fillRect(x - 6, y - 6, 12, 12);
        g.setColor(color);
        g.fill3DRect(x - 5, y - 5, 10, 10, false);
    } else if (shape.equals(DottedChartPanel.ITEM_HANDLE_CIRCLE)) {
        if (!color.equals(Color.black))
            g.setColor(Color.black);
        else
            g.setColor(Color.red);
        g.fillOval(x - 6, y - 6, 13, 13);
        g.setColor(color);
        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)) {
        if (!color.equals(Color.black))
            g.setColor(Color.black);
        else
            g.setColor(Color.red);
        g.fillRoundRect(x - 6, y - 6, 13, 13, 2, 2);
        g.setColor(color);
        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)) {
        if (!color.equals(Color.black))
            g.setColor(Color.black);
        else
            g.setColor(Color.red);
        g.fillOval(x - 6, y - 6, 13, 13);
        g.setColor(color);
        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:org.processmining.analysis.performance.dottedchart.ui.DottedChartPanel.java

/**
 * convenience method for internal use. paints a log item handle
 * visualization.//from w  w  w. j  av a2 s.  co  m
 * 
 * @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:uk.ac.babraham.BamQC.Graphs.ScatterGraph.java

@Override
protected void paintComponent(Graphics g) {

    g.setColor(Color.WHITE);/*from ww w. j  a v a2 s .c  om*/
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(Color.BLACK);

    if (g instanceof Graphics2D) {
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    }

    double yStart, xStart;
    if (minY % yInterval == 0) {
        yStart = minY;
    } else {
        yStart = yInterval * (((int) minY / yInterval) + 1);
    }

    if (minX % xInterval == 0) {
        xStart = minX;
    } else {
        xStart = xInterval * (((int) minX / xInterval) + 1);
    }

    int xOffset = 0;

    // Draw the yLabel on the left of the yAxis
    int yLabelRightShift = 12;
    if (yLabel == null || yLabel.isEmpty()) {
        yLabelRightShift = 0;
    } else {
        if (g instanceof Graphics2D) {
            Graphics2D g2 = (Graphics2D) g;
            AffineTransform orig = g2.getTransform();
            g2.rotate(-Math.PI / 2);
            g2.setColor(Color.BLACK);
            g2.drawString(yLabel, -getY(-yInterval) / 2 - (g.getFontMetrics().stringWidth(yLabel) / 2),
                    yLabelRightShift);
            g2.setTransform(orig);
        }
    }

    // Draw the y axis labels
    int lastYLabelEnd = Integer.MAX_VALUE;
    for (double i = yStart; i <= maxY; i += yInterval) {
        String label = "" + i;
        label = label.replaceAll(".0$", ""); // Don't leave trailing .0s where we don't need them.
        // Calculate the new xOffset depending on the widest ylabel.
        int width = g.getFontMetrics().stringWidth(label);
        if (width > xOffset) {
            xOffset = width;
        }
        // place the y axis labels so that they don't overlap when the plot is resized.
        int baseNumberHeight = g.getFontMetrics().getHeight();
        int baseNumberPosition = getY(i) + (baseNumberHeight / 2);
        if (baseNumberPosition + baseNumberHeight < lastYLabelEnd) {
            // Draw the y axis labels
            g.drawString(label, yLabelRightShift + 6, baseNumberPosition);
            lastYLabelEnd = baseNumberPosition + 2;
        }
    }

    // Give the x axis a bit of breathing space
    xOffset = xOffset + yLabelRightShift + 8;

    // Now draw horizontal lines across from the y axis
    g.setColor(new Color(180, 180, 180));
    for (double i = yStart; i <= maxY; i += yInterval) {
        g.drawLine(xOffset, getY(i), getWidth() - 10, getY(i));
    }
    g.setColor(Color.BLACK);

    // Draw the graph title
    int titleWidth = g.getFontMetrics().stringWidth(graphTitle);
    g.drawString(graphTitle, (xOffset + ((getWidth() - (xOffset + 10)) / 2)) - (titleWidth / 2), 30);

    // Draw the xLabel under the xAxis
    g.drawString(xLabel, (getWidth() / 2) - (g.getFontMetrics().stringWidth(xLabel) / 2), getHeight() - 5);

    // Now draw the data points
    double baseWidth = (getWidth() - (xOffset + 10)) / (maxX - minX);

    //      System.out.println("Base Width is "+baseWidth);
    // Let's find the longest label, and then work out how often we can draw labels
    int lastXLabelEnd = 0;

    // Draw the x axis labels
    for (double i = xStart; i <= maxX; i += xInterval) {
        g.setColor(Color.BLACK);
        String baseNumber = "" + i;
        baseNumber = baseNumber.replaceAll(".0$", ""); // Don't leave trailing .0s where we don't need them.
        // Calculate the new xOffset depending on the widest ylabel.
        int baseNumberWidth = g.getFontMetrics().stringWidth(baseNumber);
        int baseNumberPosition = (int) (xOffset + (baseWidth * i) - (baseNumberWidth / 2));

        if (baseNumberPosition > lastXLabelEnd) {
            g.drawString(baseNumber, baseNumberPosition, getHeight() - 25);
            lastXLabelEnd = baseNumberPosition + baseNumberWidth + 5;
        }
        // Now draw vertical lines across from the y axis
        g.setColor(new Color(180, 180, 180));
        g.drawLine((int) (xOffset + (baseWidth * i)), getHeight() - 40, (int) (xOffset + (baseWidth * i)), 40);
        g.setColor(Color.BLACK);
    }

    // Now draw the axes
    g.drawLine(xOffset, getHeight() - 40, getWidth() - 10, getHeight() - 40);
    g.drawLine(xOffset, getHeight() - 40, xOffset, 40);

    // Initialise the arrays containing the tooltips
    rectangles = new ArrayList<Rectangle>();
    tips = new ArrayList<String>();

    g.setColor(Color.BLUE);
    // Draw the data points
    double ovalSize = 5;
    // We distinguish two inputs since the x label does not start from 0.
    // used for computing the actual line points as if they were starting from 0.
    double[] inputVar = new double[data.length];
    double[] responseVar = new double[data.length];
    for (int d = 0; d < data.length; d++) {
        double x = getX(xCategories[d], xOffset) - ovalSize / 2;
        double y = getY(data[d]) - ovalSize / 2;
        g.fillOval((int) x, (int) y, (int) (ovalSize), (int) (ovalSize));
        g.drawString(toolTipLabels[d], (int) x + 2, (int) y + 16);
        inputVar[d] = Double.valueOf(xCategories[d]);
        responseVar[d] = data[d];

        // Tool tips
        Rectangle r = new Rectangle((int) x, (int) y, (int) (ovalSize), (int) (ovalSize));
        rectangles.add(r);
        tips.add(toolTipLabels[d]);
    }
    g.setColor(Color.BLACK);

    // Draw the intercept 

    // WARNING: Is drawing a least squares regression line asserting that "the distribution follows a power law" correct?
    // This is our case if we plot log-log..
    // It seems not in this paper (Appendix A) http://arxiv.org/pdf/0706.1062v2.pdf

    if (data.length > 1) {
        LinearRegression linReg = new LinearRegression(inputVar, responseVar);
        double intercept = linReg.intercept();
        double slope = linReg.slope();
        double rSquare = linReg.R2();

        // Let's now calculate the two points (x1, y1) and (xn, yn)
        // (x1, y1). We need to skip the areas where x1<minY and y1>maxY
        double x1 = minX;
        double y1 = slope * minX + intercept;
        if (y1 < minY) {
            x1 = (minY - intercept) / slope;
            y1 = minY;
        } else if (y1 > maxY) {
            x1 = (maxY - intercept) / slope;
            y1 = maxY;
        }
        // (xn, yn). maxX which essentially is inputVar[inputVar.length-1]
        double xn = maxX;
        double yn = slope * maxX + intercept;

        if (g instanceof Graphics2D) {
            ((Graphics2D) g).setStroke(new BasicStroke(1.5f));
        }
        g.setColor(Color.RED);
        g.drawLine(getX(x1, xOffset), getY(y1), getX(xn, xOffset), getY(yn));
        g.setColor(Color.BLACK);
        if (g instanceof Graphics2D) {
            ((Graphics2D) g).setStroke(new BasicStroke(1));
        }

        // Draw the legend for the intercept
        String legendString = "y = " + Precision.round(slope, 3) + "x";
        if (intercept < 0)
            legendString += " - " + Precision.round(-intercept, 3);
        else
            legendString += " + " + Precision.round(intercept, 3);
        int width = g.getFontMetrics().stringWidth(legendString);

        // First draw a box to put the legend in
        g.setColor(Color.WHITE);
        g.fillRect(xOffset + 10, 45, width + 8, 35);
        g.setColor(Color.LIGHT_GRAY);
        g.drawRect(xOffset + 10, 45, width + 8, 35);

        // Now draw the legend label
        g.setColor(Color.RED);
        g.drawString(legendString, xOffset + 13, 60);
        g.drawString("R^2 = " + Precision.round(rSquare, 3), xOffset + 13, 76);
        g.setColor(Color.BLACK);
    }

}

From source file:uk.ac.babraham.SeqMonk.Filters.GeneSetFilter.GeneSetScatterPlotPanel.java

public void paint(Graphics g) {
    super.paint(g);

    if (customRegressionValues != null & readyToDraw) {
        g.setColor(Color.magenta);
        for (int i = 0; i < customRegressionValues[0].length; i++) {

            // some of the points fall off the plotting area
            if ((getX(customRegressionValues[0][i]) > X_AXIS_SPACE)
                    && (getY(customRegressionValues[1][i]) < getHeight() - Y_AXIS_SPACE)) {
                g.fillOval(getX(customRegressionValues[0][i]), getY(customRegressionValues[1][i]), 3, 3);

            }//from  w  ww.  j av  a2 s  .  com
        }
    }

    //System.err.println("x for 5.377 = " + getX(5.377) + ", y for 4.761 = " + getY(4.761));

    //if(calculateLinearRegression && (simpleRegression != null)){
    /*      if(simpleRegression != null){
                  
             int x1;
             int x2;
             int y1;
             int y2;
                     
             // at intersect of x axis
             y1 = getY((simpleRegression.getSlope() * (double)(getValueFromX(X_AXIS_SPACE))) + simpleRegression.getIntercept());
                     
             if(y1 < (getHeight()-Y_AXIS_SPACE)){               
    x1 = X_AXIS_SPACE;
             }
                     
             // if y1 is off the screen
             else{
    y1 = getHeight()-Y_AXIS_SPACE;
            
    x1 = getX(((double)(getValueFromY(y1) - simpleRegression.getIntercept()))/simpleRegression.getSlope());
             }
                     
             y2 = getY((simpleRegression.getSlope() * (double)(getValueFromX((getWidth()-10)))) + simpleRegression.getIntercept());  
                     
             if(y2 <= 10){
    y2 = 10;
    x2 = getX(((double)(getValueFromY(y2) - simpleRegression.getIntercept()))/simpleRegression.getSlope());
             }
                     
             else{
    x2 = (getWidth()-10);
             }
                     
             g.setColor(Color.BLACK);
             g.drawLine(x1,y1,x2,y2);
    */

    /*   System.err.println("y intercept = " + simpleRegression.getIntercept() + ", slope = " + simpleRegression.getSlope() + 
       ", x intercept = " + (-(simpleRegression.getIntercept()/simpleRegression.getSlope())));
    */
}