There is no "isNull" method. As Michael hinted, you can't call any methods on a null value or you will get a NullPointerException when the code runs. The correct way to check for null is to use "==".
if(last == ":)") { addSmiley(); }
if(":)".equals(last)) { addSmiley(); }
GreenfootImage smileyImage = new GreenfootImage(":)", 20, java.awt.Color.BLACK, new java.awt.Color.Color(0,0,0,0)); int width = smileyImage.getWidth();
//The purpose of this method is to draw the text on a // transparent image that is barely large enough to // contain the text in the specified font. Note that the // specified font can either be the name of a general // font family (such as SansSerif), or can be the name // of a specific font (such as English157 BT). I can't // tell you for sure what will happen if the specified // font is not available, but I believe that a system- // dependent default font will be used instead. //Example font family names are: // Dialog // SansSerif // Serif // Monospaced // DialogInput //Example font names are: // Symbol // Tahoma // Times New Roman // Trebuchet MS private GreenfootImage makeTextImage( String text,//Text to draw on the output image String fontName,//Name of the font or font family int style,//BOLD, ITALIC, etc. int size,//Font size in points Color color){//Color in which to draw the text. //Create a new Font object matching the name, style, // and size of the specified font. Font font = new Font(fontName,style,size); //Make a trial run to get a FontRenderContext object // describing this text in this font. GreenfootImage image = new GreenfootImage(1,1); image.setFont(font); image.drawString(text,1,1); FontRenderContext frc = ((Graphics2D)image. getAwtImage().getGraphics()).getFontRenderContext(); //Get a TextLayout object that can be used to get the // critical sizing metrics. TextLayout textLayout = new TextLayout(text,font,frc); //Get the critical sizing metrics and convert from // float to int in the process. int ascent = (int)(textLayout.getAscent() + 1); int descent = (int)(textLayout.getDescent() + 1); int advance = (int)(textLayout.getAdvance() + 1); //Create a new transparent image object with the // dimensions required to barely contain the text. // Instantiation of this object causes the trial // object to become eligible for garbage collection. image = new GreenfootImage(advance,ascent + descent); //Delete the following statement when all testing is // complete. The purpose of this statement is to show // the outline of the image and the way in which its // size correlates with the size of the text. // image.drawRect(0,0,advance - 1,ascent + descent - 1); //Draw the text on the transparent image in the // correct font and color. image.setFont(font); image.setColor(color); image.drawString(text,0,(int)(ascent)); return image; }//end makeTextImage
public final String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; public final String letters = "abcdefghijklmnopqrstuvwxyz"; public final String Numbers = "0123456789"; public final String Symbols = "`~!@#$%^&*()_+-=[]{}|;':<>?,./"; public String txtString = ""; public void act() { String myKey = Greenfoot.getKey(); if (myKey == null) { return; } String myText = ""; if (myKey == "space") { myKey = " "; } // If spaces are not wanted, change 'myKey = " ";' to 'return;' if (myKey == "backspace") { // Code for when 'backspace' is pressed if (txtString.length() > 0) { txtString = txtString.substring(0, txtString.length() - 1); } return; } if (myKey == "enter") { // Code for when 'enter' is pressed return; } // You can continue for other keys: arrow keys, function keys, 'tab' and 'escape' (and any others I may have missed) // Change the concatenation in the following statement to only wanted characters String goodChars = LETTERS + letters + Numbers + Symbols + " "; int myIndex = goodChars.indexOf(myKey.charAt(0)); if (myIndex > -1) { myText = goodChars.substring(myIndex, myIndex + 1); txtString = txtString + myText; } }