private void drawXAxis(Graphics g, Font font) { if (!xAxis.isDisplayed()) return; Color oldColor = g.getColor(); g.setColor(Color.BLACK); float leftBorder = getLeftBorder(); int yVal = getHeight() - getNumPixelsReservedOnBottom(); g.drawLine((int) leftBorder, yVal, getWidth(), yVal); float xInterval = (getWidth() - leftBorder) / xAxis.getNumberTicks(); FontMetrics fm = g.getFontMetrics(font); for (int x = 1; x < xAxis.getNumberTicks(); x++) { float xPos = leftBorder + (xInterval * x); g.setColor(Color.GRAY); g.drawLine((int) xPos, yVal, (int) (xPos), yVal + 5); float axisValue = xAxis.getLowVal() + (x / ((float) xAxis.getNumberTicks())) * (xAxis.getHighVal() - xAxis.getLowVal()); String axisString = new String("" + (int) (axisValue + 0.5)); float tickLabelPos = xPos - fm.stringWidth(axisString) / 2; g.setColor(Color.BLACK); g.drawString(axisString, (int) tickLabelPos, yVal + 18); } g.setColor(oldColor); }
private void drawYAxisAndHorizontalLines(Graphics g, Font fontForAxisLabels) { Color oldColor = g.getColor(); g.setColor(Color.BLACK); float ySize = getHeight() - getNumPixelsReservedOnBottom(); if (selectedYAxis.isDisplayed()) g.drawLine( selectedYAxis.getNumPixelsReserved(), 0, selectedYAxis.getNumPixelsReserved(), (int) ySize); if (selectedYAxis.getNumberTicks() < 1) return; g.setFont(fontForAxisLabels); int leftBorder = getLeftBorder(); if (selectedYAxis.isDisplayed()) { g.setColor(Color.RED); g.drawString(selectedYAxis.getAxisName(), selectedYAxis.numPixelsReserved, 10); g.setColor(Color.BLACK); } float yInterval = ySize / selectedYAxis.getNumberTicks(); FontMetrics fm = g.getFontMetrics(fontForAxisLabels); int stringAscent = fm.getAscent(); for (int x = 1; x < selectedYAxis.getNumberTicks(); x++) { int yVal = (int) (yInterval * x); g.setColor(Color.GRAY); g.drawLine(leftBorder, yVal, getWidth(), yVal); float axisValue = selectedYAxis.getHighVal() - (x / ((float) selectedYAxis.getNumberTicks())) * (selectedYAxis.getHighVal() - selectedYAxis.getLowVal()); String axisString = getYAxisTickLabel(axisValue); int tickLabelPos = selectedYAxis.getNumPixelsReserved() - fm.stringWidth(axisString) - 2; g.setColor(Color.BLACK); if (selectedYAxis.isDisplayed()) g.drawString(axisString, tickLabelPos, yVal + stringAscent / 2); } g.setColor(oldColor); }