Solved

FileReader info -> drawLine in graph

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet filter example 37 82
hibernate jars 4 57
Cisco ASA: Java web start no go, asdm launcher no go 3 56
American Express @Work site and Java 4 63
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

685 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