We help IT Professionals succeed at work.
Get Started

java.lang.NoSuchMethodError: main Error occuring when trying to execute some code

k1ngp1n99
k1ngp1n99 asked
on
622 Views
Last Modified: 2007-12-19
// i cant get this program to execute on my computer. Other programs compile without a problem. I have the same problem with another important program.


import java.applet.*;
import java.awt.*;

public class Viewport extends Applet {
    Polygon poly;
    Rectangle viewport = new Rectangle(50, 50, 100, 100);
    EdgeTable et;
    Graphics gr;
    boolean motion = false;
    Point prev;

    static final int LEFT = 0;
    static final int RIGHT = 1;
    static final int TOP = 2;
    static final int BOTTOM = 3;

    public void init() {
      setBackground(Color.white);
      gr = getGraphics();

      poly = new Polygon();

      String str;

      if ((str = getParameter("viewport_x")) != null) {
          viewport.x = Integer.parseInt(str);
      }
      if ((str = getParameter("viewport_y")) != null) {
          viewport.y = Integer.parseInt(str);
      }
      if ((str = getParameter("viewport_width")) != null) {
          viewport.width = Integer.parseInt(str);
      }
      if ((str = getParameter("viewport_height")) != null) {
          viewport.height = Integer.parseInt(str);
      }
    }
   
    public void start() {
        poly = new Polygon();
      et = null;
    }

    /**
     * Clips polygon by viewport.
     */
    void clipping() {
      clipByLine(viewport.x, LEFT);
      clipByLine(viewport.x+viewport.width, RIGHT);
      clipByLine(viewport.y, TOP);
      clipByLine(viewport.y+viewport.height, BOTTOM);
    }

    /**
     * Clips polygon by virtical or horizontal line.
     */
    void clipByLine(int val, int edge) {
      Polygon newPoly = new Polygon();
      boolean inside;
      Point cross;

      inside = insideArea(poly.xpoints[0], poly.ypoints[0], val, edge);

      for (int i = 1; i < poly.npoints; i++) {
          if (insideArea(poly.xpoints[i], poly.ypoints[i], val, edge)) {
            if (inside) {
                newPoly.addPoint(poly.xpoints[i], poly.ypoints[i]);
            } else {
                cross = getCrossing(poly.xpoints[i-1], poly.ypoints[i-1],
                              poly.xpoints[i], poly.ypoints[i],
                              val, edge);
                newPoly.addPoint(cross.x, cross.y);
                newPoly.addPoint(poly.xpoints[i], poly.ypoints[i]);
            }
            inside = true;
          } else {
            if (inside) {
                cross = getCrossing(poly.xpoints[i-1], poly.ypoints[i-1],
                              poly.xpoints[i], poly.ypoints[i],
                              val, edge);
                newPoly.addPoint(cross.x, cross.y);
            }
            inside = false;
          }
      }

      // Makes a polygon be a circuit
      newPoly.addPoint(newPoly.xpoints[0], newPoly.ypoints[0]);

      poly = newPoly;
    }

    /**
     * Determines whether point (x,y) is inside area.
     * Expresses a clipping line with val and edge.
     */
    boolean insideArea(int x, int y, int val, int edge) {
      switch (edge) {
      case LEFT:
          if (x >= val) {
            return true;
          }
          break;
      case RIGHT:
          if (x <= val) {
            return true;
          }
          break;
      case TOP:
          if (y >= val) {
            return true;
          }
          break;
      case BOTTOM:
          if (y <= val) {
            return true;
          }
          break;
      }

      return false;
    }

    /**
     * Gets crossing point between a line of polygon and a clipping line.
     */
    Point getCrossing(int x1, int y1, int x2, int y2, int val, int edge) {
      Point cross = new Point(0, 0);

      switch (edge) {
      case LEFT:
      case RIGHT:
          cross.x = val;
          cross.y = ((y1-y2)*val+(x1*y2-x2*y1))/(x1-x2);
          break;
      case TOP:
      case BOTTOM:
          cross.x = ((x1-x2)*val-(x1*y2-x2*y1))/(y1-y2);
          cross.y = val;
          break;
      }
      
      return cross;
    }

    /**
     * Paint viewport frame and clipped polygon.
     */
    public void paint(Graphics g) {
      g.drawRect(viewport.x, viewport.y, viewport.width, viewport.height);
      g.drawPolygon(poly);
      if (et != null) {
          et.paint(g);
      }
    }

    public boolean mouseDown(Event ev, int x, int y) {
      // Do Clipping when push right button (or META + left button)
      if (ev.metaDown()) {
          motion = false;
          poly.addPoint(poly.xpoints[0], poly.ypoints[0]);
          clipping();
          et = new EdgeTable(poly);
          repaint();
      } else {
          motion = true;
          poly.addPoint(x, y);
          
          if (poly.npoints >= 2) {
            gr.drawLine(poly.xpoints[poly.npoints-2],
                      poly.ypoints[poly.npoints-2], x, y);
          }
      }

      return true;
    }
   
    public boolean mouseMove(Event ev, int x, int y) {
      if (motion) {
          if (poly.npoints >= 1) {
            gr.setXORMode(Color.white);
            gr.drawLine(poly.xpoints[poly.npoints-1],
                      poly.ypoints[poly.npoints-1], prev.x, prev.y);
            gr.drawLine(poly.xpoints[poly.npoints-1],
                      poly.ypoints[poly.npoints-1], x, y);
            gr.setPaintMode();
          }
      }
      prev = new Point(x, y);

      return true;
    }

    /**
     * Info.
     */
    public String getAppletInfo() {
      return "Disignation of a viewport, clipping of a polygon, and painting";
    }

    /**
     * Parameter Info.
     */
    public String[][] getParameterInfo() {
      String[][] info = {
          {"viewport_x",      "integer", "x of the viewport"},
          {"viewport_y",      "integer", "y of the viewport"},
          {"viewport_width",  "integer", "width of the viewport"},
          {"viewport_height", "integer", "height of the viewport"},
      };
      
      return info;
    }
}

/**
 * Edge table for y-sort.
 */
class EdgeTable {
    int xmin[], ymin[], ymax[];
    double invM[];
    int nsize = 0;

    static final int MIN = 0;
    static final int MID = 1;
    static final int MAX = 2;

    EdgeTable(Polygon poly) {
      xmin = new int[poly.npoints-1];
      ymin = new int[poly.npoints-1];
      ymax = new int[poly.npoints-1];
      invM = new double[poly.npoints-1];
      
      for (int i = 0; i < poly.npoints-1; i++) {
          if (poly.ypoints[i] < poly.ypoints[i+1]) {
            xmin[nsize] = poly.xpoints[i];
            ymin[nsize] = poly.ypoints[i];
            ymax[nsize] = poly.ypoints[i+1];
          } else {
            xmin[nsize] = poly.xpoints[i+1];
            ymin[nsize] = poly.ypoints[i+1];
            ymax[nsize] = poly.ypoints[i];
          }
          
          if (poly.ypoints[i] != poly.ypoints[i+1]) {
            invM[nsize] = (double)(poly.xpoints[i]-poly.xpoints[i+1])/
                          (poly.ypoints[i]-poly.ypoints[i+1]);
            nsize++;
          }
      }

      ySort();
    }

    /**
     * Y-sort
     */
    void ySort() {
      for (int i = 0; i < nsize; i++) {
          for (int j = 0; j < nsize-i-1; j++) {
            if (ymin[j] > ymin[j+1]) {
                swap(j, j+1);
            }
          }
      }
    }

    /**
     * Swaps table contents.
     */
    void swap(int i, int j) {
      int temp;
      double tempd;

      temp = xmin[i];
      xmin[i] = xmin[j];
      xmin[j] = temp;

      temp = ymin[i];
      ymin[i] = ymin[j];
      ymin[j] = temp;

      temp = ymax[i];
      ymax[i] = ymax[j];
      ymax[j] = temp;

      tempd = invM[i];
      invM[i] = invM[j];
      invM[j] = tempd;
    }

    /**
     * Paint clipped polygon.
     */
    public void paint(Graphics g) {
      double xpoints[] = new double[nsize];
      int vertex[] = new int[nsize];
      double x[] = new double[nsize];

      for (int i = 0; i < nsize; i++) {
          x[i] = xmin[i];
      }

      for (int scanline = ymin[0]; ; scanline++) {
          int npoints = 0;

          for (int i = 0; i < nsize; i++) {
            if (scanline > ymin[i] && scanline < ymax[i]) {
                if (insert(xpoints, vertex, x[i], MID, npoints)) {
                  npoints++;
                }
                x[i] += invM[i];
            } else if (scanline == ymin[i]) {
                if (insert(xpoints, vertex, x[i], MIN, npoints)) {
                  npoints++;
                }
                x[i] += invM[i];
            } else if (scanline == ymax[i]) {
                if (insert(xpoints, vertex, x[i], MAX, npoints)) {
                  npoints++;
                }
                x[i] += invM[i];
            }
          }
          
          if (npoints == 0) {
            break;
          }
      
          for (int i = 0; i < npoints; i += 2) {
            g.drawLine((int)xpoints[i], scanline,
                     (int)xpoints[i+1], scanline);
          }
      }

      xpoints = null;
    }

    /**
     * Inserts xmin in xpoints[] and attr (MAX,MID,MIN) in vertex[].
     * Return true if success an insertion.
     */
    boolean insert(double xpoints[], int vertex[], double xmin, int attr,
               int npoints) {
      int i = 0;
      double EPS = 0.00001;

      while (i < npoints) {
          if (Math.abs(xpoints[i] - xmin) < EPS) {
            if ((vertex[i] == MAX && attr == MIN) ||
                (vertex[i] == MIN && attr == MAX)) {
                return false;
            }
          }
          if (xpoints[i] >= xmin) {
            for (int j = npoints-1; j >= i; j--) {
                xpoints[j+1] = xpoints[j];
                vertex[j+1] = vertex[j];
            }
            break;
          }
          i++;
      }

      xpoints[i] = xmin;
      vertex[i] = attr;
      
      return true;
    }
}
Comment
Watch Question
CERTIFIED EXPERT
Distinguished Expert 2020
Commented:
This problem has been solved!
Unlock 1 Answer and 15 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE