From: BEHROUZ on
Hi every body
I am new at Java and I have a question for you.I just wonder how I can
add several objects from several classes(lets say I have three
classes MyLine, MyRectangle, and MyOval) to one JPanel class .
I have tried this way but it did not work!
public class MyPanel extends JPanel {
MyPanel(){
MyRect rec = new MyRect();
MyOval ova = new MyOval();
add(rec);
add(ova);
}
}
I know that I can add several JLabel or JButton to Jpanel object but
can I do this for Graphic classes as well?
could you please give a hint about that?
best regards,
Behrouz
From: RedGrittyBrick on

BEHROUZ wrote:
> Hi every body
> I am new at Java and I have a question for you.I just wonder how I can
> add several objects from several classes(lets say I have three
> classes MyLine, MyRectangle, and MyOval) to one JPanel class .
> I have tried this way but it did not work!
> public class MyPanel extends JPanel {
> MyPanel(){
> MyRect rec = new MyRect();
> MyOval ova = new MyOval();
> add(rec);
> add(ova);
> }
> }
> I know that I can add several JLabel or JButton to Jpanel object but
> can I do this for Graphic classes as well?
> could you please give a hint about that?

Since JPanel.add() expects a java.awt.component, MyLine MyRectangle and
MyOval *must* descend from java.awt.component. You should post an SSCCE
so the readers don't have to guess what you are doing wrong.
http://sscce.org.

If you want to draw ovals and other geometric shapes on a canvas then
you should probaly use the 2D API instead.
http://en.wikibooks.org/wiki/Java_Programming/Graphics/Drawing_shapes

--
RGB
 | 
Pages: 1
Prev: Jave ME and BT
Next: Access SNMP using Java