0

i have a 3D drawing program which gives me a list of coordinates in a file (.txt or .properties)

the file contains something like that: (almost 5000 lines in it.)

drawCircles(2.03, 5.09, 2);
drawCircles(5.02, 6.19, 2);
drawCircles(6.12, 2.91, 2);
drawCircles(3.12, 1.12, 2);
drawCircles(4.37, 8.92, 2);
...

drawCircles is a method for my program which draws ellipses. Everytime i open file and copy all lines into my program and run it.

is there any way for my program to directly read file into program and run it.

Here is my main program to draw circles:

public class DrawClass extends JPanel{
ArrayList<Ellipse2D> arrayCircles = new ArrayList<>();
DrawClass(){
    setBounds(0,0,1000,1000);
    coordinatesFromFile();
}
void coordinatesFromFile(){
    //Here is where i paste coordinate from file
    //all i want is read file from here and run codes
}

void drawCircles(double e1,double e2,double r){
    arrayCircles.add(new Ellipse2D.Double(e1, e2, r, r));
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setColor(Color.red);
    for(int i=0;i<arrayCircles.size();i++){
        g2.draw(arrayCircles.get(i));
    }


   }    
}
Murat
  • 72
  • 10
  • 3
    I would suggest to read each line and extract values using a pattern. – jhamon Jan 14 '16 at 09:46
  • is there any way to read and directly run? (Simply add all the lines to program - like copy paste) – Murat Jan 14 '16 at 09:50
  • I downvoted because there is no sign of any effort into searching for reading files in Java. – Rob Audenaerde Jan 14 '16 at 09:50
  • it's ok. But i searched for two days for solution and i couldn't find it. Believe me i did it. All i found is reading/writing txt or properties. There is no explanation or question like i asked for. – Murat Jan 14 '16 at 09:53
  • 2
    I upvoted, because he wants to execute the commands in the text file, so I assume he is looking to evaluate those expressions. What else could he mean ? Yes a beginners question but not a bad one at all. –  Jan 14 '16 at 09:54
  • that is exactly what i want. thank you @Werner Van Belle – Murat Jan 14 '16 at 09:55
  • 1
    It's possible to [evaluate a string/file as JavaScript](http://stackoverflow.com/questions/2605032/is-there-an-eval-function-in-java) – jhamon Jan 14 '16 at 09:56
  • What you want to achieve is possible through the use of *Reflection*, as per the linked question. If this is not what you are after please let me know . – npinti Jan 14 '16 at 09:58
  • This seems like a very brittle way to arrange things. Are you stuck with this format? Is it some sort of contrived exercise ? – Kerr Jan 14 '16 at 10:02
  • i trying to write a program for drawing circles for certain coordinates. The coordinates comes in a file from another program like AutoCAD. Normally i copy and paste every time but i think there must be another way to do that. – Murat Jan 14 '16 at 10:05
  • i will try to use The Recflection API. i will share the results – Murat Jan 14 '16 at 10:11
  • 1
    The best way is to extract the coordinate data using regex, rather than trying to execute code you pick up from a text file. – Kerr Jan 14 '16 at 10:11

0 Answers0