0

So I'm trying to draw a curve that has a taper profile like this : y=

y - profile

Since I'm using awt.Graphics2D, I only have the ability to draw shapes.

One way to draw curves is to draw curves is use use B´ezier curves. Is there a way convert this or a awt trick I'm not familiar with ?

rkrishnasanka
  • 166
  • 1
  • 9
  • Not sure what you mean by 'taper profile' (don't understand this term nor how it relates to the attached image). This being said you can draw curves using the [QuadCurve2D](https://docs.oracle.com/javase/7/docs/api/java/awt/geom/QuadCurve2D.html) class. – copeg Aug 12 '16 at 22:23
  • Graphics2D lets you draw polygons just fine so: just generate a lookup table of points on the curve, and draw the polygon from point to point? – Mike 'Pomax' Kamermans Aug 13 '16 at 19:19
  • @copeg I mean a curve profile - I'm drawing a tapered line where the width profile can be found using the equation I gave there. Where y = width of the line and x is how far we go and k is a constant – rkrishnasanka Aug 23 '16 at 23:08

1 Answers1

1

One option to draw a curve is point by point :

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

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

public class Curve extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(Color.RED);

        for (int x = 1; x <= 100; x++) {

            int y = getY(x);
            //2g does not support drawing a point so you draw a line 
            g2d.drawLine(x, y, x, y);
        }
    }

    /**
     *@param x
     *@return
     */
    private int getY(int x) {
        //change to the function you want 
        return  50+ (100/(1+ (3* x)));
    }

    public static void main(String[] args) {

        Curve points = new Curve();
        JFrame frame = new JFrame("Curve");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(points);
        frame.setSize(350, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }
}
c0der
  • 18,467
  • 6
  • 33
  • 65
  • See also [Drawing the Cannabis Curve](http://stackoverflow.com/questions/20799710/drawing-the-cannabis-curve) for doing much the same using a `GeneralPath`. – Andrew Thompson Aug 13 '16 at 13:15