Solved

Draw a polygon with two data files given.

Posted on 1998-08-11
7
447 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
Comment Utility
Edited text of question
0
 

Author Comment

by:kojonen
Comment Utility
Edited text of question
0
 

Accepted Solution

by:
awilkins earned 50 total points
Comment Utility
/* 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:kojonen
Comment Utility
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
Comment Utility
/*
  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
Comment Utility
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
Comment Utility
No problem -- glad to help.

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

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

16 Experts available now in Live!

Get 1:1 Help Now