Results 1 to 7 of 7

Thread: Polygon array?

  1. #1
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default Polygon array?

    Java Code:
    private void setPolygon() {
    		int x1, y1, x2, y2;
    		dotPosition = new Polygon[strings][frets];
    		for(int x = 0; x < strings; x++) {
    			for(int y = 0; y < frets; y++) {
    				x1 = x * stringSpace + 40;
    				x2 = x * stringSpace + 60;
    				y1 = y * fretSpace + 50;
    				y2 = y * fretSpace + 70;
    				
    				dotPosition[x][y].addPoint(x1, y1);
    				dotPosition[x][y].addPoint(x1, y2);
    				dotPosition[x][y].addPoint(x2, y1);
    				dotPosition[x][y].addPoint(x2, y2);
    			}
    		}
    
    	} // End setPolygon
    I am trying to add new polygon objects to an array. What am I missing here?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What sort of errors are you getting? A NPE (that's my guess)? I don't see anywhere where you create an actual Polygon object.

  3. #3
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    at the top of the class I have

    Java Code:
    private Polygon[][] dotPosition;

    --> incompatible types
    Java Code:
    dotPosition = new Polygon[strings][frets];

    --> array required, but java.awt.Polygon found.
    Java Code:
    dotPosition[x][y].addPoint(x1, y1);
    dotPosition[x][y].addPoint(x1, y2);
    dotPosition[x][y].addPoint(x2, y1);
    dotPosition[x][y].addPoint(x2, y2);

  4. #4
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    I figured out the error about was from improper declaration of the Polygon[][]. Im not sure what it did different but i deleted and retyped out the Polygon[][] declaration.

    But now comes the main question that I had first (lost track of it for some reason).

    Java Code:
    disp.fillPolygon(dotPosition[1][1]);

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at GraphicsDisplay.paintComponent(GraphicsDisplay.jav a:37)
    at javax.swing.JComponent.paint(JComponent.java:1029)
    at javax.swing.JComponent.paintChildren(JComponent.ja va:862)
    at javax.swing.JComponent.paint(JComponent.java:1038)
    at javax.swing.JComponent.paintChildren(JComponent.ja va:862)
    at javax.swing.JComponent.paint(JComponent.java:1038)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:5 67)
    at javax.swing.JComponent.paintChildren(JComponent.ja va:862)
    at javax.swing.JComponent.paintToOffscreen(JComponent .java:5131)
    at javax.swing.RepaintManager$PaintManager.paintDoubl eBuffered(RepaintManager.java:1479)
    at javax.swing.RepaintManager$PaintManager.paint(Repa intManager.java:1410)
    at javax.swing.RepaintManager.paint(RepaintManager.ja va:1224)
    at javax.swing.JComponent.paint(JComponent.java:1015)
    at java.awt.GraphicsCallback$PaintCallback.run(Graphi csCallback.java:21)
    at sun.awt.SunGraphicsCallback.runOneComponent(SunGra phicsCallback.java:60)
    at sun.awt.SunGraphicsCallback.runComponents(SunGraph icsCallback.java:97)
    at java.awt.Container.paint(Container.java:1780)
    at java.awt.Window.paint(Window.java:3375)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:796)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:713)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(Re paintManager.java:693)
    at javax.swing.SystemEventQueueUtilities$ComponentWor kRequest.run(SystemEventQueueUtilities.java:125)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    Last edited by AcousticBruce; 02-05-2011 at 03:04 AM.

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Fubarable hit the nail on the head...you can allocate an array of objects any size, but all its elements will be null until they are instantiated.

  6. #6
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    Hmm... I thought I did instantiate correctly, how can I do this?


    //My purpose is to draw a bunch of shapes that respond to a mouse click only within those pixels.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by AcousticBruce View Post
    Hmm... I thought I did instantiate correctly, how can I do this?


    //My purpose is to draw a bunch of shapes that respond to a mouse click only within those pixels.
    When you create an array of objects, it's like you're creating an empty parking lot. You can't start painting cars or driving cars until you put cars into the lot. Same for an Array of Polygon. You've created a structure that will hold a bunch of Polygons, but you've got to somehow somewhere create new Polygons to populate this array. Often this is done in a for loop, or for a 2-d array, in a nested for loop.

Similar Threads

  1. Drag polygon
    By cassysumandak in forum New To Java
    Replies: 5
    Last Post: 03-10-2011, 07:30 PM
  2. Polygon won't show up
    By pizzadude223 in forum Java 2D
    Replies: 5
    Last Post: 08-09-2010, 04:51 AM
  3. How to use Polygon class?
    By lost_soul in forum New To Java
    Replies: 8
    Last Post: 04-25-2010, 01:48 AM
  4. How to copy a polygon
    By cassysumandak in forum New To Java
    Replies: 6
    Last Post: 10-07-2009, 01:37 AM
  5. How to Draw a Polygon in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 12:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •