iText 7 Adding colored text and fill to a rectangle - itext

I am trying to add colored text to a rectangle and fill color in the rectangle but the
text seems to be behind the rectangle and so is not visible even though I can select it.
Paragraph = new Paragraph("The quick brown fox");
PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
Rectangle rect = new Rectangle(ps.getWidth() - 90, ps.getHeight() - 100, 50, 50);
new Canvas(canvas, pdfDoc, rect)
.setFontColor(ColorConstants.WHITE)
.setFontSize(12)
.add(p);
canvas.rectangle(rect)
.setFillColor(ColorConstants.LIGHT_GRAY)
.fillStroke();

You first draw the text and then fill the rectangle. Thus, obviously the text ends up behind the rectangle.
Switch the order of your instructions, first fill the rectangle rect on your PdfCanvas canvas and then add the Paragraph p to the Canvas on canvas.

Related

Anylogic: Adding text label to Rectangular Wall

Is it possible to add a text label on top of rectangular walls in Anylogic? I am trying to dynamically create rectangular walls using code and then adding labels. So far I am able to create the walls but the text label does not appear on top of the walls as I want it.
If you are creating walls dynamically I assume you also want to create your text dynamically.
There is a little trick in AnyLogic where you can view the code that gets created for you by AnyLogic discussed in this blog post
You can use the code snipped below to draw a Text object and add it to the presentation collection (which is needed for it to be displayed)
Font _text_Font = new Font("SansSerif", 0, 10 ); // Only needed if you don't have any text in your agent else _text_Font gets created for you.
ShapeText myProgramticallyCreatedText = new ShapeText(
SHAPE_DRAW_2D, true,170.0, 90.0, 0.0, 0.0,
black,"Programatically Created text",
_text_Font, ALIGNMENT_LEFT );
presentation.add(myProgramticallyCreatedText);
Here is the syntax for creating your
ShapeText(boolean ispublic, double x, double y, double rotation, java.awt.Color color, java.lang.String text, java.awt.Font font, TextAlignment alignment)
Constructs a 2D-only text shape with specific attributes.
ShapeText(ShapeDrawMode drawMode, boolean ispublic, double x, double y, double z, double rotation, java.awt.Color color, java.lang.String text, java.awt.Font font, TextAlignment alignment)
Constructs a text shape with specific attributes.
Depending on whether or not you have 3D or not you can rotate the text by changing the correct parameters
If you want to make it visible in 3d and also rotate it correctly you need to create a group, add the text to that group, then create a new group, and the group this new group and then rotate the first group. Like below
Font _text_Font = new Font("SansSerif", 0, 10 );
ShapeText myProgramticallyCreatedText = new ShapeText(
SHAPE_DRAW_2D3D, true,170.0, 90.0, 0.0, 0.0,
black,"Programatically Created text",
_text_Font, ALIGNMENT_LEFT );
ShapeGroup innerGroup = new ShapeGroup(this, SHAPE_DRAW_2D3D, true, 170.0, 20.0, 0.0, 0.0 , myProgramticallyCreatedText );
ShapeGroup outerGroup = new ShapeGroup(this, SHAPE_DRAW_2D3D, true, 170.0, 20.0, 0.0, 0.0 , innerGroup );
innerGroup.setRotationX(-PI/2 );
presentation.add(outerGroup);
(P.S. I figured all of this out using the trick from the blog post referenced above ;-) )
If it should show horizontally on top of the wall, just ensure it has the same x,y coordinates as the center of the wall and then set the z-coordinate higher than the wall height.
If you want to show it vertically (like a clock hanging from the wall), you need to set the xY coordinates carefully so it does not display inside the wall. Z coordinate half way of the wall height. And then you need to do a trick to turn the text: put it into a group and rotate the group. The example model "Airport terminal" does that, check it out

Draw images on a canvas next to each other without space in flutter

I'm creating a game in flutter. I'm using a tilesheet to render tiles. I chop the image in 50x50 sections and then render them depending on their surrounding.
The tiles are rendered at the position of their corresponding "game object". In order to keep the code clean from details about converting from the game world positions and sizes to actual screen sizes my painting classes always obtain world space and then scales it up to screen space.
However, after scaling up the tiles I'm sometimes left with gaps between the tiles.
I've tried:
Placing the tiles at their appropriate screen position, without scaling the canvas first, in case the scaling produces the problem.
Adding borders around the tiles in the tilesheet in case the canvas.drawImageRect samples too many pixels.
Making the board take a size divisible by the number of tiles, in case there is a rounding error.
Drawing a subsection of the tile.
I can't seem to make the gap disappear. Do you have any suggestions?
Below is the relevant drawing code, where size and position are in world space and frameX and frameY is the positions to extract from the spritesheet.
void drawFrameAt(int x, int y, Canvas canvas, Offset position, Size size) {
var frameX = x * frameWidth;
var frameY = y * frameHeight;
var rect = Rect.fromLTWH(frameX, frameY, frameWidth, frameHeight);
var width = size.width;
var height = size.height;
var destination = Rect.fromLTWH(position.dx, position.dy, width, height);
canvas.drawImageRect(image, rect, destination, Paint());
}

Draw line relative to Image position and scale

I am making a game in Unity and I have a Canvas set to Screen Space - Overlay and it has that background green as you can see from the image. I also have a RawImage with the background blue as a child of the Canvas and the player will have to draw an object like a car or a house inside the RawImage.Right now, the line Instantiates in a random position on the screen even if I give it the coordinated (0, 0).
What I need help with is to find a way to convert the coordinates so I only work with the inside coordinates of the RawImage. For example, the (0, 0) coordinate I will pass to the line, has to be converted to the bottom-left corner of the RawImage. and the maximum width or height should also depend on the RawImage width and height.
Here is the CreateLine function I have for now:
public void CreateLine(int x, int y)
{
currentLine = Instantiate(linePrefab, Vector3.zero, Quaternion.identity);
currentLine.transform.parent = GameObject.Find("GameMenu(Clone)").transform; //Make the line a child of the Canvas named GameMenu
lineRenderer = currentLine.GetComponent<LineRenderer>();
lineRenderer.SetPosition(0, new Vector3(x, y, 99));
lineRenderer.SetPosition(1, new Vector3(x, y, 99));
}
Any amount of help will be greatly appreciated. Have a great day!

How to draw a filled polygon

How to draw a filled polygon in flutter?
Currently I am able to a eyebrow shape with array of point like below.
Path leftEyePath = Path();
leftEyePath.moveTo(leftEye[0].getX(),leftEye[0].getY());//starting point
for(int i=1;i<leftEye.length;i++){
leftEyePath.lineTo(leftEye[i].getX(),leftEye[i].getY());
}
canvas.drawPath(leftEyePath,painter);
The code above will eventually draw a connected polygon with many line , but how do I draw a filled polygon with color I want?
EDIT:
I am currently go through their function and found drawShadow(..)
canvas.drawShadow(leftEyePath, Colors.orange[600],0,true);
Unfortunately it only draw transparent color.
Below is my current output.
You may change your paint style into PaintingStyle.fill. The paint brush will automatically fill up the closing area of your path.
Paint _filledPainter = new Paint()
..style = PaintingStyle.fill
;

How to place text on foreground of rectangle

I have existing pdf document with white rectangle at top right coner. Hidden textfield is placed on rectangle. I have to put some text on the rectangle instead of hidden field. Here is my C# code:
public void setTextOverField(String sFieldName, String sText)
{
IList<AcroFields.FieldPosition> positions = this._stamper.AcroFields.GetFieldPositions(sFieldName);
Rectangle rect = positions[0].position;
PdfContentByte cb = this._stamper.GetOverContent(positions[0].page);
cb.BeginText();
cb.SetFontAndSize(this._font, this._fontSize);
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, sText, (rect.Left + rect.Right) / 2, (rect.Top + rect.Bottom) / 2, 0);
cb.EndText();
}
Now the text is hidden under the white rectangle. If I make the rectangle transparent the text become visible.
How to bring text on foreground?