Java的swing库中的paintComponent()函数有时会被repaint()调用,有时不会

问题描述 投票:0回答:0

我正在为计算机图形学课程做一个项目。大部分工作是由 Java 的 Swing API 的 paintComponent() 函数完成的。

我在另一个类中有一行代码使用 repaint() 调用我创建的 JPanel 类中的 paintComponent() 函数,称为 Display2。在整个学期中,有时当我运行程序时它会调用 paintComponent() 函数,有时则不会。我的编译器和运行名为 paintComponent() 的程序的次数足以使我能够完成作业。我只是假设这是我必须解决的编译器或 IDE 的某种故障。但是,最近它从不调用 paintComponent() 并且我在处理作业时遇到了麻烦。我正在使用 JGrasp,想知道这是否与我的问题有关。

我附上了应该使一切正常工作的代码:

最后几行诊断打印语句,用于调用 paintComponent() 之前的函数调用

“显示初始化

Gouraud 叫

展示”

前几行和 paintComponent() 函数定义:

    
    public void paintComponent (Graphics g) {
     super.paintComponent(g);// panel for graphics
   
   
     
     int polyScannedThrough = 0; 
            System.out.println("polygons to scan through : " + polygons.length); 
         
        for (int i = 0; i < polygons.length; i ++) {// iterate through polygons
                   



           if (polyScannedThrough < polygons.length) {
          

类 Display2 的实例化:

public Display2 () {
 
 
                     this.setVisible(true);
                      this.setBackground(Color.white);
                    imageBuffer = new int[800][600][4];
                    zBuffer = new int[800][600];
                   // normalBuffer = new Matrix[800][600];
                                        // intialize edge table and linked list 
                    this.setBackground(Color.WHITE);
                    System.out.println("display initialized"); 
                    
                   //  polygons = new Polygon[Transformation2.numPoly];
                    
                     
                     
                     Ks = 0.778; // values hard coded for now
                     
                     Kd = 0.035;
                     
                     Ka = 0.113;
                     
                     intensity = 0.22;
                     Iambient = 0.90;
                     light = new Matrix (new double[][] {{-5}, {4}, {10}, {1}});
                     
                     Random rand = new Random();
                     
                     polyColor = new Color (rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
                     for (int i = 0; i < imageBuffer.length; i ++) {
                        for (int j = 0; j < imageBuffer[i].length; j ++) {
                           for (int k = 0; k <3; k ++) {
                              imageBuffer[i][j][k] = 255;
                           }
                        }
                     }
                    /* mb = new JMenuBar();
                    
                     
                     JMenu fileMenu = new JMenu("File");
                     JMenu shadingMenu = new JMenu("shading"); 
                     
                     System.out.println("buttons added");

                        JMenuItem constantShading = new JMenuItem("Constant shading");
                        constantShading.addActionListener (a -> constant());

                     JMenuItem phongShading = new JMenuItem("Phong shading");
                              phongShading.addActionListener(a-> phong());
                     JMenuItem gouraudShading = new JMenuItem("Gouraud shading");
                      gouraudShading.addActionListener(a -> gouraud());
                 shadingMenu.add(constantShading);
                  shadingMenu.add(phongShading);
                  shadingMenu.add(gouraudShading);
                  
                 
                  mb.add(shadingMenu);
                  Transformation2.frame.setJMenuBar(mb);*/
                  // initialize specular terms
                  
                 gouraud();

                 
    }

创建 JPanel 子类并调用 paintComponent() 的另一个类中的代码:

   Display2 panel = new Display2();

      Container cPane = frame.getContentPane();
      cPane.add (panel);
           System.out.println("display"); 
   
      panel.repaint(); 

我期待着遍历多边形数组,一堆关于多边形扫描转换的打印语句,以及一些要在屏幕上绘制的彩色形状。

相反,屏幕是空白的,并且正如打印语句所示,我没有通过 Display2 的初始化。

java swing graphics paintcomponent jgrasp
© www.soinside.com 2019 - 2024. All rights reserved.