0

I wrote a code for testing Breshenam Algorithm and also for its faster version. The code takes input from user for two points using mouse clicks and then draws lines and displays total number of cycles. Code runs fine but it is giving a stack trace before displaying output:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at CvAssign22.paint(Assign22.java:62) at sun.awt.RepaintArea.paintComponent(RepaintArea.java:264) at sun.awt.RepaintArea.paint(RepaintArea.java:240) at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:358) at java.awt.Component.dispatchEventImpl(Component.java:4967) at java.awt.Component.dispatchEvent(Component.java:4713) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Here is the code:

import java.awt.*;
import java.awt.event.*;



public class Assign22 extends Frame {
    public static void main(String[] args) {
        // TODO code application logic here
        new Assign22();
    }

    Assign22()
   {  super("dfdf");
      addWindowListener(new WindowAdapter()
         {public void windowClosing(WindowEvent e){System.exit(0);}});
      setSize(400, 300);
      add("Center", new CvAssign22());
      setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
      show();
   }
}

class CvAssign22 extends Canvas {
    int xi,yi,x0,y0,pixelSize = 1;
    boolean ready = true;
    int centerX,centerY,np=0;
    Point2D[] p = new Point2D[2];
//   float rWidth = 10.0F, rHeight = 7.5F, eps = rWidth/100F, pixelSize;

    CvAssign22(){
        {  addMouseListener(new MouseAdapter()
      {  public void mousePressed(MouseEvent evt)
         {  int x = evt.getX(), y = evt.getY();
            if (np == 2) np = 0;
            p[np++] = new Point2D(x, y);
            repaint();
         }
      });
   }
    }
    class Point2D
{  int x, y;
   Point2D(int x, int y){this.x = x; this.y = y;}
}


    void putPixel(Graphics g, int x, int y){
        g.drawLine(x, y, x, y);
    }
    public void paint(Graphics g){
        drawLine(g,p[0].x,p[0].y,p[1].x,p[1].y,"faster");
        System.out.println("Running Breshenam Algorithm now:");
        drawLine(g,p[0].x,p[0].y,p[1].x,p[1].y);
    }
    void drawLine(Graphics g, int xP, int yP, int xQ, int yQ,String f){

        int x = xP, y = yP, d = 0, dx = xQ - xP, dy = yQ - yP,
      c, m, xInc = 1, yInc = 1,xmid,x2 = xQ,y2=yQ,cycles=0;
        xmid = (xP+xQ)/2;
        int ymid = (yP+yQ)/2;
   if (dx < 0){xInc = -1; dx = -dx;} 
   if (dy < 0){yInc = -1; dy = -dy;}
   if (dy <= dx)
   {  c = 2 * dx; m = 2 * dy;
      if (xInc < 0) dx++;
      for (;;)
      {  
          putPixel(g, x, y);
      putPixel(g, x2, y2);
      cycles++;
         if (x == xmid) break;
         x += xInc;//increase x
         x2-=xInc;//decrease x2
         d += m;
         if (d >= dx){y += yInc; d -= c;y2-=yInc;}

      }
   }
   else
   {  c = 2 * dy; m = 2 * dx;
      if (yInc < 0) dy++;
      for (;;)
      {  putPixel(g, x, y);
      putPixel(g,x2,y2);
      cycles++;
         if (y == ymid) break;
         y += yInc;
         y2-=yInc;
         d += m;
         if (d >= dy){x += xInc; d -= c;x2-=xInc;}
      }
   }
   System.out.println("Total number of cycles for FasterBres: "+cycles);
//   cycles = 0;
    }

    void drawLine(Graphics g, int xP, int yP, int xQ, int yQ) {
        int x = xP, y = yP, d = 0, dx = xQ - xP, dy = yQ - yP,
      c, m, xInc = 1, yInc = 1,cycles=0;
   if (dx < 0){xInc = -1; dx = -dx;} 
   if (dy < 0){yInc = -1; dy = -dy;}
   if (dy <= dx)
   {  c = 2 * dx; m = 2 * dy;
      if (xInc < 0) dx++;
      for (;;)
      {  putPixel(g, x, y);
      cycles++;
         if (x == xQ) break;
         x += xInc;
         d += m;
         if (d >= dx){y += yInc; d -= c;}
      }
   }
   else
   {  c = 2 * dy; m = 2 * dx;
      if (yInc < 0) dy++;
      for (;;)
      {  putPixel(g, x, y);
      cycles++;
         if (y == yQ) break;
         y += yInc;
         d += m;
         if (d >= dy){x += xInc; d -= c;}
      }
   }
   System.out.println("Total number of cycles for Breshenam: "+cycles);
    } 
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • I am not sure why I am getting this stack trace at all. All things seems just fine to me. –  Jun 26 '16 at 19:05
  • no, nothing is fine. You try to paint a line between points before there are any points set. – luk2302 Jun 26 '16 at 19:05
  • for the record : that's not swing, that's AWT, swing belongs to `javax` package(AWT is old by the way :) ) – niceman Jun 26 '16 at 19:07
  • 1
    Ahh... my bad....traditional rookie mistake from my side! Resolved the error now! Getting outputs as desired without any stack trace! Thanks Mr. @luk2302 –  Jun 26 '16 at 19:08

0 Answers0