I am coding a game in fonts and I encountered a problem.
I have no idea how to use fonts even though I tried everything.
Can you also please explain what is a glyph?
And where to get .fnt files if that is needed?
A lot of that stuff is either no longer used, or just not necessary anymore. Basically all you need is a Graphics instance, and to use something like the drawString method. You can also use setFont on the Graphics object to set the font you want to use for rendering.
You can get a Graphics instance from your GameContainer, and then just render to it. If you want to see an example, the render method of this file in my project on github has some lines in it that render debug information to the screen (fps, memory usage, etc.). The relevant part of the code is:
if (ConfigValues.renderSystemInfo) {
g.setColor(new Color(50, 50, 50, 180));
g.fillRect(0, 0, 300, 70);
g.setColor(Color.white);
g.drawString("MEM total(used): " + (Runtime.getRuntime().totalMemory()/1000000) + "(" + ((Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/1000000) + ") MB", 10, 25);
g.drawString("Ped. history size: " + (peds.size()*peds.get(0).getMovementHistory().size()) + " nodes", 10, 40);
}
Just for reference, a glyph, at least conceptually, is just the visual representation of a character in a font. So, the way that the letter a looks in a given font, for example, is that font's glyph for that letter. There's a different glyph for upper and lowercase (a or A), which is what makes the upper and lowercase letters appear different in fonts.
Font font = new Font('Verdana', Font.BOLD, 32);
TrueTypeFont ttf = new TrueTypeFont(font, true);
In your update method:
ttf.drawString(32.0f, 32.0f, "Your words here", Color.green);
Related
For some reason, if you highlight the Text Area text the background turns black and therefore makes the text unreadable, also when I select the radio buttons at the bottom (see attached pictures), the whole text area then goes black, again making the text unreadable! Anyone know why it is doing this?
Here's the code for my text area...
//Text area that displays the games instructions
textAreaInstructions = new JTextArea(
"To play choose your difficulty and then click start. \n\n" +
"The aim of the game is to select the 'golden' rectangle, you will be presented with 4 \n" +
"rectangles and you must choose one that has height and width proportions that represent \n" +
"the golden ratio: 1.618. If you're not too sure what a golden rectangle looks like it's \n" +
"best you start on beginner until you get the hang of it! \n\n" +
"For every correctly identified golden rectangle you score 1 piece of 'Gold', however if you \n" +
"incorrectly identify a golden rectangle, 1 piece of 'Gold' will be taken away from your \n" +
"score. Remember, you only have 30 seconds, so collect as much 'Gold' as you can, good luck! \n");
c.add (textAreaInstructions);
textAreaInstructions.setBounds(130, 110, 600, 200);
textAreaInstructions.setEditable(false);
textAreaInstructions.setBackground( new Color(0, 0, 0, 0) ); //Transparrent background to text area
Swing doesn't understand transparent colors. Instead, you MUST use setOpaque and pass it false
textAreaInstructions.setEditable(false);
//textAreaInstructions.setBackground( new Color(0, 0, 0, 0) );
textAreaInstructions.setOpaque(false);
You can use a JLabel with it's text wrapped in HTML as well, for example. JLabel is transparent by default
Now I feel like I've been looking all over the internet to find out how to add a border on a text, so I decided to ask here, since you guys always knows the answer.
So, how do I, in java, draw a border of approx 2 pixels around every letter in a string drawn on a Graphics2D element ?
Like this:
Thanks in advance.
I found one simple solution in Javaworld for drawing an outline on text in Java:
g.setColor(Color.red);
g.drawString("Outline", ShiftWest(x, 1), ShiftNorth(y, 1));
g.drawString("Outline", ShiftWest(x, 1), ShiftSouth(y, 1));
g.drawString("Outline", ShiftEast(x, 1), ShiftNorth(y, 1));
g.drawString("Outline", ShiftEast(x, 1), ShiftSouth(y, 1));
g.setColor(Color.yellow);
g.drawString("Outline", x, y);
Essentially, you draw the same string shifted in each direction first before you draw the string in the desired color. This works well for a one pixel outline, but does not scale well to thick outlines as there may be gaps in the corners if you repeat the shifting multiple times.
Another solution would be to use a transformation and getOutline() which is a method of the TextLayout class. An example for doing outline can be found here.
See: Transforming Shapes, Text, and Images. Set the "primitive" to "text" and the "rendering" to "Stroke and Fill" in the transform example.
I'm going to draw some sort of glassy buttons in java me (targeting devices with MIDP 2.0).
An Example:
Actually I need to impelement Gradient and Bevel-Emboss effects in Java ME, do you have any opinion or "guideline" on how to implement this?
EDIT: Now I know how to draw gradient background, but That is not sufficient.
Is it possible to draw such glassy button in Java ME?
I've worked with C# and I can draw these kinds of glassy buttons there, but I'm struggling on how to simulate something like these buttons in Java ME or at least something near to them, Note that I'm looking for a good guidance and help to move forward.
Do I need to provide more information? If so, please let me know.
Thanks In advance.
You can do it by using an alpha gradient paint. Heres an example:
Graphics2D g = (Graphics2D)screen.getGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setFont(new Font("Serif", Font.BOLD, 30));
Color c1 = new Color(0,0,0,0);
Color c2 = new Color(0,0,0,100);
GradientPaint gradient = new GradientPaint(10,8,c1,10,40,c2,true);
g.setColor(Color.GREEN);
g.fillRect(0, 0, screen.width, screen.height);
g.setColor(Color.BLACK);
g.setPaint(gradient);
g.fillRoundRect(100, 100, 200, 50, 25, 25);
g.setPaint(Color.BLACK);
g.drawRoundRect(100, 100, 200, 50, 25, 25);
g.drawString("Hello World!", 118, 135);
it will look like this:
You can use LWUIT framework for developing java-me (MIDP 2.0) applications. Its good GUI framework for java-me application. Here you can create the theme manually using ResourceEdit. For more info see this discussion and LWUIT blog also.
yoy can not do so in me because it dont have such reach effect on UI
get image that have effect like that gredient and make it transparent using
btn.getStyle().setBgTransparency(100) // from 0-255
note: you must use images that is already semi transparent , if not than setBgTransparency would not work properly
I can not recommend you to use canvas
i recommend you to use use LWUIT
it have many more effects ads you required ,like
btnBuzz.getStyle().setBorder(Border.createBevelLowered());
it have different layout as well
So here's the deal:
I've got a JPanel and a JTextArea inside that one. The JPanel has a background color which has an alpha value of 200, ie. you can paritally see the background image through the JPanel. Ain't that called partial transparency? Anyway, then I've set the JTextArea non-opaque, so that I can fully see through that one:
JPanel p = new JPanel();
p.setBackground(new Color(237, 234, 215, 200);
JTextArea jta = new JTextArea("Blahblahblahblah");
jta.setOpaque(false);
p.add(jta);
Ok, so when I hit a button, the text will be changed like this:
jta.setText("new BlahBlah...");
Then it happens: the first text remains back there with a new partially transparent film on it. And the text I added, comes on top of course, but right there behind you can see the previous one. When I change the text some few times more, the ghost disappears.
The screenshot as a link.
Translation (the screenshot has 3 shots on a row): Left one: "Question # 1 out of 8: (a political question)? (My comment: OK)"Center: "Question # 2 out of 8: (another question, never mind)? (My comment: The ghost is there! [and the arrow pointing it out])"Right-hand side: "Question # 8 out of 8: (another question)? (My comment: OK)"
If I reduce the amount of questions this program swaps, from 8 to 3, for example, the last one looks usually good, the ghost is gone. But sometimes it sticks on no matter what I do. Could it possibly have something to do with not enough memory (I hardly swallow that, though)?
So, please help me out, dudes! I've got the deadline in 48 hrs.
PS. In case you wonder, that language is Finnish. I'm working on a school project: It's gonna be a comparing machine that can have a variety of applications: For example, if you have a public election coming, the press/media may use this kind of machine on the web to get the candidate's opinions on whatever issues, and then voters may enter their opinions too, and the machine calculates which candidates match the voter's thoughts best. See my homepage, ie. the project blog (Finnish) for more screenshots in case you're interested.
an55i
p.setBackground(new Color(237, 234, 215, 200);
Swing does not support transparent backgrounds.
Swing expects a component to be either:
opaque - which implies the component will repaint the entire background with an opaque color first before doing custom painting, or
fully transparent - in which case Swing will first paint the background of the first opaque parent component before doing custom painting.
The setOpaque(...) method is used to control the opaque property of a component.
In either case this makes sure any painting artifacts are removed and custom painting can be done properly.
If you want to use tranparency, then you need to do custom painting yourself to make sure the background is cleared.
The custom painting for the panel would be:
JPanel panel = new JPanel()
{
protected void paintComponent(Graphics g)
{
g.setColor( getBackground() );
g.fillRect(0, 0, getWidth(), getHeight());
super.paintComponent(g);
}
};
panel.setOpaque(false); // background of parent will be painted first
Similar code would be required for every component that uses transparency.
Or, you can check out Background With Transparency for custom class that can be used on any component that will do the above work for you.
Finally I got it showing up right! It's just that:
Both the container (p in this case) and the component (jta) inside must be setOpaque(false);
The container must always clear itself by grabbing the inner component's background color, which - actually - is the color that the inner component kind of gets from it's ancestor, ie. the container itself.
Aren't I somehow correct? Well, at least the code does it's trick.
Class fields:
private JTextArea jta;
private JPanel p;
Later on:
jta = new JTextArea("BlahBlahBLAH");
jta.setBackground(new Color(237, 234, 215, 200));
p = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
if(getContentPane().isAncestorOf(jta)) {
g.setColor(jta.getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
}
}
};
p.setOpaque(false);
jta.setOpaque(false);
p.add(ta);
And that's it. I even put some Borders and stuff before adding "jta" to "p". And what's coolest, I actually learnt something about this. Thanks for support!
Why does the text quality in Processing not look as good as Flash? In general it looks slightly grainy, and hard to read.
I had this problem as well. I found that if you continually overwrite the text (in the draw loop for example) without 'wiping' the underlying surface (by calling background in the draw loop for example) the text becomes jagged.
I think this is because the semi-transparent corners on the letters due to antialiasing get written over and over until they become fully opaque.
Take a look at this example, (you will have to create a font 'ArialMT-20')
PFont fontA;
void setup() {
size(300, 100);
fill(0);
fontA = loadFont("ArialMT-20.vlw");
textFont(fontA, 20);
// Background drawn once here
background(255);
}
void draw() {
// When mouse is held down, the background is wiped
if (mousePressed) {
background(255);
}
text("Hi there", 20, 50);
}
try to put the smooth() command inside the setup() method and be sure you are loading a font that exist in your system or inside your data folder.
IMHO the font rendering quality in processing is quite good :)