Print

Print


getResource expects that robot.jpg be located *right next* to the  
class file for the Java class on which getClass() is being called.   
Are you sure it's there?  Eclipse moves class files to its own  
repository.

Sean

On Jun 28, 2010, at 11:04 PM, recent:[log in to unmask] wrote:

> Yes I checked the docs/howto.html file about how to draw a background.
> According to this doc, I use the following code:
>
> Display2D display;
> Image i = new  
> ImageIcon(getClass().getResource("robot.jpg")).getImage();
> BufferedImage b =  
> display 
> .getGraphicsConfiguration().createCompatibleImage(i.getWidth(null),  
> i.getHeight(null));
> Graphics g = b.getGraphics();
> g.drawImage(i,0,0,i.getWidth(null),i.getHeight(null),null);
> g.dispose();
> display.setBackdrop(new TexturePaint(b, new  
> Rectangle(0,0,i.getWidth(null),i.getHeight(null))));
>
>
> But Eclipse indicates there are errors:
> Exception in thread "main" java.lang.NullPointerException
> at groupSize.FlockersWithUI.init(FlockersWithUI.java:118)
> at sim.display.Console.<init>(Console.java:981)
> at groupSize.FlockersWithUI.main(FlockersWithUI.java:24)
>
>
> Line 118 is:
> BufferedImage b =  
> display 
> .getGraphicsConfiguration().createCompatibleImage(i.getWidth(null),  
> i.getHeight(null));
>
> I think I have put robot.jpg in the bin folder.
> But I don't know why there is a NullPointerException.
>
> From: Sean Luke <[log in to unmask]>
> To: MASON-INTEREST-L <[log in to unmask]>
> Time: 2010-06-22  17:04:42
> Carbon Copy: <>
> Subject: Re: How to use the code in the app "Flockers"?
> On Jun 20, 2010, at 5:40 PM, recent:[log in to unmask] wrote:
> > 1) In the app "Flockers", how should I revise the code so that I can
> > change the colorful triangle to a png file which is provided by
> > myself?
> No sweat.  Check out sim.portrayal.simple.ImagePortrayal2D.  You'd
> register that portrayal in the ContinuousPortrayal2D instead of the
> Flockers drawing themselves.  If this terminology is confusing, check
> out the MASON tutorials.  See MASON's tutorial 5 in particular.
> > 2) In the app "Flockers", how should I revise the code so that I can
> > change the black background to a jpg file which is provided by  
> myself?
> Also straightforward.  Check out the docs/howto.html file, which has a
> number of items along these lines.
> Sean