We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Eclipse code won't do it
Page Index Toggle Pages: 1
Eclipse code won't do it (Read 496 times)
Eclipse code won't do it
Dec 29th, 2007, 1:09pm
 
I'm trying to use Eclipse IDE, but I'm experiencing troubles with doing nice OOP.
following code in Processing IDE will do it just fine:

(file DrawingImages):

Background bg;
PImage a;

void setup()
{
 size(1000,600);
 bg = new Background("pic.jpg");

 a = loadImage("pic.jpg");
}

void draw()
{
 bg.display();
}

(file Background):

class Background
{
 PImage img;
 int posX, posY;

 public Background(String filename)
 {
   posX = 0;
   posY = 0;
   img = loadImage(filename);
 }

 public void display()
 {
   image(img,posX,posY);
 }
}

now in Eclipse i have to import processing...the first class:

import processing.core.*;

public class Panorama extends PApplet
{

 Background bg;

 PImage a;

 public void setup()
 {
   size(1000,600);
   bg = new Background("pic.jpg");
   a = loadImage("pic.jpg");
 }

 public void draw()
 {
   bg.display();
 }
}

(and the constructor class...)

import processing.core.*;
public class Background extends PApplet
{
 public PImage img;
 private int posX, posY;

 public Background(String filename)
 {
   posX = 0;
   posY = 0;
   img = loadImage(filename);
 }
   public void display()
   {
     image(img,posX,posY);
   }
 }
}

This will not produce a compilation error, but folloing runtime errors:

Error while running applet.
java.lang.NullPointerException

at processing.core.PApplet.image(PApplet.java:7567)

at Background.display(Background.java:24)

at Panorama.draw(Panorama.java:34)

at processing.core.PApplet.handleDisplay(PApplet.java:1463)

at processing.core.PGraphics.requestDisplay(PGraphics.java:690)

at processing.core.PApplet.run(PApplet.java:1560)

at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException

at processing.core.PApplet.image(PApplet.java:7567)

at Background.display(Background.java:24)

at Panorama.draw(Panorama.java:34)

at processing.core.PApplet.handleDisplay(PApplet.java:1463)

at processing.core.PGraphics.requestDisplay(PGraphics.java:690)

at processing.core.PApplet.run(PApplet.java:1560)

at java.lang.Thread.run(Unknown Source)


I don't know what i'm doing wrong... can anyone please help me out? Smiley
Thanks in advance
Re: Eclipse code won't do it
Reply #1 - Dec 29th, 2007, 11:02pm
 
The problem with your seconds class is, that all the processing code needs the main PApplet object to work (I cant explain it cause I'm not that java guru). So your class should look like this:

Code:

import processing.core.*;
public class Background extends PApplet
{
public PImage img;
private int posX, posY;
private PApplet parent;

public Background(String filename, PApplet parent)
{
posX = 0;
posY = 0;
this.parent=parent;
img = parent.loadImage(filename);

}
public void display()
{
parent.image(img,posX,posY);
}
}
}



And in your main class initialize the new object like this:

Code:

bg = new Background("pic.jpg", this);

Re: Eclipse code won't do it
Reply #2 - Dec 30th, 2007, 12:02am
 
This worked perfectly. Thanks! I knew what was the problem, but wasn't clever enough to remember the solution Smiley
Page Index Toggle Pages: 1