简体   繁体   English

Java没有调用组件的重载paintComponent方法

[英]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. 这是在我的main函数中创建面板的代码。

The last piece mypanel.create() is the mosaic logic. 最后一块mypanel.create()是镶嵌逻辑。

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 Mosaic面板类代码片段

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. 我在create Mosaic Function中调用了paintTile函数。

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()); 当我在第二个片段// 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. 如果我跟踪了paintComponent函数的跟踪,它会在程序结束时调用两次而不是每次重绘。

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 然后一个包含一堆jpg的目录用它来平铺它现在还很慢我还需要修复那个Google文档链接到java文件

try the repainting in an other thread. 尝试在另一个线程中重新绘制。 Maybe this will be solve your problem. 也许这将解决你的问题。 Good Luck! 祝好运! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM