title: Drawing a simple 2D graph in java
body: <p>I got a below java code which plots a 2D graph where x axis has values 1,2,3,......20
  and y axis has values 21,14,18..........18
  The code works perfectly but the only problem is that the graph does not shows the corresponding x and y values on x axis and y axis respectively. I know it could be a small addition to the code. but i am new to java graphics and i am not able to figure out where to add the relevant code considering the time constraint.</p>
  
  <pre><code>/*Sample code */
  
  public class GraphingData extends JPanel {
  
      int[] data = {
          21, 14, 18, 03, 86, 88, 74, 87, 54, 77,
          61, 55, 48, 60, 49, 36, 38, 27, 20, 18
      };
      final int PAD = 20;
  
      protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D)g;
          g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                              RenderingHints.VALUE_ANTIALIAS_ON);
          int w = getWidth();
          int h = getHeight();
          // Draw ordinate.
          g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
          // Draw abcissa.
          g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
          // Draw labels.
          Font font = g2.getFont();
          FontRenderContext frc = g2.getFontRenderContext();
          LineMetrics lm = font.getLineMetrics("0", frc);
          float sh = lm.getAscent() + lm.getDescent();
          // Ordinate label.
          String s = "Average Byte Value";
          float sy = PAD + ((h - 2*PAD) - s.length()*sh)/2 + lm.getAscent();
          for(int i = 0; i &lt; s.length(); i++) {
              String letter = String.valueOf(s.charAt(i));
              float sw = (float)font.getStringBounds(letter, frc).getWidth();
              float sx = (PAD - sw)/2;
              g2.drawString(letter, sx, sy);
              sy += sh;
          }
          // Abcissa label.
          s = "file blocks";
          sy = h - PAD + (PAD - sh)/2 + lm.getAscent();
          float sw = (float)font.getStringBounds(s, frc).getWidth();
          float sx = (w - sw)/2;
          g2.drawString(s, sx, sy);
          // Draw lines.
          double xInc = (double)(w - 2*PAD)/(data.length-1);
          double scale = (double)(h - 2*PAD)/getMax();
          g2.setPaint(Color.green.darker());
          for(int i = 0; i &lt; data.length-1; i++) {
              double x1 = PAD + i*xInc;
              double y1 = h - PAD - scale*data[i];
              double x2 = PAD + (i+1)*xInc;
              double y2 = h - PAD - scale*data[i+1];
              g2.draw(new Line2D.Double(x1, y1, x2, y2));
          }
          // Mark data points.
          g2.setPaint(Color.red);
          for(int i = 0; i &lt; data.length; i++) {
              double x = PAD + i*xInc;
              double y = h - PAD - scale*data[i];
              g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));
          }
  
      }
  
      private int getMax() {
          int max = -Integer.MAX_VALUE;
          for(int i = 0; i &lt; data.length; i++) {
              if(data[i] &gt; max)
                  max = data[i];
          }
          return max;
      }
  
      public static void main(String[] args) {
          JFrame f = new JFrame();
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          f.add(new GraphingData());
          f.setSize(400,400);
          f.setLocation(200,200);
          f.setVisible(true);
      }
  }
  </code></pre>