This site requires JavaScript, please enable it in your browser!
Greenfoot back
RM6442
RM6442 wrote ...

2011/11/16

I need help understanding if(player != null)

RM6442 RM6442

2011/11/16

#
I am working on a project that calls for the removal of the player object, but once the object has been removed the world stops. A few different objects use the player object, so I assumed something like if(player!=null) would work. It didn't. The error looks like this: java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:655) at greenfoot.Actor.getX(Actor.java:157) at NyanGame.createRainbow(NyanGame.java:38) at ForestWorld.act(ForestWorld.java:35) at greenfoot.core.Simulation.actWorld(Simulation.java:513) at greenfoot.core.Simulation.runOneLoop(Simulation.java:456) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) Note the piece of code in question: if(player != null) { createRainbow(player); } is located in the world's code, if that helps.
bourne bourne

2011/11/16

#
change if(player != null) to if (player.getWorld() != null) This is because the player does not become null when removed from the world. But when it has no world, you can't call its location methods which is what is causing the error.
RM6442 RM6442

2011/11/16

#
And the cat has been destroyed!!! Sorry, it works now, thanks for the help. Now, if only I can get someone to help me change fonts.
bourne bourne

2011/11/16

#
No problem. And changing a font of text being drawn in a GreenfootImage?
RM6442 RM6442

2011/11/16

#
Yes, I been through changing a font's size, but the ability to change the actual font evades me. I have tried looking everything, google, err google again, and I guess google for a third time? Any way, I've also been trying to look for a list of available fonts, if you could give me a link, it would be pretty helpful.
bourne bourne

2011/11/16

#
You need an import statement at the top of your class, import java.awt.Font; And something like this will work, (where "pic" is a GreenfootImage) Font font = new Font("New Times Roman", Font.PLAIN, 12); pic.setFont(font); You can alter the construction of the Font (more info found here http://download.oracle.com/javase/6/docs/api/java/awt/Font.html), Font(String name, int style, int size) Creates a new Font from the specified name, style and point size. To get a list of fonts on your computer, I found this to work: GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); ArrayList<Font> fonts = new ArrayList<Font>(); Collections.addAll(fonts, e.getAllFonts()); // where you would need to import: import java.awt.GraphicsEnvironment;
RM6442 RM6442

2011/11/16

#
*ahem* "cannot find symbol - class ArrayList" I didn't do something wrong, did I?
bourne bourne

2011/11/16

#
Oh sorry you need another import statement, import java.util.*;
RM6442 RM6442

2011/11/16

#
Great, no errors or anything. I'm just not sure how to view it.
bourne bourne

2011/11/16

#
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); ArrayList<Font> fonts = new ArrayList<Font>(); Collections.addAll(fonts, e.getAllFonts()); This gives you an ArrayList of the Fonts available, should you want to access them within your code. (more information on ArrayList here, http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html) pic.setFont(fonts.get(Greenfoot.getRandomNumber(fonts.size()))); // will set the font to a random font found on your computer Otherwise you should just use the following with the name of a Font you would like to use. Any font found in a text editing program on your computer would work. new Font("New Times Roman", Font.PLAIN, 12);
RM6442 RM6442

2011/11/16

#
Is it supposed to be tiny? I do this: pic.drawString("This is an example of a font",1,20); setImage(pic); size = fonts.size(); right after : pic.setFont(fonts.get(Greenfoot.getRandomNumber(fonts.size()))); and it looks like a line, or occasionally a squiggle, sometimes dashes. I tried changing the font size, before the code listed above, but it has no effect: pic.setFont(pic.getFont().deriveFont(48.0f)); What did I do wrong
RM6442 RM6442

2011/11/16

#
about the: size = fonts.size(); I got curious about what value was being put in. It's always 468. I'm assuming it's the size of the list.
bourne bourne

2011/11/16

#
I have never used the method deriveFont But in the line pic.drawString("This is an example of a font",1,20); the 20 needs to be at least about equal to the size of the font. Since this is the baseline of where the text 'sits' on. I would do something similar to: Font font = fonts.get(Greenfoot.getRandomNumber(fonts.size())); pic.setFont(new Font(font.getFamily(), Font.PLAIN, 48)); What are you needing this font for? In general purposes you wouldn't need to get all the fonts in a list. If you are looking for a "nice or cool looking" font, for a particular part of your program, I would test different fonts in a text editor like ms Word.
You need to login to post a reply.