Solved

FileReader info -> drawLine in graph

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Core Java. What output will be and why ? 6 66
Strange loading of website behaviour 3 32
Java Options in Zimbra Server 2 47
MySqlDump not dumping triggers 1 19
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 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…
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…

861 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

25 Experts available now in Live!

Get 1:1 Help Now