Hi guys,
I have a file named DrawPanel and this class is supposed to draw different shapes which i declared before by using mousedrag. I am not able to draw more thatn one shape and put them in an array.It seems that i don't know where to change the value of shapeCount in my code.The followings are part of my code:
I am completely messed up. I would be appreciate if anybody can help me to solve my problem.

public void mousePressed(MouseEvent event)
            {
                System.out.println("MousePress...");
                x1 = event.getX();
                y1 = event.getY();
                //currentShape = shapes[shapeCount-1];
                repaint();
                //shapeCount --;
                System.out.println("currentShape="+currentShape);
            }//end mousePressed method

            public void mouseReleased(MouseEvent event)
            {
                System.out.println("MouseRelease,shapeCount="+shapeCount);
                x2 = event.getX();
                y2 = event.getY();
                
                if(shapeType == 0)
                {
                    //shapes[shapeType] = new MyLine(x1, y1, x2, y2,color);
                    //shapes[shapeType].draw(g);
                    shapes[shapeCount-1]= new MyLine(x1,y1,x2,y2,color);
                    currentShape = shapes[shapeCount-1];
                    System.out.println("currentshaperelease="+currentShape);

                    //line.draw(g);
                    repaint();
                }

               /* else if (shapeType == 1)
                {
                    MyRectangle rectangle = new MyRectangle(x1,y1,x2,y2,color,true);
                    rectangle.draw(g);

                    //shapes[shapeType] = new Rectangle();
                    //shapes[shapeType].draw(g);
                    repaint();
                }

                else if (shapeType ==2)
                {
                    shapes[shapeType] = new MyOval(x1,y1,x2,y2,color,true);
                    shapes[shapeType].draw(g);
                    repaint();
                }

                else */
                    ;
            }//end mouseReleased
public void mouseDragged(MouseEvent event)
            {
                System.out.println("mouseDrag");
                x2 = event.getX();
                y2 = event.getY();
                //System.out.println("x1y1x2y2"+x1+y1+x2+y2);
                //g.setColor(Color.BLACK);
                System.out.println("shapeCountdrag="+shapeCount);
                 
                //shapes[shapeCount-1]=new MyLine(x1,y1,x2,y2,Color.black);

                System.out.println("currentShapedrag="+currentShape);
                
                //shapes[shapeType].draw(g);
               repaint();
            }
public void paintComponent(Graphics g){
                
        super.paintComponent(g);
        //for(int i=shapeCount;i>0;i--){
            if(currentShape != null){
                //shape.draw(g);
                System.out.println("shapeCountPaint="+shapeCount);
                //shapeCount--;
                shapes[shapeCount-1].draw(g);
                
                shapeCount--;
            }
            else {
                System.out.println("currentshapepaint="+currentShape);
                //shapeCount--;
                //break;
            }

        //}

    }

Here's what I think your code should be doing:
You have an array shapes[], and an int shapeCount that holds the number of shapes you currently have stored in shapes[]. OK.
To ADD a shape in the mouse drag handler, you create a shape of the appropriate kind, and add it to the shapes[] array... for the very first shape shapeCount is 0, so you add the shape at index 0, ie shapes[shapeCount] = (the new shape).
After that you immediately add 1 to shapeCount, which becomes 0+1=1 as you expect.
When the second shape is created it goes into shapes[shapeCount] - which is shapes[1], then shapeCount gets incremented to 2.

In paintComponent(...) all you have to do is loop thru all the shapes between shapes[0] and shapes[shapeCount-1] and draw them all.

where to change the value of shapeCount

Can you describe what the shapeCount variable is used for and when/why you want to change its value?

This article has been dead for over six months. Start a new discussion instead.