简体   繁体   中英

How to draw a taper profile using java awt.Graphics2D

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

y-个人资料

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 ?

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);

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM