请高手帮忙看一下,为什么我的这个程序只能画出lines中的第一条直线,而后面的都不能画出。
文件e://drawline//draw.txt中存储的是一系列坐标。
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() {
setTitle("4545");
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) {
System.out.println(l);
g2.draw(l);
}
}}
文件e://drawline//draw.txt中存储的是一系列坐标。
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() {
setTitle("4545");
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) {
System.out.println(l);
g2.draw(l);
}
}}
楼主【huangshua】截止到2008-07-10 16:31:17的历史汇总数据(不包括此帖):
发帖的总数量:45 发帖的总分数:752 每贴平均分数:16
回帖的总数量:59 得分贴总数量:19 回帖的得分率:32%
结贴的总数量:43 结贴的总分数:702
无满意结贴数:0 无满意结贴分:0
未结的帖子数:2 未结的总分数:50
结贴的百分比:95.56 % 结分的百分比:93.35 %
无满意结贴率:0.00 % 无满意结分率:0.00 %
值得尊敬
System.out.println(l);
g2.draw(l);
} 这个循环是不是有问题?
而如果你的txt文件中每行的数是3个以上,所画出来的线和只有前两个数一样。原因是:
你读入文件的时候是readLine()的,但是你只去了一行中的一个x和一个y,程序就开始读下一行了,而每行第三个和以后的数字都取不到。
是的,当时就是这样。
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() {
setTitle("4545");
setSize(500, 500);
Linepane lp = new Linepane();
this.getContentPane().add(lp, BorderLayout.CENTER); //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) {
//System.out.println(l);
g2.draw(l);
}
}}测试文件数据:1 1
60 120
0 50
300 0
160 125
我试了断点设那都不会同步显示