Java Swing Not Rendering Correctly

2019-08-08 08:51发布

问题:

I have been working on a project. In one part of the project I needed to draw on JPanel. Graphics2D object is passed to the model part of the framework and model will draw whatever it needs to draw at that instance. Problem is JFrame is not rendered correctly. It looks half white shaded and not painting correctly. It works fine on my laptop (Sony Vaio, Windows7) but it's not rendering correctly in all other systems I tested (2 Dell laptops,windows8;1 Thoshiba,windows7;1 samsung, windows8). It's also rendering correctly on few other systems my teammate tested.

I'm sure there are no errors made in model part. Problem doesn't seem to be rendering on JPanel. But one information I can give is that we're doing our project in Netbeans. Perhaps the code generated by Netbeans IDE causing the problem? I checked the code but couldn't Identify the problem. It's difficult to post an SSCCE for this but I will share some screen shots of the issue.

Ok since I don't have enough reputation to post images see the links below....

[Displays Wrongly][1]
   [Displays Wrongly][2]
   [Displays correctly on my system][3]
   [Displays Correctly on my system][4]

I tried to debug it but can't know what the problem is.... Update It's not an SSCCE but it may help

Code for JFrame....

<pre>

import btapp.Question;
import btapp.qGens.QGenGeomAbstract;
import btapp.qGens.QGenI;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JPanel;

public class UItestGeomFrame extends javax.swing.JFrame implements UIFrameI, UIGeomFrameAbstract {

    /**
     * Creates new form UItestGeomFrame
     */
    public UItestGeomFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        unitTestGeomPanel1 = new btapp.ui.UnitTestGeomPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Draw Panel JUnitTestCase");

        unitTestGeomPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout unitTestGeomPanel1Layout = new javax.swing.GroupLayout(unitTestGeomPanel1);
        unitTestGeomPanel1.setLayout(unitTestGeomPanel1Layout);
        unitTestGeomPanel1Layout.setHorizontalGroup(
            unitTestGeomPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 406, Short.MAX_VALUE)
        );
        unitTestGeomPanel1Layout.setVerticalGroup(
            unitTestGeomPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 260, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(160, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(142, 142, 142))
            .addGroup(layout.createSequentialGroup()
                .addGap(76, 76, 76)
                .addComponent(unitTestGeomPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(30, 30, 30)
                .addComponent(unitTestGeomPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(UItestGeomFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(UItestGeomFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(UItestGeomFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(UItestGeomFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new UItestGeomFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    private btapp.ui.UnitTestGeomPanel unitTestGeomPanel1;
    // End of variables declaration                   

    @Override
    public void presentQuestions(Question qO, int qNo) {
        ArrayList qd = qO.getQuestionDraw();
        unitTestGeomPanel1.setQObject(qd);
        unitTestGeomPanel1.repaint();

    }

    @Override
    public void presentAnswers(Question qO, int qNo) {
        ArrayList ad = qO.getAnswerDraw();
        unitTestGeomPanel1.setQObject(ad);
        unitTestGeomPanel1.repaint();
    }

    @Override
    public void presentQuestions(Question qO, int qNo, QGenI qg) {
        unitTestGeomPanel1.setqGen(qg, qO, 0);
        unitTestGeomPanel1.repaint();
    }

    @Override
    public void presentAnswers(Question qO, int qNo, QGenI qg) {
        unitTestGeomPanel1.setqGen(qg, qO, 1);
        unitTestGeomPanel1.repaint();
    }


    @Override
    public int getHeight(){
        return unitTestGeomPanel1.getHeight();
    }

    @Override
    public int getWidth(){
        return unitTestGeomPanel1.getWidth();
    }

}
</pre>

Code for JPanel


    import btapp.Question;
    import btapp.qGens.QGenGeomAbstract;
    import btapp.qGens.QGenI;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Shape;
    import static java.awt.image.ImageObserver.HEIGHT;
    import static java.awt.image.ImageObserver.WIDTH;
    import java.util.ArrayList;


    public class UnitTestGeomPanel extends javax.swing.JPanel {
        private int type;

        /**
         * Creates new form UnitTestGeomPanel
         */
        public UnitTestGeomPanel() {
            initComponents();
        }

        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        //                           
        private void initComponents() {

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 400, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 300, Short.MAX_VALUE)
            );
        }//                         
        // Variables declaration - do not modify                     
        // End of variables declaration                   
        ArrayList dAL = new ArrayList();
        private double h, w;

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            System.out.println("repainign based on QO object of type "+type);

            if(type == 0){
                qg.presentQuestions(qO, (Graphics2D)g);
            }
            else
                if(type == 1)
                    qg.presentAnswers(qO, (Graphics2D)g);
                }

        void setQObject(ArrayList dAL) {
            this.dAL = dAL;
        }

        QGenI qg;
        Question qO;

        void setqGen(QGenI qg, Question qO, int type) {
            this.qO = qO;
            this.qg = qg;
            this.type = type;
        }
    }

Please help me... Thanks in advance

EDIT: Days later problem occurs in my system also but not as worse. Please refer to the images in comment.

回答1:

I had the same problem, so I changed my display setting to High performance and it SOLVED the problem (in Windows 8 And java 1.8).