Reputation: 95
Sorry for my bad grasp on Java Graphics. So, I have this paint method:
public void paint(Graphics window)
{
window.setColor(Color.WHITE);
window.fillRect(0,0,getWidth(), getHeight());
window.setColor(Color.BLUE);
window.drawRect(20,20,getWidth()-40,getHeight()-40);
window.setFont(new Font("TAHOMA",Font.BOLD,18));
window.drawString("CREATE YOUR OWN SHAPE!",40,40);
}
Any everything works nicely, with the rectangles and the text showing up.
Then I change it to:
public void paint(Graphics window)
{
window.setColor(Color.WHITE);
window.fillRect(0,0,getWidth(), getHeight());
window.setColor(Color.BLUE);
window.drawRect(20,20,getWidth()-40,getHeight()-40);
window.setFont(new Font("TAHOMA",Font.BOLD,18));
window.drawString("CREATE YOUR OWN SHAPE!",40,40);
sh.setX(sh.getX()+sh.getXSpeed());
sh.setY(sh.getY()+sh.getYSpeed());
sh.draw(window);
if(!(sh.getX()>=10 && sh.getX()<=730))
sh.setXSpeed(-sh.getXSpeed());
if(!(sh.getY()>=10 && sh.getY()<=530))
sh.setYSpeed(-sh.getYSpeed());
}
And suddenly nothing shows up. (Not even the text that had shown up before) "sh" is an instance of my Shape class. The methods called are fairly self explanatory (Setters and getters), but included at the end for reference.
Why does this happen? I also get a slew of terminal errors, if it's of any use I'll post them here. I mean, I wouldn't expect setters and getters to cause things to disappear or any significant errors (Note that things still disappear when I remove the "sh.draw(window)" as well). Is there a problem with calling object methods inside the paint method? What could I do instead?
Methods for reference, but not needed to understand the code :
public int getX()
{
return xPos;
}
public int getY()
{
return yPos;
}
public int getXSpeed()
{
return xSpeed;
}
public int getYSpeed()
{
return ySpeed;
}
public void setX(int newX)
{
xPos=newX;
}
public void setY(int newY)
{
yPos=newY;
}
public void setXSpeed(int newX)
{
xSpeed=newX;
}
public void setYSpeed(int newY)
{
ySpeed=newY;
}
public void draw(Graphics window)
{
window.setColor(color);
window.fillRect(xPos, yPos, width, height);
if(color!=Color.BLUE)
window.setColor(Color.BLUE);
else
window.setColor(Color.RED);
int xDev=(width/10);
int yDev=(height/10);
int xDev2=width/8;
int yDev2=height/8;
window.fillOval(xPos+xDev, yPos+yDev, width-yDev*2, height-yDev*2);
window.setColor(Color.BLACK);
window.fillOval(xPos+xDev*2, yPos+yDev*2, width-xDev*4, height-yDev*4);
xPos+=width/2;
yPos+=height*2/3;
int[] treeXPoints = {xPos,xPos-xDev*2,xPos-xDev,xPos+xDev,xPos+xDev*2};
int[] treeYPoints = {yPos,yPos-yDev*2,yPos-yDev*3,yPos-yDev*3,yPos-yDev*2};
Polygon present1 = new Polygon(treeXPoints,treeYPoints,treeXPoints.length);
window.setColor(Color.YELLOW);
window.fillPolygon(present1);
xPos-=width/2;
yPos-=height*2/3;
}
Terminal Errors:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at MovingShapePanel.paint(MovingShapePanel.java:48)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5219)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1529)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1452)
at javax.swing.RepaintManager.paint(RepaintManager.java:1249)
at javax.swing.JComponent._paintImmediately(JComponent.java:5167)
at javax.swing.JComponent.paintImmediately(JComponent.java:4978)
at javax.swing.RepaintManager$3.run(RepaintManager.java:808)
at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)
at javax.swing.RepaintManager.access$1100(RepaintManager.java:62)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1677)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Upvotes: 0
Views: 552
Reputation: 31648
You are getting a NullPointerException
in your paint method line 48.
I would guess your field sh
is not set before you call methods on it
Upvotes: 1