aspose file tools*
The moose likes Beginning Java and the fly likes much trouble reading files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "much trouble reading files" Watch "much trouble reading files" New topic
Author

much trouble reading files

Joe Panully
Greenhorn

Joined: Oct 10, 2004
Posts: 22
I have a class called TrackerView which I know works 100% correctly, and it creates a frame with a text field and a panel (specifically, an instance of the DrawingPanel class). The text field is used to retrieve the hurricane path filename. The panel is used for drawing the map of the USA and the hurricane path.

I have written the DrawingPanel class, however right now I am just trying to get it to display the map of the usa I have, I'm ignoring putting the path in of the hurricane. The file, which I have in that directory that I am trying to read is called usa. Here is my code:

import javax.swing.*;
import java.awt.*;

/**
* DrawingPanel creates a panel used for drawing
* a map
*/
public class DrawingPanel extends JPanel {

private Graphics g; // used for drawing on the panel
private boolean timeToDrawPath = false; // tells if it is time to draw
private String pathFile = null; // the filename of the hurricane path
private BufferedReader br;
private int intValue;


/**
* Creates a new, white DrawingPanel.
*/
public DrawingPanel() {

setBackground(Color.white); // for drawing the maps

}

/**
* This method is called automatically by Java. There will NEVER
* be a direct call to paint() anywhere in the code. Anytime that we
* neet to update what is drawn on the screen, we call repaint().
*
* @param grph The graphics window to paint
*/
public void paint(Graphics grph) {

g = grph;

super.paint(g);
processUSA();

if (timeToDrawPath) drawPath();

}

/**
* Reads the USA coastline coordinates from a file and draws the path
* to the panel
*/
public void processUSA(String usa) {
try {
br = new BufferedReader(new FileReader(usa));
}

catch (IOException e) {
System.err.println("Input File Error: " + e);
}

try {
intValue = 0;
String s;

while ((s = br.readLine()) != null) {
intValue = Integer.parseInt(s);
System.out.println("Value just read : " + intValue);
}

}

catch (IOException e) {
System.err.println("Read error: " + e);
System.exit(1);
}

g.drawLine(intValue);



}

/**
* Receives the file name containing the
* coordinates for the hurricane path
*
* @param filename file containing the coordinates for
* the line segments that produce
* the path of the hurricane
*NOT GOING TO WORRY ABOUT THIS METHOD UNTIL I CAN GET THE MAP
*/
// public void processPath(String usa){
// pathFile = usa;
// timeToDrawPath= true;
// repaint();

// }

/**
* Reads the hurricane path coordinates from the given file and draws
* the path.
*/
private void drawPath() {

g.drawLine(50,50,200,200); //just a place holder

}

}

----------------------------------------------------------------------

ERRORS:
DrawingPanel.java:13: cannot resolve symbol
symbol : class BufferedReader
location: class DrawingPanel
private BufferedReader br;
^
DrawingPanel.java:38: processUSA(java.lang.String) in DrawingPanel cannot be applied to ()
processUSA();
^
DrawingPanel.java:50: cannot resolve symbol
symbol : class BufferedReader
location: class DrawingPanel
br = new BufferedReader(new FileReader(usa));
^
DrawingPanel.java:50: cannot resolve symbol
symbol : class FileReader
location: class DrawingPanel
br = new BufferedReader(new FileReader(usa));
^
DrawingPanel.java:53: cannot resolve symbol
symbol : class IOException
location: class DrawingPanel
catch (IOException e) {
^
DrawingPanel.java:68: cannot resolve symbol
symbol : class IOException
location: class DrawingPanel
catch (IOException e) {
^
DrawingPanel.java:73: drawLine(int,int,int,int) in java.awt.Graphics cannot be applied to (int)
g.drawLine(intValue);
^
7 errors


Help?
[ November 01, 2004: Message edited by: Joe Panully ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
To start, you should be using paintComponent() instead of paint().

You're trying to draw lines (from point to point) to represent a map of
the usa (rather than print an image of the usa)?

if so, read in your points to an array (if number of points is known) or to
an arraylist, pass the array/list to the panel, then draw the lines in paintComponent()

something like this (aircode)

Joe Panully
Greenhorn

Joined: Oct 10, 2004
Posts: 22
Well, let me just ask this, why am I getting an error declaring and calling the new BufferedReader?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
import java.io.*;
Joe Panully
Greenhorn

Joined: Oct 10, 2004
Posts: 22
I more or less understand what you're saying, but as I am so new to java I'm not sure how to implement it. Also, the file I have I don't think is "formatted" because it's just one number per line and like the number in line 1 and the number in line2 are supposed to make a point (line1#,line2#), I'm not sure how to handle this really. This is what I wrote and now I'm stuck again:



[ November 01, 2004: Message edited by: Joe Panully ]

[ November 01, 2004: Message edited by: Joe Panully ]
[ November 01, 2004: Message edited by: Joe Panully ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Just extending my earlier code, this will draw a diamond shape on the panel.

this is the contents of Test.txt (8 lines)

300
300
400
200
500
300
400
400

Joe Panully
Greenhorn

Joined: Oct 10, 2004
Posts: 22
I am still confused. Mind you I use another program to actually display this, and I know that works, but right now it just shows a blank screen. I'm sorry I'm being so thickheaded, but I think I understand how this is <i>supposed</i> to work, I just don't see why it's not.


[ November 01, 2004: Message edited by: Joe Panully ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Seems you want to do this all in one, if so, try this

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Can you please explain what you mean by "won't work"? Does your code compile? If not, what are the compiler errors? If so, what happens when you run it? These and any other details that you deem important can help us answer your question more quickly. Also, if you happen to have any guesses why it "won't work" feel free to voice them. It could give us a place to start looking rather than trying to go through all of your code.

Also, when you post code in the future, please use the UBB CODE tags. This will help preserve your formatting so that it is easier for us to read.

Thanks,

Layne


Java API Documentation
The Java Tutorial
 
Consider Paul's rocket mass heater.
 
subject: much trouble reading files