Although I have programmed for several decades I am new to Java. I decided
to learn it to create Applets to use in web pages I make as a volunteer in
the computer club at a seniors' centre.
My current learning project is to divide a picture into rows and columns of
squares, then redisplay the picture with these squares positioned randomly
in the rows and columns.
In an applet I have stored the original picture as an image, divided it up
into the squares (each containing part of the picture), and stored the
images of each of these squares in an array of images. Since there are 48
squares I have also randomized the order of 48 numbers (0 to 47) and then
used these numbers with some simple arithmetic to create random positions
(rows and columns) for each of the squares. I wish to use these random rows
and columns to display each of the squares in the applet's display area.
When I try this, some of the squares display okay but others do not get
displayed. By putting a delay between the displaying of each square what
appears to happen is that some squares display in their random positions,
then the display area clears and then others display in their random
positions.
I have tried putting
public void update(Graphics g) {
paint(g);
}
in my program to stop the clearing but this does not work. So I am now
trying to use double buffering to display the squares at random positions in
a buffered image rather than on the screen and then, after all squares have
been drawn, transfer the buffered image to the applet display area. The
problem is that the Java book I am using seems to be outdated and all the
code examples I find on the Internet have so much in them that I cannot
(with my novice understanding of Java) extract the useful part of the code
that does the double buffering. (I also have a novice's incomplete
understanding of how paint and repaint work.)
Could anyone provide the code which will let me create a buffered image,
then draw the various picture squares to this image at their randomized
positions, and then redraw the completed image of squares to the screen
display area? (I already have the row and column positions for the picture
squares and the actual picture squares.) As a novice I also need to have
some instruction on where to put the above coding in the structure of the
applet. I currently get the original picture, divide it into squares and
determine the randomized positions in a 'public void init()' with the
squares and positions stored in arrays. Does the double buffering have to
take place in the 'public void paint(Graphics g)' area?
Thank you.
Doug van Vianen