[英]How to draw a taper profile using java awt.Graphics2D
因此,我試圖繪制一條曲線,該曲線具有如下錐度輪廓:y =
由於我使用awt.Graphics2D ,所以我只能繪制形狀 。
繪制曲線的一種方法是使用B´ezier曲線來繪制曲線。 有沒有一種方法可以轉換這個或我不熟悉的awt技巧?
繪制曲線的一種方法是逐點:
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.