Hi guys,
I have a class (DrawPanel) which extends Jpanel.This class generates random shapes (lines,recangles and ovals) . I am suppose to add a statusbar to this class but I can't add even a simple lable to this panel... I am so frustrated and would be so appreciate if anyone can help me to figure it out.

Thanks
Hany

public class DrawPanel extends JPanel {
    private MyShape shapes[];
    private Random randomNumber;
    private int shapeType;
    

    int lineCount=0, rectCount=0, ovalCount=0;
   
    public DrawPanel(){
        
        randomNumber = new Random();
        int count =  1+randomNumber.nextInt(10);
        shapes = new MyShape[count];

        for(int i=0; i<count;i++){
                /*       Generate random coordinates     */
            int x1 = randomNumber.nextInt(300);
            int y1 = randomNumber.nextInt(300);
            int x2 = randomNumber.nextInt(300);
            int y2 = randomNumber.nextInt(300);
            Color color = new Color(randomNumber.nextInt(256),randomNumber.nextInt(256),randomNumber.nextInt(256));
                /*            Initiate Shapes            */

            shapeType =randomNumber.nextInt(3);
            if (shapeType==0){    // Line
                lineCount++;
                shapes[i] = new MyLine(x1,y1,x2,y2,color);
            }
            else if (shapeType==1){   // Rectangle
                rectCount++;
                shapes[i] = new MyRectangle(x1,y1,x2,y2,color,true);
            }
            else {     // Oval
                ovalCount++;
                shapes[i] = new MyOval(x1,y1,x2,y2,color,true);
            }
        }

 }
 
    public void paint(Graphics g){
        for (MyShape shape:shapes)
            shape.draw(g);
    }
}

--------------------------------------- TestClass------------------------------
public class TestDraw {
public static void main (String[] args){
DrawPanel panel = new DrawPanel();
JFrame frame = new JFrame();
frame.setBackground(Color.white);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setSize(300,300);
frame.setVisible(true);
}
}

I think the problem is caused because you are only drawing (painting) your shapes. You can try to call super.paint() and see if that fixes your problem. However my recomendation would be not to override paint. Insead override the paintComponent method. So you could do something like

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    for (MyShape shape:shapes)
        shape.draw(g);
}

Hi guys,
I have a class (DrawPanel) which extends Jpanel.This class generates random shapes (lines,recangles and ovals) . I am suppose to add a statusbar to this class but I can't add even a simple lable to this panel... I am so frustrated and would be so appreciate if anyone can help me to figure it out.

Thanks
Hany

public class DrawPanel extends JPanel {
    private MyShape shapes[];
    private Random randomNumber;
    private int shapeType;
    

    int lineCount=0, rectCount=0, ovalCount=0;
   
    public DrawPanel(){
        
        randomNumber = new Random();
        int count =  1+randomNumber.nextInt(10);
        shapes = new MyShape[count];

        for(int i=0; i<count;i++){
                /*       Generate random coordinates     */
            int x1 = randomNumber.nextInt(300);
            int y1 = randomNumber.nextInt(300);
            int x2 = randomNumber.nextInt(300);
            int y2 = randomNumber.nextInt(300);
            Color color = new Color(randomNumber.nextInt(256),randomNumber.nextInt(256),randomNumber.nextInt(256));
                /*            Initiate Shapes            */

            shapeType =randomNumber.nextInt(3);
            if (shapeType==0){    // Line
                lineCount++;
                shapes[i] = new MyLine(x1,y1,x2,y2,color);
            }
            else if (shapeType==1){   // Rectangle
                rectCount++;
                shapes[i] = new MyRectangle(x1,y1,x2,y2,color,true);
            }
            else {     // Oval
                ovalCount++;
                shapes[i] = new MyOval(x1,y1,x2,y2,color,true);
            }
        }

 }
 
    public void paint(Graphics g){
        for (MyShape shape:shapes)
            shape.draw(g);
    }
}

--------------------------------------- TestClass------------------------------
public class TestDraw {
public static void main (String[] args){
DrawPanel panel = new DrawPanel();
JFrame frame = new JFrame();
frame.setBackground(Color.white);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setSize(300,300);
frame.setVisible(true);
}
}

Hi, do you have a solution for this ? i am having the same problem

There seems to be no attempt to add any JLabel to anything - which is probably why it doesn't work. Don't confuse things by trying to add the JLabel to your special panel, just add it to the JFrame directly below the panel.
However, cale is right in saying that you should override paintComponent, not paint.

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