I have been trying for the last two days to draw objects on my JPanel
. The code was running when I tried to draw objects on the JPanel
when it was not placed in JTabbedPane
. But it is hectic for me now. Kindly help me.
registering JPanel
with MouseMotionListener
panel[i].addMouseMotionListener(this);
adding panel to JTabbedPane
tp.addTab("Tab1",panel[i]);
mouseDragged
event has the following code
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
mouseReleased event
public void mouseReleased(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
repaint();
}
paint function ( that must be called automatically) has the following lines
public void paint( Graphics g )
{
super.paint(g);
// I even tried to add the following line but it didn't work too
/// g=panel[tp.getSelectedIndex()].getGraphics();
Graphics2D gtd=(Graphics2D) g;
gtd.fillOval(x, y, x1-x, y1-y); //x1-x: For width of Oval; y1-y: for height
}