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

2012/11/29

Drawing text on screen

-nic- -nic-

2012/11/29

#
How would you do this very simply and without importing many imports?
-nic- -nic-

2012/12/1

#
Im not very good at drawing but how would i draw a rectangle then draw on that a smaller rectangle in a different color and then draw text in the middle of that? please help
Gevater_Tod4711 Gevater_Tod4711

2012/12/1

#
If you want to print it on the background you have to call the background out of the world class and then draw something on it using the methods from the GreenfootImage class. If you want different colors you should import java.awt.Color; There you can find many different static colors you can use. If you want to write something on the screen you should also import java.awt.Font; because the default font if very small.
danpost danpost

2012/12/1

#
Actually, you only need to import java.awt.Color to accomplish this.
// additional imports
import java.awt.Color;
// drawing your image
int fontSize = 24; // whatever size
Color textColor = new Color(0, 255, 128); // whatever color
String text = "Text to display"; // whatever text
GreenfootImage textImage = new GreenfootImage(text, fontSize, textColor, new Color(0, 0, 0, 0));
GreenfootImage image = new GreenfootImage(textImage.getWidth()+fontSize/2, textImage.getHeight()+fontSize/2);
Color outerColor = new Color(196, 196, 0); // whatever color
image.setColor(outerColor);
image.fill();
Color innerColor = new Color(0, 0, 196); // whatever color
image.seColor(innerColor);
image.fillRect(fontSize/8, fontSize/8, image.getWidth()-fontSize/4, image.getHeight()-fontSize/4);
image.drawImage(textImage, (image.getWidth()-textImage.getWidth())/2, (image.getHeight()-textImage.getHeight())/2);
It is not neccessary to declare all the values before using them. The following is basically the same code with the values hard-coded within the making of the image (and simplified) and produces the exact same image as the code, as it is, above:
GreenfootImage textImage = new GreenfootImage("Text to display", 24, new Color(0, 255, 128), new Color(0, 0, 0, 0));
GreenfootImage image = new GreenfootImage(textImage.getWidth()+12, 36);
image.setColor(new Color(196, 196, 0));
image.fill();
image.setColor(new Color(0, 0, 196));
image.fillRect(3, 3, image.getWidth()-6, 30);
image.drawImage(textImage, 6, 6);
-nic- -nic-

2012/12/1

#
thanks a bunch Danpost im not very good at drawing images
danpost danpost

2012/12/1

#
Maybe you should review what the code does and try to get an understanding of what is involved (from the API, mathematical, and procedural perspectives).
-nic- -nic-

2012/12/1

#
i will very much so i have just used images in the past but im having troubles with images using greenfoot4android
You need to login to post a reply.