使用paintComponent方法中的属性

时间:2014-12-03 21:27:09

标签: java paintcomponent

我正在使用NetBeans GUI Builder,而我正试图创建一种" Geogebra" 。我对paintComponent的定义有问题。我想在我的组件中使用几何图形的arraylist,这是我的JFrame的一个属性但是,似乎java并没有认识到arraylist。 这里是代码中存在问题的部分:

public GUI() {
        initComponents();
    }
      public  List<Figure> Liste = new ArrayList<Figure>();

    /**
     * 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() {

        jDialog1 = new javax.swing.JDialog();
        lblx = new javax.swing.JLabel();
        lbly = new javax.swing.JLabel();
        txtx = new javax.swing.JTextField();
        txty = new javax.swing.JTextField();
        btnOkPoint = new javax.swing.JButton();
        jDialog2 = new javax.swing.JDialog();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lblx1 = new javax.swing.JLabel();
        lbly1 = new javax.swing.JLabel();
        lblx2 = new javax.swing.JLabel();
        lbly2 = new javax.swing.JLabel();
        txtx1 = new javax.swing.JTextField();
        txty1 = new javax.swing.JTextField();
        txtx2 = new javax.swing.JTextField();
        txty2 = new javax.swing.JTextField();
        btnoksegment = new javax.swing.JButton();
        MainPan = new javax.swing.JPanel();
        GraphPan = new javax.swing.JPanel(){
            @Override
            public void paintComponent(Graphics g ) {

                for (int i = 0; i < this.Liste.size(); i++) {
                    if(this.Liste.get(i) instanceof Pts){
                        Pts p = (Pts)this.Liste.get(i);
                        g.fillOval((int)Math.round(p.x),(int)Math.round(p.y), 10, 10);
                    }
                    if(this.Liste.get(i) instanceof Segment) {
                        Segment s = (Segment)this.Liste.get(i) ;
                        Segment s = (Segment) this.Liste.get(i);
                        g.drawLine(s.debut.x, s.debut.y, s.fin.x, s.fin.y);
                    }
                }
            }
        };

1 个答案:

答案 0 :(得分:0)

this引用匿名类,但主类包含成员变量。取代

Pts p = (Pts)this.liste.get(i);

Pts p = (Pts)liste.get(i);