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

2017/2/8

Make greenfoot.Font.Font(java.awt.Font) protected or even public

barbu110 barbu110

2017/2/8

#
As far as I see greenfoot.Font has a constructor which takes java.awt.Font
/**
 * Creates a Greenfoot font based on a java.awt.Font
 *
 * @param font
 */
Font(java.awt.Font font)
{
    this.font = font;
}
I can't find any usages of this constructor, but it is not protected or public so we can inherit from greenfoot.Font which is a pretty limited abstraction. What can be done to use a java.awt.Font to draw on a GreenfootImage?
davmac davmac

2017/2/8

#
The removal of the use of the java.awt.* classes from the public API was a deliberate decision; for one thing having methods requiring objects of these types meant that Greenfoot is tied to AWT/Swing, which:
  • is essentially deprecated in favour of JavaFX in the Java ecosystem itself
  • is not available on Android
  • is difficult to re-implement, even partially, in any mechanism which allows exporting Greenfoot scenarios as Javascript apps.
The last two points are important as we are planning on implementing export to Javascript and to Android in a future version of Greenfoot. So, in answer to:
What can be done to use a java.awt.Font to draw on a GreenfootImage?
I would suggest: firstly, see about whether this is really necessary; this should only be the case for more advanced use cases. If you do really want to draw java.awt.Font onto a Greenfoot image, you can use the getAwtImage() method from GreenfootImage and manipulate that directly (the return is a java.awt.image.BufferedImage, which you can manipulate with standard AWT functions, though the API is much more complex than what Greenfoot otherwise provides). Note that using getAwtImage() will mean your scenario will not be able to be exported as Javascript/Android app in the future.
davmac davmac

2017/2/8

#
Also:
I can't find any usages of this constructor
It is used in the deriveFont method implementation, and for getFont in GreenfootImage.
but it is not protected or public so we can inherit from greenfoot.Font which is a pretty limited abstraction
I'm not sure what you mean by this. It is not intended that users extend (derive from) the greenfoot.Font class. Though it is possible to do so, it is certainly not intended that one could then call API-private methods. The package-private constructor that you found is an example of such a method. It is not part of the public API and you should not use it. There are various similar methods in other Greenfoot classes.
You need to login to post a reply.