List of usage examples for java.awt Graphics2D rotate
public abstract void rotate(double theta);
From source file:uk.ac.babraham.BamQC.Graphs.ScatterGraph.java
@Override protected void paintComponent(Graphics g) { g.setColor(Color.WHITE);//from w w w . java 2s. c o m 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); } }