How set Libgdx bitmap font size? - java

I'm rendering on screen the game fps using bitmap font but there are no methods for the size. This is a problem for me because my camera viewport size is very small so the text when rendered is huge and pixelated.
font.draw(batch, Float.toString(Gdx.graphics.getFramesPerSecond), x, y);

Did you try setScale() method that what i use to resize my font
myFont.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
myFont.setScale(scale);
if you have trouble, leave a comment
Good Luck !!
Edit :
With the latest libgdx version, try to scale font like this :
myFont.getData().setScale();

I often use what minos23 suggested. But the downfall is that it can look pixelated especially when scaling upwards. A fancy large bitmap font can take up a lot of space and if you need many different fonts you might go over your budget.
By using Gdx.Freetype you are able to create bitmapfonts at runtime from small .ttf files. This means you only need to ship the .ttf files with your app and can generate a font based on user settings like resolution.
Other then scaling and the freetype solution is having multiple bitmaps of different font sizes. This way your fonts stay crisp all time but at the cost of runtime performance to generate the proper fonts.

setScale is the function to use. Please note that with the newest LibGDX version (this changed earlier though) you need to do this isntead:
font.getData().setScale(2, 2);
before it was enough to do:
font.setScale(2, 2);
The first number in setScale is the X scale, and the second is the Y scale.

I use setScale() function too as others to reduce the font size, but here I want to offer another solution and I have a question. Why you don't use FPSRenderer instance or why you don't draw your fps label on another batch which projection matrix has the screen size?

Netero's answer works.
If you want to make code cleaner, you can write a helper function:
public static void setLineHeight(BitmapFont font, float height) {
font.getData().setScale(height * font.getScaleY() / font.getLineHeight());
}

Related

Which image formats can be resized in JavaFX

There are about 100 jpeg & png color images used in our JavaFX-built desktop app which, when the window is resized, become stretched and blurry so I'd like to have all the graphics remade in a format that will allow them to be dynamically resized without losing quality. What image format or procedure should be used to do this?
Currently, each image is simply in an ImageView and resized as follows, but I'm open to other suggestions:
if(isSmall){
Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
double sh = visualBounds.getHeight();
Scale scale = new Scale(sh, sh, 0, 0);
root.getTransforms().setAll(scale);
}
As has already been mentioned SVG is probably the way to go for you. JavaFX does not support SVG directly but you can find support here
javafxsvg and here svg-to-fxml-converter for example.
You can't resize an image to be bigger than it is without it getting blurry for most common formats. Instead make sure your images are big enough so you only need to downscale them.
The only format I ever heard of that could upscale further was using fractal compression, but AFAIK it is not in common use.

Libgdx Pixellated GUI Effect

In LibGDX, I am currently trying to achieve the effect of a pixellated GUI, meaning the buttons are pixellated, like an 8-bit theme.
When I make my GUI elements, such as a TextButton, I am using images that are small, say 34x16 pixels, but I am rendering them on a large resolution like 1920x1080. What is the best way to render such a small image at a high resolution?
My thoughts were to use stage.setViewport(WIDTH,HEIGHT), and set the width and height to a scaled down resolution, so the gui elements wouldn't be so big. This works, however when adding elements things go wrong.
This my stage/table currently, using a scaled down resolution. I am adding widgets using to the table like this:
table.add(playButton);
table.row();
table.add(title);
but as you can see, the table.row() makes a row that is much too large, perhaps a row fit for a larger resolution. Is there any way to change the gap that is made by the row? I would have thought that the gap would be zero, and by using table.add().pad() you could change the gap, but I am not doing any of that.
Any help would be greatly appreciated.
Ninepatch
This is a common issue in all apps. To overcome this, images which can be safely resized in parts are used. they are called ninepatches.
Here is a nice Tutorial about using them in libgdx.
Distance Field Fonts
Although you haven't mentioned it here, you'd also find font sizing (pixellated fonts) as an issue. For that Distance Field Fonts are used.
Hope this helps.
I would say don't worry about scaling them up and making the virtual resolution bigger. If you want to see picture still pixelated when you scale it use Texture filter. For your case you want to use Nearest filter. use it like this:
yourTexture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
where yourTexture is the texture that you have all your bitmaps and skin elements on. If you are using texturePacker than you can specify the filter before packing too. Or just open the .pack file and on the top you will see filtering options, and edit those to Nearest.

Android / eclipse: Can a layout content be scaled to the screen size?

I am discovering android programming, I created a simple layer and I'm drawing some sprites like that:
Matrix matrix = new Matrix();
matrix.setTranslate(xPos, yPos);
canvas.drawBitmap(mysprite, matrix, null);
But depending on the screen size, everything is not scaled like it would.
Must I scale myself using the screen demensions or is there a parameter for doing that automaticaly.
Thanks
As far as I know there is no way of automatically scaling your sprites, the closest you can get is dp, which firstly I don't know if it is possible to size things using dp in a game engine, and secondly it is not the best way (because I have found that it simply doesnt change enough) to support multiple screen sizes.
This would be a good place to start however, as it covers different easy(ish) methods to support multiple screen sizes - http://developer.android.com/guide/practices/screens_support.html

How might I enlarge image in java without losing quality?

I am working with images in Java. I have a set of images - let's say 600x800 pixels each.
I resize them at 100x100 and I make some stuff on it. Now I would like to enlarge the image at the beginning size without losing my changing and pixel quality. Is this possible?
No. This also isn't really a Java question. How would you do this with an image editor? If you resize twice (especially smaller than larger), you're going to lose quality.
Your best bet is to keep the native resolution, then use vector graphics to draw what you need - eliminating any unnecessary resizing. (I.E., calculate what you need to draw, taking into account the current size - without first resizing to 100x100.) This will also fix some issues you're also probably seeing regarding the aspect ratios - as when you would resize from 100x100 back to 600x800, whatever you added is going to appear "stretched" / wider.
I would like to enlarge the image at the beginning size without losing my changing and pixel quality. Is this possible?
Yes, it's possible and very simple.
Here's how you do it:
use your original.

Create image file with java

I was trying to create a rectangular image where will be 2 things.
A label (100% width and 20% height)
A Text Area (100% width and 80% height)
So lablel will be at top and text area will be at bottom
so I tried to create a JFrame and place both components there. Then I am creating its image with JFrame.createImage(width, height) and in last I used ImageIO.write(). but problem was, There was some extra blank space around the components in Image. When I tried to set its bound then it create an exact image but this image works perfectly on Windows but it doesn't work on Mac. Any idea why?
Is there also another easy way where I can achieve this. I've spent 2 days but couldn't found any solution.
Please advice
Thanks in adnvace
Rendering using Swing components is very versatile, but the user interface delegate for each component varies by platform. Similarly, font metrics also vary by platform and vendor. If you need very fine control over the placement of text, you can access the graphics context's font metrics as seen in this example, and you can mitigate aliasing as shown here.

Categories

Resources