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 "==".


1 2 3 | if (last == ":)" ) { addSmiley(); } |
1 2 3 | if ( ":)" .equals(last)) { addSmiley(); } |
1 2 | GreenfootImage smileyImage = new GreenfootImage( ":)" , 20 , java.awt.Color.BLACK, new java.awt.Color.Color( 0 , 0 , 0 , 0 )); int width = smileyImage.getWidth(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | //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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 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; } } |