Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to read from all files in a directory & plot the contents?

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    /*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-.../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.
    ---->diagram: http://i45.tinypic.com/2z4fgog.jpg

    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

     [B]StarJPanel.java[/B]
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.Scanner;
     
    public class StarJPanel extends JPanel implements ActionListener {
     
    	private Star[] stars;
    	private Timer timer;
     
    	public StarJPanel() {
     
    		setBackground(Color.black);
    		timer = new Timer(5, this);
     
    		stars = getArrayOfStars();
     
    		timer.start();
    	}
     
    	private Star[] getArrayOfStars() {
     
    		// This method reads the Stars data from a file and stores it in the stars array.
     
    		int howMany = Integer.parseInt(Keyboard.readInput());
    		Star[] stars = new Star[howMany];
     
    		for (int i = 0; i < stars.length; i++) {
    			String input = Keyboard.readInput();
    			Scanner fields = new Scanner(input);
    			double x = Double.parseDouble(fields.next());
    			double y = Double.parseDouble(fields.next());
    			int draper = Integer.parseInt(fields.next());
    			double magnitude = Double.parseDouble(fields.next());
    			String namesString = "";
    			String[] names = {};
    			if (fields.hasNext()) {
    				namesString = fields.nextLine();
    				names = namesString.trim().split("; ");
    			}
    			stars[i] = new Star(x, y, draper, magnitude, names);
    		}
    		return stars;
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		repaint();
    	}
     
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
     
    		for(int i = 0; i < stars.length; i++) {
    			stars[i].coordinateToPixel(stars[i].getX(), stars[i].getY());
     
    			stars[i].drawStar(g);
    		}
    	}
    }

    [B]Star.java[/B] 
    import java.awt.*;
    import javax.swing.*;
     
    public class Star {
     
    	private double x, y;		// coordinates of star
    	private int draper;			// Henry Draper number (unique identifier)
    	private double magnitude;	// Magnitude (brightness) of star
    	private String[] names;		// Star name(s) - not always present
    	private int newX;
    	private int newY;
    	private int size;
     
    	public Star( double x, double y, int draper, double magnitude, String[] names ) {
    		this.x = x;
    		this.y = y;
    		this.draper = draper;
    		this.magnitude = magnitude;
    		this.names = names;
     
    		size = (int)(10/(magnitude + 2));
    	}
    	public void coordinateToPixel(double x, double y) {
    		newX = (int) ( (x + 1) * 350);
    		newY = (int) ( (y - 1) * -350);
    	}
    	public void drawStar(Graphics g) {
    		g.setColor(Color.white);
    		g.drawOval( newX, newY, size, size);
    	}
    	public int getNumberOfNames() {
    		return names.length;
    	}
    	public double getX() {
    		return x;
    	}
    	public double getY() {
    		return y;
    	}
    }

    As you can see I have done:
    Step 1: in Star class, with method: coordinateToPixel(double x, double y)
    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));

    Please tell me if I have done 3 steps without confusing & incoherent code, I guess I have written the code differently from what ppl write. Somehow thats how we were taught to write. For me, my way gets a bit confusing sometimes!!

    Now I'm left stuck with Step 4.
    I have a folder where all my StarApp, StarJPanel, etc files are & a folder called Constellation
    Inside constellation folder I have files that contain pairs of star names that make up lines in the constellation.
    --Heres the constellation folder & star-data.txt file:
    Constellations & star-data.rar


    Although I'm thinkin i can use below code to read contents in files & use g.drawLine somewhere in StarJPanel. But I'm not sure 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 ???

    import java.io.*;
     
    public class reader {
    	public static void main(String args[]) throws Exception {
    		FileReader fr = new FileReader("Constellation/....");         /** Don't know what to put in ....... **/
    		BufferedReader br = new BufferedReader(fr);
    		String s;
     
    		while((s = br.readLine()) != null) {
    			System.out.println(s);
     
    		}
     
    		fr.close();
    	}
    }


    Not sure how Step 4 is done please guide me.
    By the way, 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 into screen.
    Last edited by 123; February 10th, 2010 at 03:49 AM.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,102
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: How to read from all files in a directory & plot the contents?

    If you have a directory name, you can get a ref to every file in the directory by calling listFiles(), and from there read each file accordingly. You can consider setting up a Set or a HashMap of your stars based upon their name, then it is easy and fast to look them up by name and retrieve the coordinates to draw the constellations.

Similar Threads

  1. Java program which can list all files in a given directory
    By JavaPF in forum Java Programming Tutorials
    Replies: 8
    Last Post: July 9th, 2013, 03:38 AM
  2. About META-INF directory?
    By chinni in forum Java Servlet
    Replies: 2
    Last Post: February 9th, 2010, 09:36 AM
  3. where can i find the tmp/foo directory?
    By Idy in forum Java IDEs
    Replies: 11
    Last Post: January 19th, 2010, 10:21 AM
  4. Class directory
    By Kit in forum Java Theory & Questions
    Replies: 7
    Last Post: October 11th, 2009, 10:07 AM
  5. Convert contents of JTextArea / JEditorPane to PDF
    By rangarajank in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 30th, 2009, 02:38 PM