0
テキストの読み込みとその変数について
黒丸を指定した時間、指定した場所に上から下に流していくというプログラムをつくっているのですが、テキストファイルの読み込みはうまく言ったものの、そのあとのpaintcomponentのところまで変数にいれた数値を引き継げません。
何がどう間違えているのかがよくわからないのですが、どうしたら正しく値を引き継ぐことができるのか教えていただけますでしょうか。
//mdata.text
0,30,5
60,90,5
120,120,5
//MovingPanel.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.concurrent.*;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileNotFoundException;
class MovingPanel extends JPanel implements ActionListener{
private Timer timer;
private int x1,y1;
private int x2, y2;
public int idtime = 3000;
MovingBall mbx;
MovingPanel(){
y1 = 0;
try{
FileReader filereader = new FileReader("mdata.txt");
BufferedReader br = new BufferedReader(filereader);
String line;
while((line = br.readLine() )!= null){
int first = line.indexOf(",");
String ch1 = line.substring(0,first);
//System.out.println(ch1);
int second = line.indexOf(",",first);
String ch2 = line.substring(first,second);
//System.out.println(ch2);
String ch3 = line.substring(second+1,line.length());
//System.out.println(ch3);
MovingBall mbx = new MovingBall(Integer.parseInt(ch1), Integer.parseInt(ch2), Integer.parseInt(ch3));
}br.close();
}
catch(FileNotFoundException e){
System.out.println(e);
}
catch(IOException e){
System.out.println(e);
}
timer = new Timer(10, this);
timer.start();
}
//paintcomponent
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (mbx.IsMoving()) {
g.fillOval(mbx.x(), mbx.y(),30,30);
}
}
//actionperformed
public void actionPerformed(ActionEvent e){
y1=y1+5;
if (y1>60000) timer.stop();
this.repaint();
}
}
//MovingPanel end
//MovingBall start
class MovingBall {
private int myx, myy, mydelay, count, mystep;
MovingBall(int distance, int delay, int step) {
myx = 30 + distance;
myy = -30;
mydelay = delay;
mystep = step;
count = 0;
}
public boolean IsMoving() {
if (count > mydelay) {
myy = myy + mystep;
return true;
}
else {
count++;
return false;
}
}
public int x() { return myx; }
public int y() { return myy; }
}
//MovingFrame start
class MovingFrame extends JFrame {
public MovingFrame(){
this.setTitle("Random Frame");
this.setSize(900,600);
this.add(new MovingPanel());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String argv[]) {
new MovingFrame();
}
}