Skip to main content

Aligning text

4 replies [Last post]
mpymm
Offline
Joined: 2007-12-12
Points: 0

Hi,

I'm trying to have a SGText node positioned so it's right aligned to a point in the graph. I think the idea is to use a SGAlignment transform with horizontal alignment set to TRAILING but there are a couple of issues. When the text changes it appears to jitter a bit - I'm rendering numbers and if the text changes from 100 to 200 it shifts a bit (I'm guessing the fractional width is changing but somehow it's being rendered starting at a pixel boundary). If the text gets longer (for example 100 to 10000) the bounds don't appear to change properly and only the rightmost part of the text renders. Can anyone suggest a good way of doing this?

Thanks
Michael

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mpymm
Offline
Joined: 2007-12-12
Points: 0

I tried this out on the 0.5 release but the same thing - the text jitters and is not repainted properly.

Thanks
Michael

campbell
Offline
Joined: 2003-06-24
Points: 0

Hi Michael,

Apologies for the lack of a response. I looked into this a bit last week and it appears that there are two separate problems: one is a bug in the dirty region accumulation that we'll need to dig into, and the other (the jitter) may be a rounding issue with SGText bounds. We'll get these into our bug database and work on a fix shortly.

Thanks,
Chris

mpymm
Offline
Joined: 2007-12-12
Points: 0

Thanks Chris, that sounds great. I'm really liking scenario so far - I've written more paintComponent overrides in the past few years than I can count and this is shaping up to be a great new technology for building UIs.

mpymm
Offline
Joined: 2007-12-12
Points: 0

Here's an example program that demonstrates this - it should cycle between showing 100, 200, 20000 with the rightmost two zeros in the same location. However the number shifts a pixel to the right going from 100 to 200 and the 20000 shows "000". This is 1.6.0_02 on Windows XP SP2.

[pre]
public class Test {
public static void main(String[] args) {
new JFrame("test") {
{
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JSGPanel p = new JSGPanel();
p.setPreferredSize(new Dimension(200, 200));

// set up scene
SGGroup root = new SGGroup();
final SGText text = new SGText();
text.setText("HI");
text.setFont(Font.decode("Tahoma-11"));
text.setFillPaint(Color.BLACK);
text.setAntialiasingHint(RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
SGAlignment align = new SGAlignment();
align.setChild(text);
align.setHorizontalAlignment(SwingConstants.TRAILING);
root.add(SGTransform.createTranslation(100, 100, align));
p.setScene(root);

// timer
Thread t = new Thread() {
int i;
public void run() {
while (true) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
text.setText(i == 0 ? "100" : (i == 1 ? "200" : "20000"));
i = (i + 1) % 3;
}
});
try {
Thread.sleep(500);
} catch (InterruptedException e) {
//
}
}
};
};
t.setDaemon(true);
t.start();

// layout
getContentPane().add(p, BorderLayout.CENTER);
pack();
}
}.setVisible(true);
}
}
[/pre]