Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Rendering text more than once in the same position

10 views
Skip to first unread message

Qu0ll

unread,
Nov 22, 2008, 2:59:45 AM11/22/08
to
Why is it that the text appears smudged when you use drawString() to render
the same text in the same position twice or more without clearing the
background? I would have thought that it's the exact same pixels affected
and therefore the text would still look as sharp as if it were only rendered
once. I am suspecting it has something to do with antialiasing. Correct?

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
Qu0llS...@gmail.com
[Replace the "SixFour" with numbers to email me]

John B. Matthews

unread,
Apr 27, 2011, 11:50:49 AM4/27/11
to
To: comp.lang.java.gui
In article
<4927bbf2$0$7520$5a62...@per-qv1-newsreader-01.iinet.net.au>,
"Qu0ll" <Qu0llS...@gmail.com> wrote:

> Why is it that the text appears smudged when you use drawString() to render
> the same text in the same position twice or more without clearing the
> background? I would have thought that it's the exact same pixels affected
> and therefore the text would still look as sharp as if it were only rendered
> once. I am suspecting it has something to do with antialiasing. Correct?

I am unable to reproduce this at any scale on Mac OS 10.5 Java 1.5.

<code>
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TransTest extends JFrame {

private class MyPanel extends JPanel {

@Override
public void paintComponent(final Graphics g) {
final Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
final double scaleX = this.getWidth() / 100.0;
final double scaleY = this.getHeight() / 100.0;
final BasicStroke stroke = new BasicStroke(4f,
BasicStroke.CAP_ROUND , BasicStroke.JOIN_ROUND);
g2d.setStroke(stroke);
final AffineTransform at = new AffineTransform(
AffineTransform.getScaleInstance(scaleX, scaleY));
at.translate(10, 10);
g2d.transform(at);
g2d.setColor(new Color(128, 64, 32));
g2d.drawRect(0, 0, 80, 80);
line(g2d, 0, 0, 80, 80);
line(g2d, 0, 40, 80, 40);
line(g2d, 40, 0, 40, 80);
line(g2d, 0, 0, 80, 80);
line(g2d, 80, 0, 0, 80);
g2d.setColor(Color.darkGray);
g2d.drawString("Test/", 5, 65);
g2d.drawString("Test\\", 5, 65);
}
}

private void line(Graphics2D g2, int x1, int y1, int x2, int y2) {
Color color = g2.getColor();
g2.setColor(color.brighter());
g2.drawLine(x1, y1, x2, y2);
}

public TransTest() {
this.setLayout(new BorderLayout());
final MyPanel panel = new MyPanel();
this.add(panel, BorderLayout.CENTER);
panel.setBackground(Color.WHITE);
this.pack();
this.setSize(543, 567);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
}

public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new TransTest().setVisible(true);
}
});
}
}
</code>

--
John B. Matthews
trashgod at gmail dot com
http://home.roadrunner.com/~jbmatthews/

---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Qu0ll

unread,
Nov 22, 2008, 2:31:14 PM11/22/08
to
"John B. Matthews" <nos...@nospam.invalid> wrote in message
news:nospam-2AED2A....@news.motzarella.org...

[...]

> I am unable to reproduce this at any scale on Mac OS 10.5 Java 1.5.

Try the following. The first line of text appears smudged with Java 6u10.

[code]


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import java.awt.Font;


import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TransTest2 extends JFrame {

private class MyPanel extends JPanel {

@Override
public void paintComponent(final Graphics g) {
final Graphics2D g2d = (Graphics2D)g;
g2d
.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

g2d.setColor(Color.darkGray);
g2d.setFont(new Font("Arial", Font.PLAIN, 24));


g2d.drawString("Test", 5, 65);
g2d.drawString("Test", 5, 65);

g2d.drawString("Test", 5, 95);
}
}

public TransTest2() {


this.setLayout(new BorderLayout());
final MyPanel panel = new MyPanel();
this.add(panel, BorderLayout.CENTER);
panel.setBackground(Color.WHITE);
this.pack();
this.setSize(543, 567);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
}

public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {

public void run() {
new TransTest2().setVisible(true);
}
});
}
}
[/code]

John B. Matthews

unread,
Nov 22, 2008, 3:59:57 PM11/22/08
to
In article
<49285e02$0$7519$5a62...@per-qv1-newsreader-01.iinet.net.au>,
"Qu0ll" <Qu0llS...@gmail.com> wrote:

> "John B. Matthews" <nos...@nospam.invalid> wrote in message
> news:nospam-2AED2A....@news.motzarella.org...
>
> [...]
>
> > I am unable to reproduce this at any scale on Mac OS 10.5 Java 1.5.
>
> Try the following. The first line of text appears smudged with Java
> 6u10.

Wait, I took pictures (click to enlarge):

<http://sites.google.com/site/trashgod/transtest3>

It _is_ a shade weightier, especially at low point size. I jammed up the
baselines to exaggerate the effect. I can't tell if it's a bug or a
feature: it reminds me of overstriking a daisy wheel to get bold.

I wonder what yours looks like (hint, hint).

[...]

John B. Matthews

unread,
Nov 22, 2008, 4:17:35 PM11/22/08
to
In article <nospam-C0F823....@feeder.motzarella.org>,

"John B. Matthews" <nos...@nospam.invalid> wrote:

> In article
> <49285e02$0$7519$5a62...@per-qv1-newsreader-01.iinet.net.au>,
> "Qu0ll" <Qu0llS...@gmail.com> wrote:
>
> > "John B. Matthews" <nos...@nospam.invalid> wrote in message
> > news:nospam-2AED2A....@news.motzarella.org...
> >
> > [...]
> >
> > > I am unable to reproduce this at any scale on Mac OS 10.5 Java 1.5.
> >
> > Try the following. The first line of text appears smudged with Java
> > 6u10.
>
> Wait, I took pictures (click to enlarge):
>
> <http://sites.google.com/site/trashgod/transtest3>
>
> It _is_ a shade weightier, especially at low point size. I jammed up the
> baselines to exaggerate the effect. I can't tell if it's a bug or a
> feature: it reminds me of overstriking a daisy wheel to get bold.
>
> I wonder what yours looks like (hint, hint).

D'oh, of course it's smudged: anti-aliasing is a function of the
background, which changes on each pass. Try a dozen or so to see the
effect.

Qu0ll

unread,
Nov 22, 2008, 9:50:55 PM11/22/08
to
"John B. Matthews" <nos...@nospam.invalid> wrote in message
news:nospam-D5C41C....@feeder.motzarella.org...

As I suspected, antialiasing was the cause - I just didn't know why. Now
it's obvious, thanks John.

Andrew Thompson

unread,
Nov 22, 2008, 11:22:02 PM11/22/08
to
On Nov 23, 1:50 pm, "Qu0ll" <Qu0llSixF...@gmail.com> wrote:
...

> As I suspected, antialiasing was the cause - I just didn't know why.  Now
> it's obvious, thanks John.

And now the question is answered, might you divulge
why on earth this text is being written twice? Seems
a waste of CPU cycles (before we even get to the
blurred effect).

--
Andrew Thompson
http://pscode.org/

Qu0ll

unread,
Nov 23, 2008, 4:51:28 AM11/23/08
to
"Andrew Thompson" <andrew...@gmail.com> wrote in message
news:75e874f6-5fed-4fd6...@a37g2000pre.googlegroups.com...

It's complex. I have a situation where text is being drawn onto a
translucent graphical object which in turn is sitting in front of an
animation. I want the text to be superimposed over the graphic but the
graphic sometimes gets re-rendered when part of the animation crosses under
it. Basically I keep having to redraw the text because I cannot be sure
when the underlying graphic is re-rendered which clears the text. I could
force a repaint of the graphic but then I would have to repaint that part of
the animation and the entire surrounding panel, neither of which the text
knows anything about.

Andrew Thompson

unread,
Nov 23, 2008, 6:43:29 AM11/23/08
to
On Nov 23, 8:51 pm, "Qu0ll" <Qu0llSixF...@gmail.com> wrote:
..
> ...Basically I keep having to redraw the text because I cannot be sure

> when the underlying graphic is re-rendered which clears the text.

Odd situation. In that event I would paint the string
to a BufferedImage (with the appropriate transparency)
and paint the BI each time.

OK - you probably figured that already, but I just
thought it worth stating for later searches.

0 new messages