Swing Line break - overriding graphics draw - swing

In the code, I am reading an xml file and painting the contents accordingly on a JPanel.
If the file contains a tag <LineBreak/> I am supposed to insert a Line Break before the next word.
I thought I could use a
g.drawString(..)
but that requires that I know what follows the break, which in this case, I do not.
Is there any other way to do this by overriding the draw method?
Really appreciate the help. Thanks

I think you will need to use FontMetrics (g.getFontMetrics()) to determine the number of vertical pixels needed to display a line of text. When you call drawString to paint the second line of text, you must pass a y value large enough that the second line of text is drawn below the first line of text.
String[] lines = myStringFromXmlDoc.split("\r\n|\r|\n");
int verticalOffset = 0; // your starting offset would probably be more dynamic
foreach(String line : lines)
{
g.drawString(line, 0, verticalOffset);
// use FontMetrics to implement calcLineHeight for yourself
int currentLineHeight = calcLineHeight(line, g);
verticalOffset += currentLineHeight;
}
Edit:
Also see java.awt.font.LineBreakMeasurer for a possibly useful utility class.

Related

AlievePdf create dashed line

I need to create a schema to pdf. This schema contains a lot of dashed lines. If I paint it like not dashed lines:
But if I try to paint it by using dashed lines will paints next:
Don't paint constant bar length and constant distance between bars. String example:
pdfItem.lineStyle(new RGBColor(color), 0.5, 1, 1,null, null, new DashedLine ([ 6 ]) );
pdfItem.moveTo(firstPoint.x, pointsOnY-firstPoint.y);
pdfItem.lineTo(secondPoint.x, pointsOnY-secondPoint.y);
pdfItem.end();
I think this is why lines in the schema composed of several shorter lines фтв and it affects the display if line is dashed.
How can I fix this trouble?
If you are going to paint a dotted-line rule, you should paint it in one operation from the start point to the end point of the line ray. Otherwise you can have various problems occur:
If you are attempting to concatenate segments together to form one longer dotted-line rule (i.e. without overlapping segments) each segment must end on a full increment of the first dot pattern, so that when the next generated dotted line begins after it, the eye will not see an error in spacing, or a dash that is too long.
Similarly, if your line segments do overlap for some reason, the start point of the new segment must align with the start point of a dash on the underlying segment, or again you will have dotted lines that are either of different gap spacing or, in a worst-case scenario, completely solid in appearance.

Setting QTextDocument painter's rectangle (where to paint)

I am painting on a window simple html using QTextDocument::drawContents(painter)
I want to do the drawing inside some margins in the window but I don't see a direct way to specify the target rectangle of the painting (in the painter/window).
I guess a few ways to do it:
Using the QTextDocuments::setMargin (although this does not allow different values for left/top.
Placing the html into an styled <div>
Applying a translation transform to the painter.
But all this seems a bit too much for what I want to do and I guess if I a missing something straight (as you do with QPainter::drawText where you tell the target rectangle)
Set the textWidth property to the width of the area where the text is supposed to fit. The clipping rectangle you pass to drawContents will cut the text off vertically if there's too much of it to fit; you can't do much about that of course.
So, this would be the missing function you're after:
void drawContents(QPainter * p, QTextDocument & doc, const QRectF & rect) {
p->save();
p->translate(rect.topLeft());
doc.setTextWidth(rect.width());
doc.drawContents(p, rect);
p->restore();
}
Yes, you do need to jump through a few hoops, that's why it needs to be factored out. It's perhaps lamentable that a similar overload of drawContents doesn't exist.

How get a string width in Libgdx?

I wonder know how to get the width of my string in pixels
BitmapFont API < 1.5.6
To mesure the width of a String you use your Font and get the bounds of the String, you are going to draw.
BitmapFont.getBounds(String str).width
BitmapFont API
You can get the height to for the right offset for drawing too. Just replace width with height.
In addition for multiline texts use getMultiLineBounds(someString).width to get the bounds.
BitmapFont API >= 1.5.6
The BitmapFont API changed in 1.5.7 so there is a different way to get the bounds now:
BitmapFont.TextBounds and getBounds are done. Instead, give the string to GlyphLayout and get the bounds using its width and height fields. You can then draw the text by passing the same GlyphLayout to BitmapFont, which means the glyphs don’t have to be laid out twice like they used to.
Source (Web archive)
Example:
GlyphLayout layout = new GlyphLayout(); //dont do this every frame! Store it as member
layout.setText("meow");
float width = layout.width;// contains the width of the current set text
float height = layout.height; // contains the height of the current set text
According to #Nates answer: https://stackoverflow.com/a/20759876/619673 calling method
BitmapFont.getBounds(String str).width
not always returns proper width! Especially when you are reusing font.
If you want draw text for example in the center of part of view port, you can avoid this problem by using another method
BitmapFont.drawWrapped(...)
Example code:
font.drawWrapped(spriteBatch, "text", x_pos, y_pos, your_area_for_text, BitmapFont.HAlignment.CENTER);
If you use skins in UI it is a hassle to find out the correct font to feed into the GlyphLayout.
In this case I use a throw away instance of Label to figure everything out for me then ask the Label for the width.
Skin skin = getMySkin();
Label cellExample = new Label("888.88888", skin);
cellExample.layout();
float cellWidth = cellExample.getWidth();
Table table = new Table(skin);
table.defaults().width(cellWidth);
// fill table width cells ...
This is not the answer if you want to position the text yourself, but it is useful to make a UI layout stable and less dependent on the actual content of the cells.

html5 splash text in different lines

i am making a html5 text splash in which i require the text to be in multiple line. For eg
Everything
Awesome
Is Really
Not awesome
But is coming in one line such that
Everything
Awesome
Is Really
Not awesome
i am unable to create a line break . for different words.Can anyone help me with this.
I have created a demo to show what is goin on http://cssdeck.com/labs/owxlsevi/0.
Can anyone please suggest me how to get each word in different line like i specified above.
Unfortunately, the fillText and strokeText methods of the canvas 2d context do not support line-breaks. When you want multi-line text, you have to do this yourself by splitting the text into substrings and output them separately with different y-offsets.
This is a function which draws a text with line breaks (\n) in multiple lines separated by a fixed line height of 12 pixels:
function fillMultilineText(context, text, x, y) {
// split text at line-breaks and put the resulting sub-strings into an array
var lines = text.split('\n');
// draw each line with 12pixel offset
for(var i = 0; i < lines.length; i++) {
var line_y = y + i * 12;
context.fillText(lines[i], x, line_y);
}
}
Here is a related question with some solutions:
HTML5 canvas ctx.fillText won't do line breaks?

Comparing two bitmaps against each other for match as3

I'm trying to position an image on top of another image based upon the make-up of the smaller image. The smaller image is a cut-out of a larger image and I need it to be positioned exactly on the larger image to make it look like a single image, but allow for separate filters and alphas to be applied. As the images are not simple rectangles or circles, but complex satellite images, I cannot simply redraw them in code. I have quite a few images and therefore do not feel like manually finding the position of each image every and hard setting them manually in actionscript. Is there any way for me to sample a small 5-10 sq. pixel area against the larger image and set the x and y values of the smaller image if a perfect match is found? All the images are in an array and iterating through them has already been set, I just need a way to sample and match pixels. My first guess was to loop the images pixel by pixel right and down, covering the whole bitmap and moving to the next child in the array once a match was found, leaving the matched child where it was when the perfect match was found.
I hope I understood your question correctly.
There may be an option that uses copypixels to achieve what you want. You can use the bitmapdata.rect value to determine the size of the sample you want, and loop through the bigger bitmap using thet rectangle and a moving point. Let's see if I can code this out...
function findBitmapInBitmap(tinyimg:BitmapData, largeimg:BitmapData):Point {
var rect:Rectangle = tinyimg.rect;
var xbound:uint = largeimg.rect.width;
var ybound:uint = largeimg.rect.height;
var imgtest:BitmapData = new BitmapData(tinyimg.rect.width, tinyimg.rect.height);
for (var ypos:uint = 0, y <= ybound, y++) {
for (var xpos:uint = 0, x <= xbound, x++) {
imgtest.copyPixels(largeimg, rect, new Point(xpos, ypos);
if (imgtest.compare(tinyimg) == 0) return new Point(xpos, ypos);
}
}
return new Point(-1,-1); // Dummy value, indicating no match.
}
Something along those lines should work - I'm sure there's room for code elegance and possible optimization. However, it seems like something like this method would be very slow, since you'd have to check each pixel for a match.
There is a better way. Split your big image into layers, and use the blitting technique to composite them at runtime. In your case, you could create a ground texture without satellites, and then create the satellites separately, and use the copyPixels method to place them whereever you want. Google "blitting in as3" to find some good tutorials. I'm currently working on a game project that uses this technique and it's a very good method.
Good luck!
Edit: Forgot to code in a default return statement. Using this method, you'd have to return an invalid point (like (-1,-1)) and check for it outside the function. Alternatively, you could just copy your small bitmap to the big one within the function, which would be much more logical, but I don't know your requirements.
You need to find pixel sequence in the big image. BitmapData.getPixel gives you pixel value. So get first pixel from small image, find it in big image, then continue comparing until you find full match. If you have trouble to code that, feel free to ask.
For the actual comparison, there's BitmapData.compare which returns the number 0 if the BitmapData objects are equivalent.