Solved

FileReader info -> drawLine in graph

Posted on 1998-11-14
4
323 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
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
heyhey_ earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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_
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now