I've searched all around for this problem, but can't find a solution.
This is my render loop:
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
BitmapFont font = new BitmapFont(Gdx.files.internal("data/Media/font/myfont1.fnt"), false);
font.setColor(new Color(1, 1, 1, 1));
font.draw(batch, "Hello", 100, 100);
batch.end();
I've tried all possible colors, positions and different programs for generating fonts.
But the result is always the same: A black screen! (when glClearColor is (1, 1, 1, 1), a white screen...) Can anyone tell me what is wrong?
Thanks in advance!
EDIT:
I found the problem myself. It was a badly set up camera!
Seems that uhave not loaded the png file along with the fnt file
font = new BitmapFont(Gdx.files.internal("data/billy.fnt"), Gdx.files.internal("data/billy.png"), false);
And please never try to load anything in render method.
Try to load the font in the constructor or else u will end up with a G.C call and f.p.s will eventually drop down
You can initallize a new BitmapFont without having .fnt in your project.
BitmapFont font = new BitmapFont();
then you render it:
batch.begin();
font.draw(batch, "Hello world", 200, 0);
batch.end();
don't forget that the Y axis starts from the bottom!
font = new BitmapFont(Gdx.files.internal("data/billy.fnt"), Gdx.files.internal("data/billy.png"), false);
If you have few fonts images for example billy.fnt, billy_1.png, billy_2.png you can just use:
font = new BitmapFont(Gdx.files.internal("data/billy.fnt"), false);
create a .fnt file using hiero which is provided by libgdx website
set the size of font 150 ,it will create a .fnt file and a png file
copy both of file in your assests folder
now declare the font
BitmapFont font;
nw in create method
font = new BitmapFont(Gdx.files.internal("data/rayanfont.fnt"), false);
//rayanfont is the font name you can give your font any name
in render
batch.begin();
font.setscale(.2f);
font.draw(batch, "hello", x,y);
batch.end();
this will work smoothly
Related
i want to generate a pdf label for CD using java itext. i have drawn the circle but i am unable to set image and multiple paragraphs inside the circle.
Below is the code snippet.code snippet
String printingPath = "CD_label.pdf";
Document document = new Document(new Rectangle(PageSize.A4));
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(printingPath));
document.open();
PdfContentByte cb = writer.getDirectContent();
cb.setRGBColorFill(0xFF, 0xFF, 0xFF);
BaseColor colorval = new BaseColor(102,178,255);
cb.setColorStroke(colorval);
cb.circle(300.0f, 650.0f, 150.0f);
cb.circle(300.0f, 650.0f, 20.0f);
cb.stroke();
//cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA,BaseFont.CP1257,BaseFont.EMBEDDED), 10);
//cb.beginText();
//cb.resetRGBColorStroke();
//cb.setTextMatrix(320, 420);
//cb.showText("Text inside cd");
// ColumnText.showTextAligned(cb, Element.ALIGN_LEFT,new Phrase("Hello itext"),50, 700, 0); cb.endText();
Image img = Image.getInstance("Symbol.png");
img.setAbsolutePosition(270f, 740f);
img.scaleAbsolute(60, 34);
document.close();
Why don't you see your text?
You set the fill color to WHITE:
cb.setRGBColorFill(0xFF, 0xFF, 0xFF);
Text (usually) is drawn by filling glyph outlines defined in some font. Thus, your uncommented text drawing code
cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA,BaseFont.CP1257,BaseFont.EMBEDDED), 10);
cb.beginText();
cb.resetRGBColorStroke();
cb.setTextMatrix(320, 420);
cb.showText("Text inside cd");
ColumnText.showTextAligned(cb, Element.ALIGN_LEFT,new Phrase("Hello itext"),50, 700, 0);
cb.endText();
does draw text... in WHITE on WHITE...
If you remove that cb.setRGBColorFill instruction (or select a clearly different fill color), you'll see your text:
(The point (320, 420) clearly is outside a circle with center (300, 650) and radius 150, consequentially so is your "Text inside cd" text...)
Another issue: ColumnText.showTextAligned starts its own text object, so to create a valid PDF you must move it after your cb.endText():
cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA,BaseFont.CP1257,BaseFont.EMBEDDED), 10);
cb.beginText();
cb.resetRGBColorStroke();
cb.setTextMatrix(320, 420);
cb.showText("Text inside cd");
cb.endText();
ColumnText.showTextAligned(cb, Element.ALIGN_LEFT,new Phrase("Hello itext"),50, 700, 0);
Why don't you see your image?
Because you don't add it!
If you add it to your PdfContentByte cb
Image img = Image.getInstance("Symbol.png");
img.setAbsolutePosition(270f, 740f);
img.scaleAbsolute(60, 34);
cb.addImage(img);
the result becomes like this:
(I obviously don't have your image, so I use a simple example image instead.)
I want to write text on the screen for my game, for things like fps, random text for items and stuff. How can I write that text?
Is it possible without the Basic Game class? Isn't there a command like this g.drawString("Hello World", 100, 100);?
Update: this answer is now outdated, and does not work at all with the latest versions of LWJGL. Until I update this answer fully, I recommend that you look here: https://jvm-gaming.org/t/lwjgl-stb-bindings/54537
You could use the TrueType fonts feature in the Slick-util library.
Using a common font is easy, just create the font like this:
TrueTypeFont font;
Font awtFont = new Font("Times New Roman", Font.BOLD, 24); //name, style (PLAIN, BOLD, or ITALIC), size
font = new TrueTypeFont(awtFont, false); //base Font, anti-aliasing true/false
If you want to load the font from a .ttf file, it's a little more tricky:
try {
InputStream inputStream = ResourceLoader.getResourceAsStream("myfont.ttf");
Font awtFont = Font.createFont(Font.TRUETYPE_FONT, inputStream);
awtFont = awtFont.deriveFont(24f); // set font size
font = new TrueTypeFont(awtFont, false);
} catch (Exception e) {
e.printStackTrace();
}
After you have successfully created a TrueTypeFont, you can draw it like this:
font.drawString(100, 50, "ABC123", Color.yellow); //x, y, string to draw, color
For more information, you can look at the documentation for TrueTypeFont and java.awt.Font, and the Slick-Util tutorial I got most of this code from.
Try Making a BufferedImage of required size. Then get its Graphics and draw a String. Then Convert it to a ByteBuffer and render it in OpenGL.
String text = "ABCD";
int s = 256; //Take whatever size suits you.
BufferedImage b = new BufferedImage(s, s, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g = b.createGraphics();
g.drawString(text, 0, 0);
int co = b.getColorModel().getNumComponents();
byte[] data = new byte[co * s * s];
b.getRaster().getDataElements(0, 0, s, s, data);
ByteBuffer pixels = BufferUtils.createByteBuffer(data.length);
pixels.put(data);
pixels.rewind();
Now pixels contains the required Image data you need to draw.
Use GL11.glTexImage2D() function to draw the byte buffer.
I'm trying to set the background color of my QR Code using iText into a transparent background, however it does not work. Shows only white bars and black background.
What i have done so far:
My Code Snippet:
PdfContentByte cb = writer.getDirectContent();
BarcodeQRCode qrcode = new BarcodeQRCode("sample message on qr", 100, 100, null);
java.awt.Image qrImage = qrcode.createAwtImage(Color.WHITE,new Color(0, 0, 0, 0));
Image finalImage = Image.getInstance(writer, qrImage, 1);
finalImage.setAbsolutePosition(positionX, positionY);
cb.addImage(finalImage);
I have already generated my QR code and produced a PDF, however, when using
qrcode.createAwtImage(Color.WHITE,new Color(0, 0, 0, 0));
It does not produce an alpha background, instead it only shows a black background color.
I have also tried:
java.awt.Image qrImage =
qrcode.createAwtImage(Color.WHITE,Color.OPAQUE);
But obviously, my arguments are incorrect.
Help will be most appreciated, i've been working on this for a day now.
I have also tried Graphics, Graphics2g, converting it into BufferedImage.
Changing the assignment of finalImage to the following works:
Image finalImage = Image.getInstance(qrImage, null)
I don't know why using the getInstance method that takes a PdfWriter as first argument ruins the transparency, though...
I would solve this problem like this:
BarcodeQRCode qrcode = new BarcodeQRCode("sample message on qr", 100, 100, null);
Image image = qrcode.getImage();
Image mask = qrcode.getImage();
mask.makeMask();
image.setImageMask(mask);
document.add(image);
There may be an AWT solution too, but I'm more familiar with native PDF solutions than with using an AWT workaround.
I have a weird problem - I'm trying to write a text string over a transparent GIF picture.
I'm using awt Graphics2D object, and for some reason, I cannot affect the way the text looks - its color, its alignment and so on. Every time I use drawString with a string,
it is white and centered left.
Is this a problem with my jre? With GIF format? Or the fact that the picture is transparent?
I tried doing this: (TextAttributes is just some container class I created)
Map<TextAttribute, Object> fontAtts = new Hashtable<TextAttribute, Object>();
fontAtts.put(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL);
fontAtts.put(TextAttribute.FAMILY, TextAttributes.type);
fontAtts.put(TextAttribute.FOREGROUND, TextAttributes.color);
fontAtts.put(TextAttribute.SIZE, TextAttributes.size);
Font font = new Font(fontAtts);
BufferedImage image = getImage(picturePath, pictureName, format);\\just gives me the buffered image
Graphics2D g2 = image.createGraphics();
//first way:
g2.setFont(font);
g2.drawString("bla blah", 200,150)
//second way
g2.setPaint(TextAttributes.color);
g2.drawString("bla blah", 200,150)
//third way
g2.setColor(Color.RED);
g2.drawString("bla blah", 200,150)
//fourth way
AttributedString x = new AttributedString("blah blah", fontAtts);
g2.drawString(x.getIterator(),200,150)
but nothing works :(
I am re-sizing jpeg image from URL and store at some directory using JPEGImageEncoder in Java servlet.
Code is working fine in Our Development Solaris server. But it is storing image as black background color with square box.
Please help me for what can be the issue. Thanks in advance.
BufferedImage thumbImage = new BufferedImage(thumbWidth,
thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
int quality = Integer.parseInt(nquality);
quality = Math.max(0, Math.min(quality, 100));
param.setQuality((float)quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(thumbImage);
-Manoj
I got the solution.
Actually source image url was not accessible from Java code. That was the reason I was getting black image. We change the url to accessible and now it works fine.
Thanks.