簡體   English   中英

在paintComponent方法中使用屬性

[英]Using an attribute in the paintComponent method

我正在使用NetBeans GUI生成器,並且試圖創建一種“ 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);
                    }
                }
            }
        };

this是指匿名類,但主類包含成員變量。 更換

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM