Re: Should I extends JComponent?
On Jul 16, 4:05 am, cyprian <cyprian.ek...@gmail.com> wrote:
On Jul 16, 5:41 am, Stupid Dustbin <students.dumping.gro...@gmail.com>
wrote:
Hi,
It seem to me that what I'm trying to say is misunderstood. My
question is not really on the canvas should extends JPanel or
JComponent but rather should the item class extends JComponent?
Current Implementation:
class SomeLameCanvas extends JPanel/JComponent implements
MouseListener, MouseMotionListener{
List<Item> itemsList;
Item selectedItem;
protected void paintComponent() {
}
public void mousePressed(MouseEvent e) {
for (Itemitem : itemsList) {
if (item.isSelected()) {
selectedItem = item;
}
}
//if it is right click, remove item instead.
}
public void mouseDragged(MouseEvent e) {
//code to drag the selected item around the canvas.
}
My question is should I consider making my Item class extends
JComponent? and make it kind of event driven as suggested by a
teacher? That the part I'm unsure of.
Thanks!
You have two alternatives, either inherit JComponent and place your
ItemClass in a JPanel or inherit the specific subclass of JComponent
and that will give you other functionalities that you need for that
item that is selected. I believe the latter option is better.
Specifically, if you need to place an Image at a specific location in
your JPanel, you can use a JLabel with the icon property set to the
ImageIcon representing your Image.
You can then attach any Mouse(Motion)Listener you want on the JLabel.