Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java not calling component's overloaded paintComponent method

First this is part of a homework assignment to create a mosaic image generator. I want the program to repaint in realtime as it finds a image and place it ontop of another (source image).

This is code to create the panel in my main function.

The last piece mypanel.create() is the mosaic logic.

myPanel = new mosiacPanel(sourceFile, sizePercent, pixesize,threads, imageList);
//test.setText(Integer.toString(myPanel.getWidth()));
JFrame frame2 = new JFrame("COS 226 MOSIAC OF AWESOMENESS BY SLUIPMOORD && ELEANORASAURUSS");
myPanel.setVisible( true );
myPanel.repaint();
frame2.add(myPanel);
if(myPanel.getWidth() > menubar.getWidth()){
    frame2.setSize(myPanel.getWidth() , myPanel.getHeight() + menubar.getHeight() );
    frame2.repaint();
} else {
    frame2.setSize(menubar.getWidth() , myPanel.getHeight() + menubar.getHeight() );
}
frame2.setVisible( true );
//  myPanel.setLocation(170, 4);
myPanel.create();

Mosaic panel class code snippet

public void create()
{
    ph.createMosiac(imgUrls, this);
}

@Override
protected void paintComponent( Graphics g ) 
{  super.paintComponent(g); 
   g.drawImage( imgToPaint, 0, 0, null );
   // System.out.println("paint");
}

public void paintTile( BufferedImage img ) 
{

    imgToPaint = img;        
    this.repaint();
    // this.paintComponent(this.getGraphics());
}

I call the paintTile function within create Mosaic Function.

public void createMosiac(List<String> fileNames, mosiacPanel parent)
{
    ArrayList<TileImage> srcTiles = new ArrayList<TileImage>();

    for( int i = 0; i < fileNames.size(); i++ ) 
    {
        srcTiles.add( new TileImage( fileNames.get(i), tileSize ) );
    }

    for( int y = 0; y <= (this.getHeight() - tileSize); y += tileSize ) 
    {           
        for( int x = 0; x <= (this.getWidth() - tileSize); x += tileSize ) 
        {
            int location = 0;
            double  dist, high = 2147483647;
            for( int i = 0; i < srcTiles.size(); i++ ) 
            {
                dist = this.getTileImage(x, y).differance( srcTiles.get(i) );

                if( (dist < high) )
                {
                    high = dist;
                    location = i;
                }                       
            }

            this.setTileImage( x, y, srcTiles.get(location) );
            parent.paintTile(this);                   
        }            
    }                        
}

That is my program logic. When I uncomment this in the second snippet // this.paintComponent(this.getGraphics()); The program work but it repaints with a horrible flash and i am not one for medical bills when some of my other students within the demo venue are prone to epileptic attacks.

If I trace trace the paintComponent function it get called twice at the end of the program and not on every repaint.

Thank you in advance.

I added a source code you guys can just copy and run. Select a image you want to test with the default is not available currently because you guys don't have it

and then a directory containing a bunch of jpg to tile it with It is pretty slow at the moment i still need to fix that Google docs link to the java file

like image 555
Gerhard Janse van Rensburg Avatar asked Sep 01 '12 16:09

Gerhard Janse van Rensburg


Video Answer


1 Answers

try the repainting in an other thread. Maybe this will be solve your problem. Good Luck! :)

like image 163
Hydroid Avatar answered Oct 05 '22 20:10

Hydroid