Graphics and JVMs
To: comp.lang.java.gui
I have written this small test code to show the difference in the
graphical output of 2 different JVMs i.e. Apple JDK 1.5.0 and Windows
JDK 1.5.0_13. When I execute this code in Apple JVM, I can see the
message "Hi There" quite clearly. Whereas, in Windows JVM, the message
is harder be seen if I set Thread.sleep to 100. If I increase the
sleep period, I could see a blinking "Hi There" message. Is this has
to do with the way I have written the code (probably, I am missing
something here) or the JVMs implementation? Either way, how can make
this work the way I wanted it? Please advise. Thanks.
[CODE]
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class TestRandomBoxes extends JFrame {
private Random pointRandomizer = new Random();
public TestRandomBoxes() {
final JPanel p = new JPanel() {
@Override
public void paint(Graphics g) {
g.setColor(getBackground());
g.fillRect(0, 0, 200, 200);
g.setColor(Color.BLACK);
g.fillRect(50 + randomPoint().x, 50 +
randomPoint().y, 50, 50);
drawMessage(getParent(), "Hi There");
}
};
p.setPreferredSize(new Dimension(200, 200));
Thread animator = new Thread() {
@Override
public void run() {
while (true) {
p.repaint();
try {
Thread.sleep(100); // reduce to increase
update speed
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
animator.start();
getContentPane().add(new JScrollPane(p));
}
private void drawMessage(Container c, String message) {
if (message.length() < 1) {
return;
}
Graphics g = c.getGraphics();
int w = g.getFontMetrics().stringWidth(message) + 10;
int h = g.getFontMetrics().getHeight() + 4;
g.setColor(new Color(0x33, 0xFF, 0xFF, 50));
g.fillRect(5, 5, w, h);
g.setColor(new Color(0x33, 0xFF, 0xFF));
g.drawRect(5, 5, w, h);
g.setColor(Color.BLACK);
g.drawString(message, 8, (5 + h - 4));
}
private Point randomPoint() {
return new Point(pointRandomizer.nextInt(50),
pointRandomizer.nextInt(50));
}
public static void main(String[] args) {
TestRandomBoxes sl = new TestRandomBoxes();
sl.setVisible(true);
sl.pack();
sl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sl.setBounds(600,450, 100, 100);
}
}
[/CODE]
---
* 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