我正在编写一个程序来绘制 [x(t),y(t)] 与变量 t 在一定范围内(由用户输入)。到目前为止,我已经创建了 3 个向量来保存 t、x(t) 和 y(t) 的值。我目前的方法是创建点向量(大约 1000 个点),然后在向量中的两个相邻点之间画一条线(或路径)。然而,结果并不如我所料。
来自用户的完整格式:
Plot [x(t),y(t)] x=a..b y=a..b t=a..b where a,b are the range of x,y,t
例如,用户可以输入函数:
x(t) = 5*sin(3t + 5), t=-19..19
y(t) = 4*cos(2t), t=-19.19
这是我的绘图代码:
public static void drawGraph(String sf, String sx, String sy, String st) {
JFrame mygraph = new JFrame("PlotGraph v0.1");
final Vector<Double> range_t = getRangeT(st); //get the range of t
//create a corresponding vectors of x and y based on values of t
final Vector<Double> range_x = getRangeX(range_t,funcX,var);
final Vector<Double> range_y = getRangeY(range_t,funcY,var);
//draw the graph to a JPanel, our graph is actually just a collection of points connecting 2 points
mygraph.add(new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.BLUE );
g2.setStroke(new BasicStroke(1));
GeneralPath gpx = new GeneralPath();
GeneralPath gpy = new GeneralPath();
for (int i=0; i<998; i++) {
gpy.moveTo(range_t.get(i),range_y.get(i));
gpy.curveTo(range_t.get(i),range_y.get(i),range_t.get(i+1),range_y.get(i+1),range_t.get(i+2),range_y.get(i+2));
gpx.moveTo(range_t.get(i),range_x.get(i));
gpx.curveTo(range_t.get(i),range_x.get(i),range_t.get(i+1),range_x.get(i+1),range_t.get(i+2),range_x.get(i+2));
//g2.draw(lineY);
g2.draw(gpx);
g2.draw(gpy);
}
g2.dispose();
}
});
mygraph.setBounds(125,25,650,600);
mygraph.setLocationRelativeTo(null);
mygraph.setDefaultCloseOperation(mygraph.EXIT_ON_CLOSE);
mygraph.setVisible(true);
}
这是我从上面的 2 个函数中得到的:
问题: 有什么办法可以让情节变得更好(按比例放大)?!