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
Related
So my calculator program looks good unless I resize it. Then things get all out of whack. Right now I'm using a GridBagLayout to organize my buttons (thought it was best to use GridBag for this situation given a number of buttons). I set the preferredSize to what I would like the size of the buttons to be upon startup. How can I get the buttons to change size upon window resizing?
That's what happens after I resize the app smaller.
When I make it bigger, everything just stays the same and I get a grey emptiness on either side to make up space, instead of enlarging the buttons.
For my buttons dimensions i'm using new Dimension(80, 55)
Then setting the preferredSize to that dimension.
For the memory buttons and jlabels I compute some math to make it as wide as the total buttons across.
Please Note I'm still learning java, as well as the swing library. END NOTE
UPDATE I tried setting the minimum and maximum size when I set the preferred size and still no go END UPDATE
UPDATE 2 I tried setting the size of my main panel to match the size of the app upon start and that didn't work END UPDATE
EDIT 2 My hierarchy is like this:
FRAME
MAINPANEL - Contains all three panels
TOPPANEL - Contains the display
MIDDLEPANEL - Contains memory buttons
BOTTOMPANEL - Contains calculator buttons
END EDIT 2
I recommend using TableLayout from Oracle (you can download it there), because it is more comfortable. Sure it is possible with the GridBagLayout too, but especially when you're new, the TableLayout is much easier to understand and to configure.
double size[][] =
{{0.25, 0.25, 0.25, 0.25}, //Horizontal
{50, TableLayout.FILL, 40, 40, 40}}; //Vertical
frame.setLayout (new TableLayout(size));
It's basically a Grid which is separated into rows and columns. So, each of the cells have an id, for example "0, 0".
The height and width is adjustable with pixels, percentage, and Layout.FILL, which uses the available space. if you are using multiple Layout.FILL, it will separate the available space between them.
To add Buttons or other elements, you will need to know the cell coordinates, if it's bigger than one cell, you can add an end cell to, so you can use the whole place available.
// Add buttons
frame.add (button[0], "1, 1, 4, 1"); // Top
frame.add (button[1], "1, 4, 4, 4"); // Bottom
frame.add (button[2], "1, 3 "); // Left
frame.add (button[3], "4, 3 "); // Right
frame.add (button[4], "3, 3, c, c"); // Center
frame.add (button[5], "3, 3, 3, 4"); // Overlap
Here some useful links:
http://www.oracle.com/technetwork/java/tablelayout-141489.html
http://www.clearthought.info/sun/products/jfc/tsc/articles/tablelayout/Simple.html
http://www.clearthought.info/sun/products/jfc/tsc/articles/tablelayout/Preferred.html
I am implementing a SNMP-listener with a GUI. The GUI is supposed to print the parsed SNMP-traps in different color based on their severity. To do this I have used JTextPane.
Right now the program is fully functioning, I can append traps in different colors.
But the colors change and text get messed up, so that it is not readable, sometimes when appending new strings. For example some newly appended line take the color of an old appended line and vice versa. Also the red I am using becomes dark red, and the text is "smudgy", io the text does not have sharp lines. These problems disappear when resizing the widow, but come back when scrolling or appending new information.
I have tried to repaint and I have tried added a background color, but it made no difference.
Anyone else had this problem?
This is the code I am using to append text:
private void appendToPane(String msg, JTextPane p, Color c) {
Style style = p.addStyle("I am a style", null);
StyleConstants.setForeground(style, c);
StyledDocument doc = p.getStyledDocument();
try {
doc.insertString(doc.getLength(), msg, style);
} catch (Exception e) {
System.err.println(e);
}
}
It is working, the only problem is that the text gets messed up when scrolling or appending new text.
With messed up I mean:
- The upper or lower half of a row disappear (leaving half unreadable letters)
- The text partly changes color (red becomes dark red for some sections)
- Two rows change color with each other. (a green section gets a red row, a red section gets a green row)
- The letters looks smudgy (the letters does not have sharp lines)
All these problems disappear when resizing the window, but reappears when scrolling or appending new text.
For example some newly appended line take the color of an old appended line and vice versa
Yes, inserted text will inherit the attributes of the previous text unless you specify the attributes for the inserted text.
The basic code for appending text at the end of the text pane would be something like:
// Define a keyword attribute
SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBackground(keyWord, Color.YELLOW);
StyleConstants.setBold(keyWord, true);
// Adding a new line of text
try
{
StyledDocument doc = textPane.getStyledDocument();
doc.insertString(doc.getLength(), "\nEnd of text", keyWord );
}
. Also the red I am using becomes dark red, and the text is "smudgy", io the text does not have sharp lines
Maybe you are using transparent backgrounds. See Backgrounds With Transparency for problems when doing this.
If you need more help then post a proper SSCCE for this and all future questions.
Have you tried redrawing the background behind it before appending the new string?
Or if you do not have a background, maybe adding one to be drawn before you append the new string, even if it is the same color as the current background.
The reason I say this, is perhaps the text is layering upon itself and that is why this is happening.
I am starting to work with libgdx.
I opened the example model-loader in trunk (StillModelViewerGL20.java). In the source I see this code in the render callback:
batch.begin();
font.draw(batch, "fps: " + Gdx.graphics.getFramesPerSecond(), 20, 30);
batch.end();
But I do not see any text on screen. Is there something I need to fix in this example to see text displayed?
Maybe the screen size is smaller than 20,30. Try to draw on 0, 0.
check the order in which you draw your objects, and if this is corrected by changing.
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);
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.