public class FrameDemo extends JPanel
{
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
String[] read file contents using FileArrayProvider
for loop through above String array
g2.draw(new Line2D.Double(...));
}
public static void main(String[] args)
{
JFrame frame = new JFrame("JFrame Example");
frame.add(new FrameDemo());
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class FileArrayProvider // taken from StackOverFlow
{
public String[] readLines(String filename) throws IOException
{
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
java.util.List<String> lines = new ArrayList<String>();
String line = null;
while((line=bufferedReader.readLine())!=null)
{
lines.add(line);
}
bufferedReader.close();
return lines.toArray(new String[lines.size()]);
}
}
嗨,在paintComponent方法裏面,我爲了繪製線條而讀取文件行,我得到了很多線條,有時候超過幾百萬。因此,我的框架凍結了一段時間......這是否是正確的做法?有沒有更好的方法來做到這一點?這樣它就不會凍結,並在顯示框架時繪製所有線條?java-jframe-drawline
我認爲默認情況下雙緩衝是在Swing中不是嗎? – 2010-11-24 18:13:03