【发布时间】:2016-07-25 07:51:50
【问题描述】:
如何绘制一条由 x、y 点组成的线?我尝试使用 Graphics 2D,但出现错误“Graphics 类型中的方法 drawLine(int, int, int, int) 不适用于参数(双精度、双精度、双精度、双精度)”。如果我不使用 Graphics 2D,如何在 java 中绘制线条?
public void paint(Graphics g){
float []data= Series.getY();
double x1 = 0;
double y1 = 0;
for (int i = 0; i < data.length; i++){
double x2 = i;
double y2 = data[i];
g.drawLine(x1, y1, x2, y2);
y1 = y2;
x1 = x2;
}
}
我试过了
public class PlotPanel {
public void paint (Graphics g) {
float []data= Series.getY();
Graphics2D g2 = (Graphics2D) g;
double y1 = 0;
double x1 = 0;
for (int i = 0; i < data.length; i++){
double x2 = i;
double y2 = data[i];
g2.draw(new Line2D.Double(x1, y1, x2, y2));
y1 = y2;
x1 = x2;
}
}
public static void main(String args[]) {
PlotPanel pp = new PlotPanel();
pp.paint(g2);
}
}
它说 g2 不能解析为变量。我如何检查它是否有效?
【问题讨论】:
-
请编辑您的帖子以显示您在标签中使用的编程语言。