在循环外使用g.fillRect(50,50,50,50);可以正常显示
在循环内则无法绘制
代码如下
```java
package Rich;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
public class RoutePanel extends JPanel{
private final String FILENAME="RoutePositions.txt";
private final int LINELENGTH=4*2+1;
private RandomAccessFile file=null;
private int number;
public RoutePanel() {
try {
file=new RandomAccessFile(FILENAME,"rw");
}catch(FileNotFoundException e){
System.out.println(e.getStackTrace());
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
try {
while(file.getFilePointer()<file.length()) {
String[] pos=file.readLine().split(" ");
int x=Integer.parseInt(pos[0]);
int y=Integer.parseInt(pos[1]);
g.fillRect(x, y, 50, 50);
}
}catch(IOException e) {
System.out.println(e.getMessage());
}
}
public Dimension getPrefferedSize() {
return new Dimension(50,50);
}
}
```