This is a result of the Applet
not begin buffered in any way.
You should employ some kind of double buffering...
For example...
@Override
public void paint(Graphics g) {
BufferedImage bi = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bi.createGraphics();
super.paint(g2d);
g2d.setColor(Color.LIGHT_GRAY);
g2d.fillRoundRect(s.getx(), s.gety(), 10, 30, 10, 10);
g2d.setColor(Color.GREEN);
g2d.fillRoundRect(s.getx() - 5, s.gety() + 10, 5, 20, 10, 10);
g2d.fillRoundRect(s.getx() + 10, s.gety() + 10, 5, 20, 10, 10);
g2d.dispose();
g.drawImage(bi, 0, 0, this);
}
Now, because it's pretty safe to assume the size of the applet won't change a lot, you could create a class instance of the BufferedImage
and reuse it, for example...
private BufferedImage backingBuffer;
@Override
public void invalidate() {
backingBuffer = null;
super.invalidate();
}
@Override
public void paint(Graphics g) {
if (backingBuffer == null) {
backingBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
}
Graphics2D g2d = backingBuffer.createGraphics();
super.paint(g2d);
g2d.setColor(Color.LIGHT_GRAY);
g2d.fillRoundRect(s.getx(), s.gety(), 10, 30, 10, 10);
g2d.setColor(Color.GREEN);
g2d.fillRoundRect(s.getx() - 5, s.gety() + 10, 5, 20, 10, 10);
g2d.fillRoundRect(s.getx() + 10, s.gety() + 10, 5, 20, 10, 10);
g2d.dispose();
g.drawImage(backingBuffer, 0, 0, this);
}
I would also question the need/use of extending from an Applet
. If you were to use JApplet
, you could use a JPanel
as you base canvas, override it's paintComponent
method and gain automatic double buffering via the Swing API...