从JPanel继承的一个面板JPanelUser,然后我在JPanelUser里的PaintCompent里初始化网格线,网格画线一闪而过,如果等窗体显示出来后,点击按钮调用画虚线的话也可以画出来,但最小化一下就又没了,该如何处理???

解决方案 »

  1.   

    虽然可以继承Runable线程接口来不停的画,但看起来是动态的,用repaint也是同样的效果
    不能达到我所想要的要求
      

  2.   

    public void paint( Graphics g )
      

  3.   

    protected void paintComponent ( Graphics g )
        {
            /*
            super.paintComponent ( g ) ;
            int height = this.getHeight () ;
            int width = this.getWidth () ;
            g.drawRect ( 0 , 0 , width - 1 , height - 1 ) ;
            InitDrawGrid () ;
            */
           //repaint () ;    }
        public void paint ( Graphics g )
        {
            super.paintComponent ( g ) ;
            int height = this.getHeight () ;
            int width = this.getWidth () ;
            g.drawRect ( 0 , 0 , width - 1 , height - 1 ) ;
            InitDrawGrid () ;
            repaint () ;
        }两者的效果是一样的,只是在不停的动态的画,不能达到我的要求,我是要求静态的网格虚线
      

  4.   

    如果去掉repaint则,网格线一闪而过
    如果在某个按钮里执行InitGrid则可以生成静态的网格线,但最大化或者其它改变窗口大小的事件发生,又会重化,线就没了
      

  5.   

    你看看先用Container cp = getContentPane();
      然后在cp上画;我以前也碰到过这种问题,就这样搞定的,
    如果你用的是JBuider  可以用this.getGraphics(). //画你自己图
      

  6.   

    不要在paint 和paintComponent中调用repaint方法,
    只在你需要重画时调用
      

  7.   

    我把这部分的代码帖出来吧
    public class Flow_Panel
            extends JPanel
            implements MouseListener , MouseMotionListener{
        private Color m_linecolor = Color.green ; //画线颜色
        public boolean IsDrawGrid = false ;
        public int IsDrawCount = 0 ;
        public Flow_Point SquarePoint[][] ;
        public int SquareO[][][] ;    public void Flow_Panel_Panel()
        {
            addMouseListener(this);
            addMouseMotionListener(this);
        }    protected void paintComponent ( Graphics g )
        {         super.paintComponent ( g ) ;
             int height = this.getHeight () ;
             int width = this.getWidth () ;
             g.drawRect ( 0 , 0 , width - 1 , height - 1 ) ;         InitDrawGrid () ;    }    //画直线
        public void drawline ( Point point1 , Point point2 )
        {
            Graphics2D g = ( Graphics2D )this.getGraphics () ;
            g.setColor ( m_linecolor ) ;
            g.drawLine ( point1.x , point1.y , point2.x , point2.y ) ;
        }    //画直线
        public void drawline ( int x1 , int y1 , int x2 , int y2 )
        {
            Graphics2D g = ( Graphics2D )this.getGraphics () ;
            g.setColor ( m_linecolor ) ;
            g.drawLine ( x1 , y1 , x2 , y2 ) ;
        }    //初始化网格
        public void InitDrawGrid()
        {
            int xcount = 0 , ycount = 0 ;
            xcount = ( this.getWidth () - 50 ) / 50 + 1 ;
            ycount = ( this.getHeight () - 50 ) / 50 + 1 ;
            int lastx , lasty ;        int x0 , y0 , x1 , y1 ;
            SquarePoint = new Ivr_Point[ xcount ][ ycount ] ;
            SquareO = new int[ xcount ][ ycount ][ 20 ] ;        for ( int i = 1 ; i <= ycount ; i++ )
            {
                for ( int j = 1 ; j <= xcount ; j++ )
                {
                    x0 = x1 = ( j - 1 ) * 50 + 25 ;
                    y0 = ( i - 1 ) * 50 + 25 ;
                    y1 = i * 50 + 25 ;
                    //SquarePoint[ i ][ j ].setLocation ( x0 , y0 ) ;
                    //SquarePoint[ i ][ j ].xCenter = x0 + 25 ;
                    //SquarePoint[ i ][ j ].yCenter = y0 + 25 ;                drawMline ( x0 , y0 , x1 , y1 ) ; //画列                if ( j == xcount )
                    {
                    }
                    else
                    {
                        x0 = ( j - 1 ) * 50 + 25 ;
                        x1 = j * 50 + 25 ;
                        y0 = y1 = ( i - 1 ) * 50 + 25 ;
                        drawMline ( x0 , y0 , x1 , y1 ) ; //画行
                    }
                    //页
                    for ( int page = 1 ; page <= 20 ; page++ )
                    {
                        //SquareO[ i ][ j ][ page ] = 0 ;
                    }
                }            if ( i == ycount )
                {
                    lasty = i * 50 + 25 ;
                    for ( int m = 1 ; m < xcount ; m++ )
                    {
                        drawMline ( ( m - 1 ) * 50 + 25 , lasty ,
                                    m * 50 + 25 , lasty ) ;
                    }
                }        }
        }
        //画虚线
        public void drawMline ( int x0 , int y0 , int x , int y )
        {
            int x1 , y1 , x2 , y2 , num ;
            double m1 ;
            Graphics2D g = ( Graphics2D )this.getGraphics () ;
            g.setColor ( m_linecolor ) ;        m1 = Math.sqrt ( ( double ) ( ( x - x0 ) * ( x - x0 ) +
                                          ( y - y0 ) * ( y - y0 ) ) ) ;
            num = ( int ) ( m1 / 5 ) ;
            x1 = x0 ;
            y1 = y0 ;
            for ( int i = 0 ; i < num ; i++ )
            {
                x2 = x0 + ( int ) Math.round ( 5 * ( i + 1 ) * ( x - x0 ) / m1 ) ;
                y2 = y0 + ( int ) Math.round ( 5 * ( i + 1 ) * ( y - y0 ) / m1 ) ;
                if ( i % 2 == 0 )
                {
                    g.drawLine ( x1 , y1 , x2 , y2 ) ;
                }            x1 = x2 ;
                y1 = y2 ;
            }    }    //画东、南、西、北四个方向的箭头
        public void drawArrow(int x, int y, int direction)
        {
            int startX[]=new int[2];
            int startY[]=new int[2];        /*Direction
            1 up
            2 down
            3 left
            4 right
            */        switch(direction)
            {
                case 1 ://up
                    startX[0] = x - 3;
                    startY[0] = y - 5;
                    startX[1] = x + 3;
                    startY[1] = y - 5;
                    break;            case 2:  //Down
                    startX[0] = x - 3;
                    startY[0] = y + 5;
                    startX[1] = x + 3;
                    startY[1] = y + 5;
                    break;            case 3: //left
                    startX[0] = x - 5;
                    startY[0] = y - 3;
                    startX[1] = x - 5;
                    startY[1] = y + 3;
                    break;            case 4:  //right
                    startX[0] = x + 5;
                    startY[0] = y - 3;
                    startX[1] = x + 5;
                    startY[1] = y + 3;
                    break;
            }
            for(int i=0;i<=1;i++)
                drawline( startX[i], startY[i], x, y);    }    //设置画线颜色
        public void setLineColor ( Color c )
        {
            m_linecolor = c ;    }    //获取画线颜色
        public Color getLineColor ()
        {
            return m_linecolor ;
        }    private void jbInit () throws Exception
        {
            this.setBackground ( Color.black ) ;
        }
        public void mouseClicked ( MouseEvent mouseEvent )
        {
        }    public void mousePressed ( MouseEvent mouseEvent )
        {
        }    public void mouseReleased ( MouseEvent mouseEvent )
        {
        }    public void mouseEntered ( MouseEvent mouseEvent )
        {
        }    public void mouseExited ( MouseEvent mouseEvent )
        {
        }    public void mouseDragged ( MouseEvent mouseEvent )
        {
        }    public void mouseMoved ( MouseEvent mouseEvent )
        {
        }
    }
      

  8.   

    to Beatles_The(甲虫) 
    本身是用this.getGraphics ()画的
    to xtaotao(淘淘) 
    我那里用repaint只是试试看会出现什么样的效果(前面已经有说明)