repaint(...): int / double, NullPointerException

Hi,

I would like to use the repaint method to repaint just a certain region. My coordinates are in Point2D:

private Point2D playerCurrentPosition, playerLastPosition;

I tried the following:

repaint((int)playerLastPosition.getX(), (int)playerLastPosition.getY(), (int)playerCurrentPosition.getX(), (int)playerCurrentPosition.getY());

This gives me an "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException".

How can I solve this problem?

[660 byte] By [SFLa] at [2007-11-26 22:35:44]
# 1
By making sure that "playerLastPosition" is not null.
itchyscratchya at 2007-7-10 11:45:15 > top of Java-index,Desktop,Core GUI APIs...
# 2
As well as "playerCurrentPosition".
ignignokt84a at 2007-7-10 11:45:15 > top of Java-index,Desktop,Core GUI APIs...
# 3
Ah. Yes :o)
itchyscratchya at 2007-7-10 11:45:15 > top of Java-index,Desktop,Core GUI APIs...
# 4

My code ist getting too complex and I am getting too tired ;)

By the way... I am also painting some shapes using GeneralPath... I am using AntiAliasing to draw them. At every redraw these shapes are getting more staggered:

public class Shapes {

private GeneralPath path = new GeneralPath();

public Shape drawBird() {

path.moveTo(50, 50);

path.lineTo(70, 44);

path.curveTo(100, 10, 140, 80, 160, 80);

path.lineTo(190, 40);

path.lineTo(200, 56);

path.quadTo(100, 150, 70, 60);

path.closePath();

return path;

}

(...)

}

public class PMgmt extends JComponent {

private static final long serialVersionUID = 1;

private Shapes myShapes = new Shapes();

(...)

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

g2.setColor(Color.BLACK);

g2.draw(myShapes.drawBird());

(...)

}

}

How can I avoid this? Thanks!

SFLa at 2007-7-10 11:45:15 > top of Java-index,Desktop,Core GUI APIs...
# 5
You are welcome for the help you received on your original question.
camickra at 2007-7-10 11:45:15 > top of Java-index,Desktop,Core GUI APIs...
# 6
Every time you draw that bird you're adding more points to the path (on top of the old ones). You only need to do all the lineTo() stuff etc once. Then just paint it.
itchyscratchya at 2007-7-10 11:45:15 > top of Java-index,Desktop,Core GUI APIs...
# 7

> Every time you draw that bird you're adding more

> points to the path (on top of the old ones).

Oh oh, didn't know that I am doing this... if I don't paint it every time in my paintComponent method it will just be painted one time and will be invisible after a new repaint(). And if I do a repaint and draw it again then it's staggered... What to do?

SFLa at 2007-7-10 11:45:15 > top of Java-index,Desktop,Core GUI APIs...
# 8
Well obviously you need to paint it each time. You just don't need to manipulate it each time. I did say only call lineTo() etc once, I didn't say only call draw() once.
itchyscratchya at 2007-7-10 11:45:15 > top of Java-index,Desktop,Core GUI APIs...
# 9
> I did say> only call lineTo() etc once, I didn't say only call> draw() once.Hmmm... a newbie problem I guess... I am declaring my shape one time in my Shapes-class. In my class PMgmt I use a constructor to paint my shape... How can I call my shape just
SFLa at 2007-7-10 11:45:15 > top of Java-index,Desktop,Core GUI APIs...
# 10

public class Shapes {

private GeneralPath path = new GeneralPath();

public Shapes () {

path.moveTo(50, 50);

path.lineTo(70, 44);

path.curveTo(100, 10, 140, 80, 160, 80);

path.lineTo(190, 40);

path.lineTo(200, 56);

path.quadTo(100, 150, 70, 60);

path.closePath();

return path;

}

public Shape getBird()

{

return this.path;

}

(...)

}

public class PMgmt extends JComponent {

private static final long serialVersionUID = 1;

private Shapes myShapes = new Shapes();

(...)

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

g2.setColor(Color.BLACK);

g2.draw(myShapes.getBird());

(...)

}

}

itchyscratchya at 2007-7-10 11:45:15 > top of Java-index,Desktop,Core GUI APIs...
# 11

itchyscratchy, thanks a lot! :) However... what should I do if I have more shapes... e.g.

public class Shapes {

private GeneralPath path = new GeneralPath();

public Shape drawBird() {

path.moveTo(50, 50);

path.lineTo(70, 44);

path.curveTo(100, 10, 140, 80, 160, 80);

path.lineTo(190, 40);

path.lineTo(200, 56);

path.quadTo(100, 150, 70, 60);

path.closePath();

return path;

}

public Shape drawSomethingElse() {

path.moveTo(200, 200);

path.curveTo(200, 200, 300, 150, 350, 200);

path.closePath();

return path;

}

(...)

}

SFLa at 2007-7-10 11:45:15 > top of Java-index,Desktop,Core GUI APIs...
# 12

Store a different Shape object for each shape.

Let's eliminate a confusion here. The lineTo()/curveTo()/moveTo() methods do not draw anything, they manipulate the path. You should not name your "drawBird()" etc methods as such because they do not draw anything. They only manipulate the path: something which you do not want to do, because once it has been defined there is no need to define it again. So, you store your shapes, and provide getters for them. eg

public class MyShapes

{

private final Shape bird = createBird();

private final Shape fish = createFish();

public final Shape getBird()

{

return this.bird;

}

private final Shape createBird()

{

GeneralPath p = new GeneralPath();

// all the lineTo() etc here

return p;

}

// Similar code for the fish, etc

}

And to use the shapes,

g.draw(myShapes.getBird());

itchyscratchya at 2007-7-10 11:45:15 > top of Java-index,Desktop,Core GUI APIs...
# 13
Opened new thread: http://forum.java.sun.com/thread.jspa?threadID=5165395Message was edited by: SFL
SFLa at 2007-7-10 11:45:15 > top of Java-index,Desktop,Core GUI APIs...