How to create a paragraph of text in java - java

Is there any way to make a some text appear as a paragraph, since at the moment i use,
private String howtoplay = "[Insert How To Play THe Game Instructions Here]";
When i use strings the text just goes off my game screen which is 800 pixels by 600 pixels.
How do i make it so it doesn't go out of the screen and just either just a block of text, or that when the word reaches the end of the game screen, it will start on a new line.
I have only used strings and so i have been used t using them to create small amounts of text like the title and small commands instructing what button to click.
So in short, is there any way to create a block of text without creating like another class, or just creating a bunch of strings?
EDIT:
So it turns out that i cannot create a new line break, due to the fact i have a drawString
g.drawString(title, ((Sea_InvadersDisplay.WIDTH/2)-(titleWidth/2))-2, (Sea_InvadersDisplay.HEIGHT/2)-123);
and i have found the solution here:
Problems with newline in Graphics2D.drawString
So in other words i have found the solution to my problem.

use can \n to break to the next line and you can use.
everytime you need to move to a new line or new row u just pass \n into your string

Try to use html code.... new line in html code is represented by <br/> tag... so you will have something like this:
<html>
<p>[Insert How To Play THe Game Instructions Here]</p>
</html>

Related

Divide string in lines

I am currently trying to fix my problem with string length at buttons, in android application. So i parse string and then set it as button text. But if one text is bigger then 9 characters or 10 it gets displayed out of the button overlay. I know i could make text smaller but this is not good solution because i already have small text.
So what you guys recommend for example if i have:
String text = "ThisIsSomeRealyLongText";
How can i split this string in 2 lines or when i reach character number 9 just use /n (to break text)?
Using too much text on a button is never a good idea. You should think about using an icon, or shorter text with (if needed) more explanation text somewhere else. Remember,the best UX will be the one with the least amount of reading involved.
Using regex you can do this. It will break the string into 9 characters per line.
text = text.replaceAll("(.{9})", "$1\n");
Use
(new line):
android:text = "ThisIsSome
RealyLongText"
But in code, you can just use \n. You can define in ../res/values/strings.xml:
<string name="longname">ThisIsSome\nRealyLongText</string>
then set the text for you button
android:text="#string/longname"

How to know the line number when you select on line on a JTextArea? [JAVA]

I have done a GUI in Java with a JTextArea. It is filled with the content of a file.
When I select words with the mouse on the textarea, a new frame pops up on which I do some operations on the selected words. To do these operations, I need to know the line number of the selected text...
Does someone know how to get the line number?
(I look to some methods on the classes JTextArea and MouseListener, but i dont know how to do that...)
Thanks ;)
Check out the Text Utilities. The getLineAtCaret() method is close to what you need. It uses the offset of the caret to get the line number. In you case you will need to use the start offset of the selected text.

JTextArea indentation

I'm having a problem with JTextArea (not a problem though).
I want to create my own college program, one of the function is to write down and save a list of homework each day. It's text area having the main function here, which is to type everything my lecturer said. But text area will act like Windows original notepad, which is not keeping the indentation. So my questions are:
How do I have a function where I press the SHIFT+TAB key it will decrease indentation by one.
If number 1 is possible, then how do my program have behaviour when I press ENTER key, it will have a new line with previous indentation? Just like NetBeans and other IDE.
Edited: I'm a beginner in Java, in truth, I'm making this program while studying Java in my college.
How do I have a function where I press the SHIFT+TAB key it will decrease indentation by one.
You need to use key bindings to listen for the user typing Shift+Tab.
If number 1 is possible, then how do my program have behavior when I press ENTER key, it will have a new line with previous indentation? Just like NetBeans and other IDE.
Use a similar piece of logic to capture the Enter key presses. Then, check the previous line in the editor and count the number of tab characters preceding the text. Output a similar number at the start of the new line.
you could use Javascript/jquery for indenting by inserting empty space of specific line. while pressing your hot key combination call function to insert five spaces on your cursor.

importing dialogue from a text file and displaying it on a drawpanel - Java

i have a bit of problem which i have run into, im trying to shorten my code for a game and i got the idea to import all the dialogue from a text file instead of hardcoding it into the code itself. however it would work if every screen only had one line to display but somescreens have more than one
if (screenCount==1)
g.drawString("Hi there!", 25,515);
if (screenCount==2)
g.drawString("Welcome to the world of Pok\u00E9mon!", 25,515);
if (screenCount==3){
g.drawString("My name is Professor BLANK!", 25,515);
g.drawString("Everyone calls me the Pok\u00E9mon Professor!", 25,540);
}
As you can see for screen one and two i could easily put the dialogue in a text file like so:
1:Hi there!
2:Welcome to the world of Pok\u00E9mon!
But for the third screen I couldnt figure out how to import it/ write it in a text file and import it
3:My name is Professor Shinwa!
Everyone calls me the Pok\u00E9mon Professor!
MORE INFO: The game only displays two lines at a time at
g.drawString("", 25,515); //the first line x and y values
g.drawString("", 25,540); // the second line x and y values
I have around 37 screens and around half or more are two lines.
Thanks for any help, much apreciated :D
You can use the escape sequence \n in your String as pointed in the JavaDocs:
Insert a newline in the text at this point.
Your String could look like:
3:My name is Professor Shinwa!\nEveryone calls me the Pok\u00E9mon Professor!
BUT I think a better solution is to put each screen text in a separate textfile. You can then read the file until no more lines are remaining and print them on the screen. The textfiles should be named with screen#, e.g. screen1, screen2 ... so you don't need this big amount of if-blocks in your code. Just concatenate the screen with your current screennumber and read this file, done. What if you need dialogues with response from the user? How would you handle this case?

Selecting specified text in an HTML formatted JEditorPane

I am displaying text in a Java JEditorPane using HTML to fomrat the text. I am also designing a search function that finds text in the JEditorPane selects the text and then scrolls to it. My problem is creating an algorithim that will actually specify the beginning and ending position for the selection.
If I simply retrieve the text using myeditorpane.getText(), then find the search string in the result, the wrong selection start and end positions are calculated with the wrong text being selected (the tags are throwing the calculation off). I tried removing the html tags by executing a replace all function text.().replaceAll("\<.*?>","") before searching for the text (this replace all removes all text in between the tags) but still the wrong selection points are calculated (although I'm getting close :-)).
Does anyone have an easy way to do this?
Thanks,
Elliott
You probably want to be working with the underlying Document, rather than the raw text, as suggested in this HighlightExample.
You need to find the start location of the text. I guess something like:
int offset = editorPane().getDocument().getText().indexof(...);
Then to scroll you can use:
editorPane.scrollRectToVisible( editorPane.viewToModel(offset) );
Read up on Text and New Lines for more info.

Categories