• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to read from all files in a directory & plot the contents by processing contents?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/*PS: This topic has something to do with Java graphics as well*/
Hi,
I'm working on a star map (graphics program), the final output of the program is similar to:
http://nifty.stanford.edu/2009/reid-starmap/starchart.jpg

These are the steps i follow:
Step 1
Write a method to convert between star coordinate system to the Java picture coordinate system.
The star coordinate system has (0,0) in the center, and -1 and 1 as the extremes.
The Java graphics coordinate system has (0,0) as the top-left corner, and positive numbers extend down and right up to the screen size.
---->visualised as diagram: coord systems

Step 2
Read the contents of the star-data.txt file, and plot the stars on a Java graphics window. Use a black background, and plot the stars as white circles.

star-data.txt contains info on 3,526 stars, this number appears on the first line of the file.
Subsequent lines has the following fields:
-x, y coordinates for stars (in star coordinate system, e.g. 0.512379, 0.020508)
- Henry Draper number (just a unique identifer for the star)
-magnitude (or brightness of star)
-names of some stars. A star may have several names.



Step 3
Vary the size of the circles to reflect their magnitude. Since brighter stars have smaller magnitude values, you will need to calculate the circle radius, say, 10/(magnitude + 2).

Step 4
Read from all files in constellation folder, and plot them on top of the stars.
Each file contains pairs of star names that make up lines in the constellation.

I have already done Steps 1 - 3.
I'm using files :
-StarApp.java as my application class that has main() method
-StarJFrame.java ,this class creates the window & defines its properties & behavior
-StarJPanel.java ---> heres the code, for this class, below
-Star.java -----> heres the code, for this class, below
StarJPanel.java:-

Star.java:-

As you can see I have done:
Step 1: in Star class, with method: coordinateToPixel(double x, double y), where I'm using 350 as scaling factor since i have set 700 as width & height of JFrame.
Step 2: in StarJPanel class with method: private Star[] getArrayOfStars()
(PS: I'm also using Keyboard class to read lines from the stars.txtfile)
Step 3: in Star class, with method: drawStar(Graphics g), where size = (int)(10/(magnitude + 2));
My output is similar to the final output:http://nifty.stanford.edu/2009/reid-starmap/starchart.jpg .
Exept mine doesn't have the lines joing various dots. I guess thats what Step 4 does & I'm not sure how to make step 4 work!
I'm using textpad as my editor & complier.
So I have to go: Tools > Run & in parameters i put: "StarApp < star-data.txt" inorder to load the Stars onto JPanel.

I have a folder where all my StarApp, StarJPanel, etc files are & a folder called Constellation
Inside constellation folder I have files which contain, pairs of star names that make up lines in the constellation.
**PS:----Heres the constellation folder & star-data.txt file:
http://www.mediafire.com/file/z2nze3qmnnw/Constellations & star-data.rar**

For step 4, I'm thinkin i can add a: read method (below code) to read contents in files from Cons. folder & g.drawLine somewhere in StarJPanel. I'm not sure on how to find same names of stars in constellation files with names in the already set array from star-data & then join the coordinates with g.drawLine ???

Not sure how Step 4 is done please guide me.
Also. Please tell me if I have done all the 3 steps without confusing & incoherent code, I guess my way of writing the code is different from other peoples way.
The way I was taught java is confusing for me sometimes!!
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds to me like step 4 is drawing lines. You already know how to convert coordinates to pixels, and presumably know the the Graphics class contains a drawLine() method. What part are you having trouble with?
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Based on the title of your question, if you are just looking for a way to get the list of all the files in a folder, try the following:

You can also use file name filters, if you want to choose only certain files from the folder.
 
Raj Gee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Step 4 is not just about drawing lines with coordinates. I have to extract names in Constellation files which match names array from star-data.txt & then join the name's coordinates in order to form a constellation.

The part I'm having trouble with is: how to extract names in Constellation files, matching them with names array from star-data.txt & then joining the name's coordinates in order to form a constellation?

g.drawLine(x1, y1, x2, x2);

How to get x2 & y2?
 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, got you. I would create a Star class, with name and coordinates fields. As you process the star data, store it in Star objects and store the objects in some sort of collection. (A hash map, with the star name as a key would be my choice.) When you get around to processing constellations, you can look up the stars in your collection and get the coordinates.

This sounds like a really cool assignment actually. If I ever teach programming again, I might "borrow" this example.
 
reply
    Bookmark Topic Watch Topic
  • New Topic