Solved

Draw a polygon with two data files given.

Posted on 1998-08-11
7
452 Views
Last Modified: 2011-09-20
I'm trying to draw a polygon in a box with two data files given. The first file is the x-dimension and the second y-dimension respectively. Later this application will be added into a html-page.

The version is included. So could anyone help me to get this base fixed, or solve this problem another way ? Thanks !
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
/* The aim of this applet is to draw a polygon and it will be used
on a html-page. This applet should get two files of data and the size
as parameters.




import java.awt.Graphics;
import java.applet.Applet;

public class Polynom extends Applet {
   
       
      
        public void paint(Graphics g) {
        
              int kehysXraja;
              int kehysYraja;
          int xSkaala;
          int ySkaala;
          
              kehysXraja = 400;
              kehysYraja = 400;
          xSkaala = 1;
              ySkaala =1;
                
              g.drawRect(0,0,kehysXraja,kehysYraja);
              
              // this part draws me a nice box of the given size                
              }
                      
     
     String nimi;
     public void init() {
                
             try {             
             xName = getParameter("Xnumbers");    
             yName = getParameter("Ynumbers");
             Size = getParameter("Size");
                }
           
             catch (Exception e) {
                    System.err.println("No file found."+e);
                       e.printStackTrace();                   }
                                                                 
             }
   
   
       //
       // In this part I should get the names of files needed.  
       // The amount of numbers is given as 'size' and it's assumed that the files
       // contain the same amount of numbers  
                      
          
          
          public synchronized void readDataFiles() {
       
                                       
               StreamTokenizer stX = new StreamTokenizer(new FileInputStream("xNumbers"));
                               StreamTokenizer stY = new StreamTokenizer(new FileInputStream("yNumbers"));
       double nextXnumber;
       double nextYnumber;
       int tokenXtype = stX.nextToken();
       int tokenYtype = stY.nextToken();
       double Xnumber[];
       double Ynumber[];
       
       while (tokenXtype != StreamTokenizer.TT_EOF)
       {
         if (tokenXtype == StreamTokenizer.TT_NUMBER)
             {
               nextXnumber = stX.nval;
                     
               tokenXtype = stX.nextToken();
           
            for (int i=0;i<size;i++)
                    {
               
               Xnumber[i] = newDouble(stX.nextToken[]).doubleValue();
           
                }
            }
            else
                {
                 System.out.println("Invalid token " + stX.sval);
                 };


       while (tokenYtype != StreamTokenizer.TT_EOF)
         {      
           if (tokenYtype == StreamTokenizer.TT_NUMBER)
              {
               nextYnumber = stY.nval;
               tokenYtype = stY.nextToken();
           
               for(j=0;j<size;j++)
                     {
                 Ynumber[k] = newDouble(stY.nextToken[]).DoubleValue();
                }
         }
         else
             {
               System.out.println("Invalid token " + stY.sval);
           System.out.println(Invalid token" + stY.sval);
         }

       // in this part the xNumbers file was read as x-coordinates
       // and the yNumbers file as y-coordinates
       // and put into a one dimensional matrix
       
       
       
   }
}

          
           for(k=0;k<koko;k++)
                  {                    
          
           g.drawLine(Xnumber[i] , (kehysYraja - Ynumber[j]),
                                 Xnumber[i+1], (kehysYraja - Ynumber[j+1]); }
               
               // here I'm drawing so that xNumbers mark the horizontal
               // and yNumbers the vertical dimension
              }
              
      }

0
Comment
Question by:kojonen
  • 4
  • 3
7 Comments
 

Author Comment

by:kojonen
ID: 1232336
Edited text of question
0
 

Author Comment

by:kojonen
ID: 1232337
Edited text of question
0
 

Accepted Solution

by:
awilkins earned 50 total points
ID: 1232338
/* Here is the altered text for your program.  At a few spots I wasn't sure exactly what you wanted, so I made my best guess. If this is not correct, please tell me. */

import java.applet.Applet;
import java.awt.Graphics;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.StreamTokenizer;
import java.net.URL;

public class Polynom extends Applet {
       
  String xName, yName, nimi;
  Graphics graphics;
  int kehysXraja, kehysYraja, xSkaala, ySkaala;
  int size;
  double[] Xnumber, Ynumber;
  int tokenXtype, tokenYtype;
  StreamTokenizer stX, stY;

  public void paint(Graphics g) {
    graphics = g;
       
    kehysXraja = 400;
    kehysYraja = 400;
    xSkaala = 1;
    ySkaala =1;
         
    g.drawRect(0,0,kehysXraja,kehysYraja);
       
    // this part draws me a nice box of the given size      
  }

  public void init() {
               
    try {        
      xName = getParameter("Xnumbers");      
      yName = getParameter("Ynumbers");
      size = Integer.parseInt(getParameter("Size"));
    }
    catch (Exception e) {
      System.err.println("No file found."+e);
      System.err.println(e.toString());
    }
  }

  /* In this part I should get the names of files needed.  
   * The amount of numbers is given as 'size' and it's assumed that the files
   * contain the same amount of numbers    */
         
         
         
  public synchronized void readDataFiles() {

    double nextXnumber, nextYnumber;
    try {   // This whole mess just creates the StreamTokenizer in a non-deprecated manner

      stX = new StreamTokenizer(new InputStreamReader(new URL(getDocumentBase(), xName).openConnection().getInputStream()));
      stY = new StreamTokenizer(new InputStreamReader(new URL(getDocumentBase(), yName).openConnection().getInputStream()));

      tokenXtype = stX.nextToken();
      tokenYtype = stY.nextToken();

    } catch (IOException e) {System.err.println(e.toString());}
             
    while (tokenXtype != StreamTokenizer.TT_EOF)
    {
      if (tokenXtype == StreamTokenizer.TT_NUMBER)
      {
        nextXnumber = stX.nval;

        try {
          tokenXtype = stX.nextToken();
             
              for (int i = 0; i < size; i++)
                Xnumber[i] = new Double(stX.nextToken()).doubleValue();

        } catch (IOException e) { System.err.println(e.toString()); }
      }
      else
        System.out.println("Invalid token " + stX.sval);
    }

    while (tokenYtype != StreamTokenizer.TT_EOF)
    {        
      if (tokenYtype == StreamTokenizer.TT_NUMBER)
      {
        nextYnumber = stY.nval;
                        try {
          tokenYtype = stY.nextToken();

          for(int j=0;j<size;j++)
            Ynumber[j] = new Double(stY.nextToken()).doubleValue();

        } catch (IOException e) { System.err.println(e.toString()); }
       }
       else
         System.out.println("Invalid token " + stY.sval);

       // in this part the xNumbers file was read as x-coordinates
       // and the yNumbers file as y-coordinates
       // and put into a one dimensional matrix
    }

    for(int k=0;k<size;k++)
    graphics.drawLine((int) Xnumber[k] ,(int) (kehysYraja - Ynumber[k]), (int) Xnumber[k+1],
                  (int) (kehysYraja - Ynumber[k+1]));
       
    // here I'm drawing so that xNumbers mark the horizontal
    // and yNumbers the vertical dimension
  }

}
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:kojonen
ID: 1232339
Great !  Best thanks awilkins !

Still got small problems because the files sholdn't be read from URL-address but from
two files of my kawa-directory. I tried to make changes but still got something wrong...

Could you still help a little bit to get this right, please ?

Here's the revisited code:


    import java.applet.Applet;
    import java.awt.Graphics;
    import java.io.InputStreamReader;
    import java.io.IOException;
    import java.io.StreamTokenizer;
    import java.net.URL;
    import java.io.*;
    import java.util.*;
     
    public class PolyExp1 extends Applet {
             
      String xName, yName, nimi;
      Graphics graphics;
      int kehysXraja, kehysYraja, xSkaala, ySkaala;
      int size;
      double[] Xnumber, Ynumber;
      int tokenXtype, tokenYtype;
      StreamTokenizer stX, stY;

      public void paint(Graphics g) {
        graphics = g;
           
        kehysXraja = 400;
        kehysYraja = 400;
        xSkaala = 1;
        ySkaala =1;
             
        g.drawRect(0,0,kehysXraja,kehysYraja);
           
        // this part draws me a nice box of the given size        
      }

      public void init() {
                     
        try {          
          xName = getParameter("Xnumbers");      
          yName = getParameter("Ynumbers");
          size = Integer.parseInt(getParameter("Size"));
        }
        catch (Exception e) {
          System.err.println("No file found."+e);
          System.err.println(e.toString());
        }
      }

      /* In this part I should get the names of files needed.    
       * The amount of numbers is given as 'size' and it's assumed that the files
       * contain the same amount of numbers    */
             
             
             
      public synchronized void readDataFiles() {

        double nextXnumber, nextYnumber;
        try {  
       
         // This whole mess just creates the StreamTokenizer in a non-deprecated manner


/*
    These part is taken away because no URL-address is at time available


          stX = new StreamTokenizer(new InputStreamReader(new URL(getDocumentBase(),
    xName).openConnection().getInputStream()));
 
          stY = new StreamTokenizer(new InputStreamReader(new URL(getDocumentBase(),
    yName).openConnection().getInputStream()));    
         
*/


        stX = new StreamTokenizer(new FileInputStream("xName"));
        stY = new StreamTokenizer(new FileInputStream("yName"));
      
         // Here the two files are read. Not from URL-address but from
         // my kawa-directory which includes the two number files.
         // This is done for test purposes only.    



          tokenXtype = stX.nextToken();
          tokenYtype = stY.nextToken();

        } catch (IOException e) {System.err.println(e.toString());}
                 
        while (tokenXtype != StreamTokenizer.TT_EOF)
        {
          if (tokenXtype == StreamTokenizer.TT_NUMBER)
          {
            nextXnumber = stX.nval;

            try {
              tokenXtype = stX.nextToken();
                   
            for (int i = 0; i < size; i++)
               Xnumber[i] = new Double(stX.nextToken()).doubleValue();

            } catch (IOException e) { System.err.println(e.toString()); }
          }
          else
            System.out.println("Invalid token " + stX.sval);
        }

        while (tokenYtype != StreamTokenizer.TT_EOF)
        {        
          if (tokenYtype == StreamTokenizer.TT_NUMBER)
          {
            nextYnumber = stY.nval;
    try {
              tokenYtype = stY.nextToken();

              for(int j=0;j<size;j++)
                Ynumber[j] = new Double(stY.nextToken()).doubleValue();

            } catch (IOException e) { System.err.println(e.toString()); }
           }
           else
             System.out.println("Invalid token " + stY.sval);

           // in this part the xNumbers file was read as x-coordinates
           // and the yNumbers file as y-coordinates
           // and put into a one dimensional matrix
        }

        for(int k=0;k<size;k++)
        graphics.drawLine((int) Xnumber[k] ,(int) (kehysYraja - Ynumber[k]), (int) Xnumber[k+1],
    (int) (kehysYraja - Ynumber[k+1]));
           
        // here I'm drawing so that xNumbers mark the horizontal
        // and yNumbers the vertical dimension
      }

    }

and here's my html-page:

<HTML>
<HEAD>
<TITLE>PolyExp1</TITLE>
</HEAD>
<BODY>
<H1>PolyExp1</H1>
<HR>
<APPLET CODE="PolyExp1.class" WIDTH=401 HEIGHT=401>
<PARAM name="Xnumbers" value="Xnumerot.txt">
<PARAM name="Ynumbers" value="Ynumerot.txt">
<PARAM name="Size" value=7>
</APPLET>      
<HR>
</BODY>
</HTML>

The named files Xnumerot and Ynumerot are the files containing both 7 numbers to be read.

0
 

Expert Comment

by:awilkins
ID: 1232340
/*
  Here is the revised code -- the main problem was that the method readDataFiles was never called.  Now it is explicitly called from init(), but I moved the code that actually draws the lines into the paint method by the box-drawing code.  I also made some minor changes in the tokenizing structure -- nothing major.
  As to the setting the StreamTokenizers to work from a your hard drive as well as from a URL -- the applet must read from a URL, otherwise it will get a SecurityException when it tries to access your hard drive.  However, the getDocumentBase() method always returns the URL the HTML file is at, even if it resides on your hard drive.  This means that you don't need to know the URL beforehand, and that the xNumbers and yNumbers parameters only need to specify the corresponding files' locations with respect to the HTML file -- if they are in the same directory, you only need to specify their names, as you did in your example HTML file.  
  The version shown below should run correctly.  Just for reference, I used the example HTML file you gave in your other question and data files as shown below:

Xnumerot.txt

100 200 300 400 300 200 100

Ynumerot.txt

200 300 200 100 300 100 0

  If you have any more questions, don't hesitate to ask --
                Alexander Wilkins


*/

import java.applet.Applet;
import java.awt.Graphics;
import java.net.URL;
import java.io.*;
import java.util.*;

public class PolyExp1 extends Applet {

  String xName, yName, nimi;
  Graphics graphics;
  int kehysXraja, kehysYraja, xSkaala, ySkaala, size, tokenXtype, tokenYtype;
  int[] Xnumber, Ynumber;
  StreamTokenizer stX, stY;

  public void paint(Graphics g) {
    graphics = g;

    kehysXraja = 400;
    kehysYraja = 400;
    xSkaala = 1;
    ySkaala = 1;
             
    g.drawRect(0,0,kehysXraja,kehysYraja);
    // this part draws me a nice box of the given size
                         
    for(int k=0;k<size;k++) {
      int l = k + 1;
      if (l >= size)
      l = 0;
      graphics.drawLine(Xnumber[k], (kehysYraja - Ynumber[k]), Xnumber[l], (kehysYraja -
                  Ynumber[l]));
    }
    /* here I'm drawing so that xNumbers mark the horizontal
     * and yNumbers the vertical dimension
     * Line-drawing code moved here with the box-drawing code. */
  }

  public void init() {
                                 
    try {            
      xName = getParameter("Xnumbers");        
      yName = getParameter("Ynumbers");
      size = Integer.parseInt(getParameter("Size"));
    }
    catch (Exception e) {
      System.err.println("No file found."+e);
      System.err.println(e.toString());
    }

    Xnumber = new int [size]; // Xnumber and Ynumber are both arrays of ints now
    Ynumber = new int [size];
    readDataFiles(); // init now calls readDataFiles();
  }

  /* In this part I should get the names of files needed.      
   * The amount of numbers is given as 'size' and it's assumed that the files
   * contain the same amount of numbers    */
                           
                   
                           
  public synchronized void readDataFiles() {

    double nextXnumber, nextYnumber;
    try {    

    // This whole mess just creates the StreamTokenizer in a non-deprecated manner

      stX = new StreamTokenizer(new InputStreamReader(new URL(getDocumentBase(),
            xName).openConnection().getInputStream()));
               
      stY = new StreamTokenizer(new InputStreamReader(new URL(getDocumentBase(),
            yName).openConnection().getInputStream()));      
                     
    tokenXtype = stX.nextToken();
    tokenYtype = stY.nextToken();

  } catch (IOException e) {System.out.println(e.toString());}
                               
  int i = -1;

  while (stX.ttype != StreamTokenizer.TT_EOF)
  {
    i++;
    if (stX.ttype == StreamTokenizer.TT_NUMBER)
    {

      try {
        Xnumber[i] = (int) stX.nval;

        tokenXtype = stX.nextToken();

      } catch (IOException e) { System.err.println(e.toString()); }
    }
    else
      System.out.println("Invalid token " + stX.sval);
  }
  i = -1;
  while (stY.ttype != StreamTokenizer.TT_EOF)
  {
    i++;    
    if (stY.ttype == StreamTokenizer.TT_NUMBER)
      {
        try {
          Ynumber[i] = (int) stY.nval;

          stY.nextToken();

        } catch (IOException e) { System.err.println(e.toString()); }
      }
      else
        System.out.println("Invalid token " + stY.sval);

      // in this part the xNumbers file was read as x-coordinates
      // and the yNumbers file as y-coordinates
      // and put into a one dimensional matrix
    }
  }

0
 

Author Comment

by:kojonen
ID: 1232341
Now it's really working !!!

To be honest I got to admit that my original task was not to draw a polygon but a graph.
I don't know how you call it in english, maybe a polynome or what ? In german they probably say: " Ein Polygonzug" or "Zick Zack".

However, I got this feature done with our polygon by changing the loop of index a little bit, so that the last line is not drawn.

Anyway, I'm still thankful for you help Mr. Wilkins. If you ever need to ask a question conserning mathematical analysis or general thory of expertise, please contact me and maybe I can help you then.

Timo Kojonen
email: timo.kojonen@helsinki.fi

0
 

Expert Comment

by:awilkins
ID: 1232342
No problem -- glad to help.

  Alexander Wilkins
  email: Awilkins@hmc.edu (starting August 30th)
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
micro services spring boot application error 3 187
object oriented programming comparison 5 69
arguments to jar 5 26
What's wrong with this code? 4 22
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 “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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 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