Solved

FileReader info -> drawLine in graph

Posted on 1998-11-14
4
362 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

717 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