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

2015/6/29

how to make text bold

vessaliuzz vessaliuzz

2015/6/29

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.font.FontRenderContext; import java.awt.Graphics2D; /** * This class provides objects that just show a bit of text on the screen. */ public class Text extends Actor { public Text(int length) { setImage(new GreenfootImage(length * 12, 16)); } public Text(String text) { this (text.length()); setText(text); } public void setText(String text) { GreenfootImage image = getImage(); image.clear(); image.drawString(text, 2, 12); // calculate width of text in pixels // FontRenderContext frc = ((Graphics2D)getImage().getAwtImage().getGraphics()).getFontRenderContext(); // int textWidth = (int)image.getFont().getStringBounds(text, frc).getWidth(); } /** * Adapt location to make placement left-justified. */ public void setLocation(int x, int y) { super.setLocation(x + getImage().getWidth() / 2, y); } } this my text class, how to make the text bold? please help :)
danpost danpost

2015/6/29

#
Import my TextImage Support Class TextImage class into your project and you will be able to easily make text images of any available font, style, colors and size just by creating TextImage objects instead of GreenfootImage objects.
vessaliuzz vessaliuzz

2015/6/30

#
thanks dude. it just import?
danpost danpost

2015/6/30

#
vessaliuzz wrote...
thanks dude. it just import?
You need to download and open the demo in greenfoot and then either copy/paste the code from the TextImage class into a new class in your project or copy/paste the 'TextImage.java' file itself into the 'Greenfoot/lib/greenfoot/common' folder so you can 'Edit>Import class...' from the menubar in the greenfoot application.
vessaliuzz vessaliuzz

2015/7/1

#
one question more sir, please help i imported your class into my project, and than, im still confusing this the text that i want to bold. on class "Board" so what the code that i need to make this to bold, Thanks for your help :) private void createCounters() { boardLabel = new Text("BOARD " + board); addObject (boardLabel, 670, 120); tryLabel = new Text("Nyawa: " + tries); addObject (tryLabel, 670, 150); rollsLabel = new Text("Kesempatan : " + rolls); addObject (rollsLabel, 670, 200); scoreLabel = new Text("Score: " + score + " "); addObject (scoreLabel, 670, 230); } i tried to change Text into TextImage, but error. so what should i do, n what should i write on the Board class to make it bold
danpost danpost

2015/7/1

#
You can change the lines like this:
boardLabel = new Text("BOARD "+board);
to this:
boardLabel = new TextImage("BOARD "+board);
However, unless the field is typecast as a TextImage object, you will not be able to use the TextImage methods on it. That is, you must either declare the field as a TextImage object reference with something like this:
private TextImage boardLabel;
or cast it as type TextImage before using one of its methods -- for example:
((TextImage)boardText).setStyle(1);
vessaliuzz vessaliuzz

2015/7/1

#
ok i try it. thanks sir.. you're my hero thanks :)
You need to login to post a reply.