I have an applet which I wanted to convert from AWT to Swing,
which went pretty well except for one thing.
The applet window is divided into two parts:
1) Top part is a Panel/JPanel with some labels, buttons, etc.
2) Bottom part is just drawing area (drawRect, fillRect, that sort of thing).
Part of the applet is 'drag and drop' whereby shapes are moved around
by dragging them with the mouse.
In order to eliminate flicker while doing that, I use an image buffer for the painting.
It all works fine with AWT.
With Swing, it seems I have two choices:
a) No panel appears
b) Panel appears, but flickers every time mouse is moved.
The difference is a call to repaint() for the Panel itself,
which wasn't necessary with AWT.
I tried making the JPanel a (regular) Panel,
which stopped the flicker problem, but caused some others -
like the controls added to it were painted incorrectly,
and some of them ceased functioning.
Any ideas?
Here is a simplified version of the applet.
See the line marked with ***** for the focus of my question.
It doesn't seem to matter where I try to put it, the results are the same.
The panel just has one button, and you can drag the blue rectangle around with the mouse.
/* <applet code="sw1.class" codebase="./" align="baseline" width="600" height="600"> alt="Applet not running" Applet tag ignored </applet> */ import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class sw1 extends JApplet implements MouseMotionListener { JPanel p; JButton b; private Image im; private Dimension imsize; private Graphics g2; public void init() { setLayout(null); p = new JPanel(); b = new JButton("Yo! A button"); p.setBounds(50,50,160,40); p.add(b); setBackground(Color.gray); add(p); addMouseMotionListener(this); } int x = 50, y = 100; public void paint(Graphics g) { Dimension d = getSize(); if ( (im == null) || (d.width != imsize.width)|| (d.height !=imsize.height) ) { im = createImage(d.width,d.height); imsize = d; g2 = im.getGraphics(); } g2.setColor(Color.gray); g2.fillRect(0,0,d.width, d.height); // Need this or dragged image gets left behind and replicated. // ***** p.repaint(); // ***** This is the line in question: without it, no panel, with it, flickering panel. // ***** paint2(g2); g.drawImage(im,0,0,null); } public void paint2(Graphics g) { g.setColor(Color.blue); g.drawRect(x, y, 100, 60); } public void mouseMoved(MouseEvent e) { } public void mouseDragged(MouseEvent e) { x = e.getX(); y = e.getY(); repaint(); } }