文件e://drawline//draw.txt里面放的是很多组坐标,这些坐标都在同一条曲线上,想用这个程序将此曲线画出来,
就是实现不了,不知道哪里的问题,请高手指点。
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;public class Dtawline {
public static void main(String args[]) {
try {
Lineframe lf = new Lineframe();
lf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lf.setVisible(true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}class Lineframe extends JFrame {
public Lineframe() throws IOException {
setTitle("4545");
setSize(500, 500);
Linepane lp = new Linepane();
add(lp); }
}class Linepane extends JPanel {
Line2D li; double x = 0, y = 0; double x1 = 0, y1 = 0; public Linepane() { } public void paintComponent(Graphics g) { try {
FileReader fr = new FileReader("e://drawline//draw.txt");
BufferedReader br = new BufferedReader(fr);
while (true) {
String yihang = br.readLine();
if (yihang == null)
break;
String xy[] = yihang.split(" ");
x1 = Double.valueOf(xy[0]);
y1 = Double.valueOf(xy[1]);
li = new Line2D.Double(x, y, x1, y1); super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g; g2.draw(li); x = x1;
y = y1;
continue; }
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }}

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【huangshua】截止到2008-07-10 10:50:37的历史汇总数据(不包括此帖):
    发帖的总数量:44                       发帖的总分数:722                      每贴平均分数:16                       
    回帖的总数量:51                       得分贴总数量:19                       回帖的得分率:37%                      
    结贴的总数量:39                       结贴的总分数:632                      
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:5                        未结的总分数:90                       
    结贴的百分比:88.64 %               结分的百分比:87.53 %                  
    无满意结贴率:0.00  %               无满意结分率:0.00  %                  
    楼主加油
      

  2.   

    super.paintComponent(g);
    该句放方法首行或直接去除
    在行中调用会擦除之前所作的绘画操作只保留余下的 又double-buffering是默认打开的,所以通常在paintComponent方法中第一件做的事就是通过 
    super.paintComponent来清除off-screen bitmap 
      

  3.   

    去掉了,可是问什么还是只能画出第一条直线,程序修改如下:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.geom.Line2D;
    import java.awt.geom.Point2D;
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;import javax.swing.JFrame;
    import javax.swing.JPanel;public class Dtawline {
    public static void main(String args[]) {
    Lineframe lf = new Lineframe();
    lf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    lf.setVisible(true);
    }}class Lineframe extends JFrame {
    public Lineframe() {
    this.setTitle("4545");
    this.setSize(500, 500);
    Linepane lp = new Linepane();
    add(lp); }
    }class Linepane extends JPanel { double x = 0, y = 0; ArrayList<Line2D> lines = new ArrayList<Line2D>(); public Linepane() { try {
    FileReader fr = new FileReader("e://drawline//draw.txt");
    BufferedReader br = new BufferedReader(fr);
    int numb = 0;
    double x1 = 0, y1 = 0;
    Line2D li;
    while (true) { String yihang = br.readLine();
    if (yihang == null) {
    break;
    }
    String xy[] = yihang.split(" ");
    x1 = Double.valueOf(xy[0]);
    y1 = Double.valueOf(xy[1]);
    Point2D last = new Point2D.Double(x, y);
    Point2D end = new Point2D.Double(x1, y1);
    li = new Line2D.Double(last, end);
    System.out.println(li);
    lines.add(li);
    repaint();
    x = x1;
    y = y1;
    } } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } } public void paintComponent(Graphics g) {
    // super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.RED);
    for (Line2D l : lines) { g2.draw(l);
    }
    }}