Solved

FileReader info -> drawLine in graph

Posted on 1998-11-14
4
355 Views
Last Modified: 2010-03-30
Anyone have an example of reading data from a data file
(FileReader, BufferedReader) then graphing the data
in an Applet (paint, drawLine)?

rjcole@clearsail.net
0
Comment
Question by:robjay
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
heyhey_ earned 100 total points
ID: 1227432
here is some code - how to read some data from file and draw into the screen . Only Applications can access files on the local disk, if want to use this code in Applet you' have to change FileInputStream with new URL(...) and URL.getStream(); (so you'll get the information from the remote host)

data.txt
---start
10,10
100,10
100 100 10 100 10 10 50 50
---end

the app code
-- start

import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;


public class PaintTest extends Panel
{
    Vector data;

public PaintTest() {
    super();
 }

 public void paint(Graphics g)
 {
    if (data == null) return;
    int len = data.size();
    if (len < 2) return;
    int[] cur;
    int[] next;
    cur = (int[]) data.elementAt(0);
    next = (int[]) data.elementAt(0);
    for (int i = 1; i < len; i++)
    {
        cur = next;
        next = (int[]) data.elementAt(i);
        g.drawLine(cur[0], cur[1], next[0], next[1]);
    }
    cur = (int[]) data.elementAt(0);
    g.drawLine(cur[0], cur[1], next[0], next[1]);
 }

 public void loadFile (String name)
 {
    data = new Vector();
    int x,y;
    int[] cur;
    String st;
   
    try {
        FileInputStream is = new FileInputStream(name);
        StreamTokenizer t = new StreamTokenizer(is);
        t.whitespaceChars(',', ',');
        while (true) {
       
            if (t.nextToken() == StreamTokenizer.TT_EOF) return;
    //        st = t.sval;
            x = (int) t.nval;
            System.out.println("\nnext token = " + x);
            if (t.nextToken() == StreamTokenizer.TT_EOF) return;
            y = (int)t.nval;
            System.out.println("\nnext token = " + y);
            cur = new int[2];
            cur[0] = x;
            cur[1] = y;
            data.addElement(cur);    
        }
    } catch (Exception e )
    {
        System.out.println("Error caught");
        e.printStackTrace();
    }
 }

public static void main (String[] args) {
    Frame f = new Frame();
    f.setLayout(new BorderLayout());
    PaintTest test = new PaintTest();
    test.loadFile("c:\\data.txt");

    f.add(test , BorderLayout.CENTER);
    f.setSize(150,150);
    f.addWindowListener(
    new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    }
    );
//    f.pack();
    f.show();

}
}
--- end

hope this helps
  heyhey

P.S. if you need more information on applets post a question here
 
0
 

Author Comment

by:robjay
ID: 1227433
This looks like the right direction - but I definately need to get this to the web browser -
(you can look at my attempt at http://www.clearsail.net/~robjay/Graph3Applet.java)
a gif would be OK, but presentation in an applet would be better - can you explain a little
more how I can do this in an applet or my best alternative?
Thanks

0
 

Author Comment

by:robjay
ID: 1227434
As I have looked at this a little more, could you write the exact code that you would use to replace this line, and say yourfile is out there at www.somewhere.com/data.txt :

FileInputStream is = new FileInputStream(name);

As I understand this, as long as my file is accessible via the internet, I can download the contents?


0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227435
haven't tested it, but

URL url = new URL("www.somewhere.com/data.txt");
InputStream = URL.getStream();

should work. Your browser sends HTTP reuqest to the web-server at www.somewhere.com, handles the response and gives you only the body of the message (which is your data.txt file)
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Suggested Courses

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question