Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #36085
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? |
| Date | 2016-12-11 20:48 -0500 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <o2kvkk$1i8r$1@gioia.aioe.org> (permalink) |
| References | <75624c6e-6a78-4e8a-82a4-4ff2556cc241@googlegroups.com> <5bda0936-09ee-4eea-8001-e0ad17f31b0b@googlegroups.com> |
On 12/9/2016 2:35 PM, The Kreepy Gamer wrote:
> public class Fonts {
> public Fonts() {
> JFrame window = new JFrame("Test");
> window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> window.setSize(300,300);
>
> label.setHorizontalAlignment(JLabel.CENTER);
>
> window.add(label);
> window.setVisible(true);
> }
>
> public static void main(String[] args) {
> new Fonts();
See Eric Sosman's comments about EDT.
And fix it.
The behavior of the code is unpredictable.
I don't think it impact this problem though.
> Font compFont = comp.getFont();
> String compText = text;
> int stringWidth = comp.getFontMetrics(compFont)
> .stringWidth(compText);
> int compWidth = comp.getWidth()-75;
>
> if (stringWidth == 0) {
> stringWidth++;
> }
>
> while (compWidth < 0) {
> compWidth++;
> }
>
> double widthRatio = (double)compWidth / (double)stringWidth;
>
> int newFontSize = (int)(compFont.getSize() * widthRatio);
> int compHeight = comp.getHeight();
>
> int fontSizeToUse = Math.min(newFontSize, compHeight);
> if (fontSizeToUse < 1) {
> return new Font(compFont.getName(), Font.PLAIN, 1);
> }
> return new Font(compFont.getName(), Font.PLAIN, fontSizeToUse);
There is any guarantee that this algorithm will be stable.
And in fact it keeps jumping (between 17 and 18 on my PC).
There are a couple of possible fixes:
1)
replace:
int newFontSize = (int)(compFont.getSize() * widthRatio);
with:
int newFontSize = (int)(compFont.getSize() * widthRatio + 0.5);
(or use Math.round)
It solves the problem on my PC. But I don't think it guarantees
stability either.
2)
replace:
int newFontSize = (int)(compFont.getSize() * widthRatio);
with:
int newFontSize = (int)(compFont.getSize() * (widthRatio/2 + 0.5));
I think this is a good change as I believe it in general should make
it more stable, but I still don't think it guarantees.
3)
But maybe with an extra check.
replace:
int newFontSize = (int)(compFont.getSize() * widthRatio);
with:
int newFontSize = (int)(compFont.getSize() * (widthRatio/2 + 0.5));
if(newFontSize == oldFontSize) newFontSize = compFont.getSize();
oldFontSize = compFont.getSize();
I don't know if it is guaranteed to be stable, but I am rather
confident that it will be in practice.
Arne
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? The Kreepy Gamer <tylerc2077@gmail.com> - 2016-12-06 15:21 -0800
Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-12-07 13:36 -0600
Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? The Kreepy Gamer <tylerc2077@gmail.com> - 2016-12-08 12:48 -0800
Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-12-08 15:30 -0600
Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Martin Gregorie <martin@address-in-sig.invalid> - 2016-12-08 22:17 +0000
Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Arne Vajhøj <arne@vajhoej.dk> - 2016-12-08 20:09 -0500
Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Gene Wirchenko <genew@telus.net> - 2016-12-09 09:07 -0800
Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Martin Gregorie <martin@address-in-sig.invalid> - 2016-12-09 20:01 +0000
Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Nigel Wade <nmw@local.domain> - 2016-12-09 10:54 +0000
Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? The Kreepy Gamer <tylerc2077@gmail.com> - 2016-12-09 11:35 -0800
Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-12-09 15:32 -0500
Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Martin Gregorie <martin@address-in-sig.invalid> - 2016-12-09 21:22 +0000
Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Martin Gregorie <martin@address-in-sig.invalid> - 2016-12-12 12:08 +0000
Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Arne Vajhøj <arne@vajhoej.dk> - 2016-12-11 20:48 -0500
Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? The Kreepy Gamer <tylerc2077@gmail.com> - 2016-12-12 11:18 -0800
Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-12-12 14:22 -0500
Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Arne Vajhøj <arne@vajhoej.dk> - 2016-12-12 20:15 -0500
csiph-web