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

2011/7/5

Greenfoot.isKeyDown(any) ???

1
2
3
davmac davmac

2011/7/6

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

2011/7/6

#
Got what you are saying! Thanx.
manster2008 manster2008

2011/7/7

#
mjrb4, you have answered my new question even before I have asked it. I have been making smiley commands for my Typer, and what it does is if the cursor keys are pressed, it takes the last two characters of the message, and tests it with known smileys, & if they are equal to each other, it creates a new object of class Smileys, that set their image according to their smiley characters. I had done the following, and it would not work:
if(last == ":)") {
    addSmiley();
}
It works after I use:
if(":)".equals(last)) {
    addSmiley();
}
Thanks, I also have another question... Is it possible to figure out the number of pixels a string takes up? I want to place this smiley, where the last two characters used to be, but all I know is finding the length() of the string, which gives me the number of characters, I think... Help would be appreciated...
mjrb4 mjrb4

2011/7/7

#
You need to use the FontMetrics class for this, construct it with the font you're using (use the getFont() method on GreenfootImage to find this.) Then use the stringWidth() method on fontmetrics with your specified string. http://download.oracle.com/javase/6/docs/api/java/awt/FontMetrics.html
nccb nccb

2011/7/7

#
The other way to find out the width of a string is to use the GreenfootImage constructor and then check the width of the image. So:
GreenfootImage smileyImage = new GreenfootImage(":)", 20, java.awt.Color.BLACK, new java.awt.Color.Color(0,0,0,0));
int width = smileyImage.getWidth();
That's quite inefficient if you then throw away the image, but I presume you want that image to place on the screen afterwards anyway.
manster2008 manster2008

2011/7/7

#
@mjrb4, I'm not quite sure how to use this FontMetrics system, if you can, could you please explain it a bit more? @nccb, the image in which my characters are placed is a fixed image. I do not know this GreenfootImage constructor, is it the new one that prints on to the background? Is the 20 the font? Black is the foreground, and a transparent Black is the background?
mjrb4 mjrb4

2011/7/7

#
@nccb, the image in which my characters are placed is a fixed image. I do not know this GreenfootImage constructor, is it the new one that prints on to the background? Is the 20 the font? Black is the foreground, and a transparent Black is the background?
This way is the easier to find a string width actually (I've just been using FontMetrics since before GreenfootImage had that constructor so I have it burned into my head :-) In terms of parameters, 20 is the font size, black is the foreground colour (of the text) and the background colour is just specified as a blank transparent colour.
manster2008 manster2008

2011/7/7

#
I assume that this constructor of GreenfootImage uses the FontMetrics you suggested to determine the width and height of the image. I almost never use something unless I get exactly how it works. So, just for my own common knowledge, I would just like to know this: I have a program made by another member, and I see he has used something from java.awt.font called FontRenderContext and TextLayout. Could you please explain how these work. This is how this person's program goes, and I understand the end bit of it, where it gets the ascent and descent, but I don't really get the advance part (What is it exactly?):
//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
davmac davmac

2011/7/8

#
Read the documentation for FontMetrics (linked provided by mjrb4 above). It has details about the ascent, descent and advance, and there is a graphic showing how they relate.
kiarocks kiarocks

2011/7/20

#
danpost wrote...
I had just written some code a few days ago; it is generalized here.
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;
    }
}
Hope it helps!
Does this make an image? If not, how could it?
danpost danpost

2011/7/21

#
What I wrote was a response to 'Greenfoot.isKeyDown(any)' which deals with keystrokes from the keyboard. I was generalizing on how to input text Strings and act on the particular keys that were input through the getKey() method. It had nothing to do with images.
danpost danpost

2011/7/21

#
If you wanted to make an image showing the text String that was input through the keyboard, one way would be: 1) use the 'new GreenfootImage(int, int)' method to create a blank image of the desired size or, grab the image of an existing object with 'getImage()' 2) apply a background, if desired 3) use the 'drawString(String, int, int)' method to add the text to the image 4) use the 'setImage(String)' method to set the image to an object
mjrb4 mjrb4

2011/7/21

#
No - such a method would have no purpose if it did exist, since you can't deference (call a method or access a field) on null anyway (it throws a NullPointerException)! If you want to check whether something is null, just do mykey==null.
davmac davmac

2011/7/21

#
mjrb4, is it possible you answered a question at the end of the first page of the discussion without realising the discussion already flowed on to another page? :)
kiarocks kiarocks

2011/7/21

#
you could make an is null method
//NOT formatted


public boolean isNull()
{
if(myKey==Null);
{
return;
}
}
There are more replies on the next page.
1
2
3