Draw exponential curve - Java 2D Graphics

Java examples for 2D Graphics:Graphics

Description

Draw exponential curve

Demo Code

     /* w  w w  .  ja  v  a 2 s  .c o  m*/
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JFrame {
  private static final long serialVersionUID = 6;

  public static void main(String arg[]) {
    JFrame frame = new Main();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(new Dimension(400, 400));
    frame.pack();
    frame.setVisible(true);
  }

  public Main() {
    JPanel panel = new DrawStuff();
    panel.setPreferredSize(new Dimension(800, 800));
    add(panel);
  }

  class DrawStuff extends JPanel {
    private static final long serialVersionUID = -1L;
    int nPoints = 3000;
    public void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.translate(200, 200);
      int x1 = 0;
      int y1 = 0;
      int x2;
      int y2;
      for (int i = 0; i < nPoints; i++) {
        x2 = (int) Math.pow(i, 2);
        y2 = (int) Math.pow(i, 3);
        g2.drawLine(x1, y1, x2, y2);
        x1 = x2;
        y1 = y2;
      }
    }
  }
}

Related Tutorials