这是书上的一道练习题,据提示用DrawLine就行了,可是我想不出做法来

解决方案 »

  1.   

    我自己解出来了,不太复杂。public class DrawPanel extends JPanel{
        
        public void paintComponent(Graphics g) {
            //ensure the panel is properly rendered
            super.paintComponent(g);
            
            //get window hight and width
            int intWidth = getWidth();
            int intHight = getHeight();
            //get the increment unit of both width and hight
            int intUnitWidth = getWidth() /15;
            int intUnitHight = getHeight()/15;
            //variable to store current hight and width
            int intCurrWidth = 0;
            int intCurrHight = 0;
            
            //while loop: run until either width or hight reach the edge
            while((intCurrWidth < intWidth) && (intCurrHight < intHight)){
                //draw the line.
                g.drawLine(0,intCurrHight,intCurrWidth,intHight);
                
                intCurrWidth += intUnitWidth; //increment the width by one unit
                intCurrHight += intUnitHight; //increment the hight by one unit   
            }
        }
        
    }