SHARE
TWEET

Untitled

a guest Jul 23rd, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.Graphics;
  2. import java.awt.Point;
  3.  
  4. public class Snake implements Entity {
  5.     Point location;
  6.     Segment[] segments;
  7.     int segmentCount = 0;
  8.  
  9.     public Snake(Point location) {
  10.         this.location = location;
  11.         segments = new Segment[25];
  12.     }
  13.  
  14.     public void increaseSegmentCount() {
  15.  
  16.         segments[segmentCount] = new Segment(new Point(location.x + (Segment.getSize().width * segments.length),
  17.                 location.y + (Segment.getSize().height * segments.length)));
  18.  
  19.         segmentCount++;
  20.  
  21.     }
  22.  
  23.     public Segment[] getSegments() {
  24.         return segments;
  25.     }
  26.  
  27.     public int getSegmentCount() {
  28.         return segmentCount;
  29.     }
  30.  
  31.     @Override
  32.     public void paint(Graphics g) {
  33.         for (Segment segment : segments) {
  34.             if (segment != null)
  35.                 segment.paint(g);
  36.         }
  37.     }
  38. }
  39.      
  40. @Override
  41.     public void paint(Graphics g) {
  42.         for (Segment segment : segments) {
  43.             if (segment != null)
  44.                 System.out.println("Called")
  45.                 segment.paint(g);
  46.         }
  47.     }
  48.      
  49. import java.awt.Color;
  50. import java.awt.Dimension;
  51. import java.awt.Graphics;
  52. import java.awt.Point;
  53.  
  54. public class Segment {
  55.     Point location;
  56.  
  57.     public Segment(Point location) {
  58.         this.location = location;
  59.     }
  60.  
  61.     public static Dimension getSize() {
  62.         return new Dimension(20, 20);
  63.     }
  64.  
  65.     public void paint(Graphics g) {
  66.         g.setColor(Color.WHITE);
  67.         g.fillRect(location.x, location.y, 20, 20);
  68.     }
  69. }
  70.      
  71. @Override
  72.     public void paintComponent(Graphics g) {
  73.         super.paintComponent(g);
  74.         for (Entity entity : model.getEntities()) {
  75.             entity.paint(g);
  76.         }
  77.     }
  78.      
  79. Called
  80. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  81.     at Snake.paint(Snake.java:61)
  82.     at View.paintComponent(View.java:50)
  83.     at javax.swing.JComponent.paint(Unknown Source)
  84.     at javax.swing.JComponent.paintToOffscreen(Unknown Source)
  85.     at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
  86.     at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
  87.     at javax.swing.RepaintManager.paint(Unknown Source)
  88.     at javax.swing.JComponent._paintImmediately(Unknown Source)
  89.     at javax.swing.JComponent.paintImmediately(Unknown Source)
  90.     at javax.swing.RepaintManager$4.run(Unknown Source)
  91.     at javax.swing.RepaintManager$4.run(Unknown Source)
  92.     at java.security.AccessController.doPrivileged(Native Method)
  93.     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
  94.     at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
  95.     at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
  96.     at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
  97.     at javax.swing.RepaintManager.access$1200(Unknown Source)
  98.     at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
  99.     at java.awt.event.InvocationEvent.dispatch(Unknown Source)
  100.     at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
  101.     at java.awt.EventQueue.access$500(Unknown Source)
  102.     at java.awt.EventQueue$3.run(Unknown Source)
  103.     at java.awt.EventQueue$3.run(Unknown Source)
  104.     at java.security.AccessController.doPrivileged(Native Method)
  105.     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
  106.     at java.awt.EventQueue.dispatchEvent(Unknown Source)
  107.     at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
  108.     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
  109.     at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
  110.     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  111.     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  112.     at java.awt.EventDispatchThread.run(Unknown Source)
  113.      
  114. if (segment != null) {
  115.             System.out.println("Called")
  116.             segment.paint(g);
  117. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top