Drawing to off-screen image (JPanel)

Hi guys I won't make any speeches and proceed to the problem right away. My problem is this. I have a two-dimensional field (matrix) of Colors 50x50. I want to draw this matrix to an off-screen image of the JPanel (that's what I thought will work. If you have better idea let me know, please.) So I'll create an instance of this off-screen image and begin to draw each Color as a small cell 5x5 by the

fillRect()

method. For drawing to this off-screen image I use Graphics of that image. This way I eventually create a picture 250x250 right ? (5*50 x 5*50). Now I get Graphics for JPanel itself and by using

drawImage()

method I draw this prefabricated image onto the JPanel. All this happens about thousand times in a matter of seconds. In my GUI I have three JPanels one for drawing that image, one for displaying current status of the procces and the final JPanel contains start button for launching this sequence of image prefabrication and consequent flaping on the JPanel. What happens is that when I press that start button and the image is beign drawn as it should... all other components "disappear" (I mean they don't disappear you just can't see them cos they're being redrawn I guess). I can provide runnable JAR if you feel like puting up with it. (that would be much appreciated :) By this I don't "encourage" you to rewrite my code. It's only to serve as demonstration of how the program behaves.

http://www.2shared.com/file/5633069/d51a3981/SOM.html

(JAR includes source *.java files) and here's problematic code:


private void jButtonLearnActionPerformed(java.awt.event.ActionEvent evt) {
 Double lRate = Double.parseDouble((String) jComboBoxLRate.getSelectedItem());
        Integer iterations = (Integer) jSpinnerIters.getValue();
        somTrainer = new Trainer(SOM, iterations, lRate);

        Graphics panelG = jPanelSOM.getGraphics();
        BufferedImage offIm = new BufferedImage(250, 250, BufferedImage.TYPE_3BYTE_BGR);
//                jPanelSOM.createImage(250, 250);
        Graphics offImG = offIm.getGraphics();
        Color color;

        for (int i = 1; i <= iterations; i++) {
            color = randColor();

//            for (Color color : colorList) {
                somTrainer.update(i, color);
                updtStatus(somTrainer);
                for (int x = 0; x < 50; x++) {
                    for (int y = 0; y < 50; y++) {
                        offImG.setColor(SOM.getNode(x, y).getWeights());
                        offImG.fillRect(x * 5, y * 5, 5, 5);
                    }
                }
                panelG.drawImage(offIm, 12, 35, null);

//            }
        }

I also tried to put the exucutive part of the code to the paintComponent() method, but it didn't work. Please help, I already tried to ask different people but no luck hope u guys can crack it somehow :) Thanks Jacob

Recommended
Join the discussion
Be the first to comment on this article. Our Commenting Policies
See more