Draw coordinate and curves - Java 2D Graphics

Java examples for 2D Graphics:Graphics

Description

Draw coordinate and curves

Demo Code

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;

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

public class Main extends JFrame {
  public static void main(String[] args) {
    Main opdr = new Main();
  }/*from   ww  w  .j a v a  2  s  . c  o m*/

  public Main() {
    super("line");
    setSize(600, 600);
    setTitle("2D Line");
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JPanel panel = new JPanel(new BorderLayout());
    JPanel topPanel = new JPanel(new FlowLayout());
    panel.add(topPanel, BorderLayout.NORTH);

    JLabel statusLabel = new JLabel("Draw a Line");
    topPanel.add(statusLabel);

    JPanel centerPanel = new Opdracht2_1a();
    panel.add(centerPanel, BorderLayout.CENTER);

    JPanel bottomPanel = new JPanel(new FlowLayout());
    panel.add(bottomPanel, BorderLayout.SOUTH);

    setContentPane(panel);
    setVisible(true);
  }

  public class Opdracht2_1a extends JPanel {
    public Opdracht2_1a() {
      setPreferredSize(new Dimension(600, 400));
    }

    public void paintComponent(Graphics g) {
      Graphics2D g2 = (Graphics2D) g;

      g2.translate(getWidth() / 2, getHeight() / 2);

      int x1 = 0;
      int y1 = 0;

      g2.drawLine(-300, -0, 300, 0);
      g2.drawLine(0, -300, 0, 300);

      for (double t = -8; t < 8; t++) {

        int x = (int) Math.pow(t, 2);
        int y = (int) Math.pow(t, 3);
        g2.drawLine(x1, y1, x, y);

        x1 = x;
        y1 = y;

      }
    }
  }

}

Related Tutorials