Before I answer Chris, a couple of things..
It was probably better to chase this up in
the group (where any of a score of people
might help you) rather than by dropping me
a note.
(shrugs) Firstly I may have simply been
too busy to give time to it, so may have
ignored it.. Second, just 'cos I offer you
some help in the forums, does not create some
'life bond' between us.
Lastly, it seems you are not aware of a
far better group for these type of GUI
and rendering problems, I do not know if
it has been mentioned yet..
<
http://www.physci.org/codes/javafaq.jsp#cljg>
There is also a better group for people
who are just learning Java..
<
http://www.physci.org/codes/javafaq.jsp#cljh>
** A warning to anybody who would dare
criticize the code I am about to post **
"Don't even *start* on me, with your
'it would be much more efficient to blit
an array to screen..' crap, damn-it!
You had the opportunity over the last *15*
days to demonstrate your cleverness in code.
So provide a better example if you will,
...if you can, but otherwise leave it out!!"
(harumph!)
Now.. (finally) my example that only writes
the grid once..
<sscce>
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
public class CadTest {
public static void main(String[] args) {
Frame frame1 = new Frame();
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setLocation(200, 200);
frame1.show();
}
}
class Frame extends JFrame {
public Frame() {
setSize(200, 200);
Panel panel = new Panel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
}
class Panel extends JPanel {
BufferedImage bi;
int width = 800, height = 800;
public Panel() {
addMouseMotionListener(new MouseMotionHandler());
}
private class MouseMotionHandler implements MouseMotionListener {
public void mouseMoved(MouseEvent event) {
x = event.getX();
y = event.getY();
repaint();
} public void mouseDragged(MouseEvent event) {
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
drawGrid(g2);
drawSquare(g2);
}
void drawGrid(Graphics2D g2) {
if (bi==null) {
System.out.println( "drawing grid" );
bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g1 = bi.getGraphics();
// initialise the background color for the size of the screen
g1.setColor( new java.awt.Color(220,220,220) );
g1.fillRect(0,0,width,height);
g1.setColor( new java.awt.Color(0,0,0) );
for (int i = 0; i < width/5; i++) {
for (int j = 0; j < height/5; j++) {
g1.drawLine(i * 5, j * 5, i * 5, j * 5);
}
}
} else {
System.out.println( "NOT drawing grid" );
}
// write the new graphic to screen in a single pass
g2.drawImage( bi, 0, 0, width, height, null );
}
void drawSquare(Graphics2D g2) {
g2.fill(new Rectangle(x, y, 20, 20));
} int x, y;
}
</sscce>
BTW - that was a good SSCCE *you* posted.. ;-)