how to draw on jFrame
hello everyone,
I am trying to draw on a JFrame some circles and some ovals using drawOval() method whenever the user clicks the left mouse button.
I have implemented the above description, however whenever i click the Left MB i dont see the drawings right away. I had to restore down and maximize the window. In which case, the drawing appear this time bt in a different location.
I would really appreciate if someone can provide assistance with these two matters:
1- why do n't the drawinings occur instantly upon clicking the left mouse button.
2- why do the drawings occur in a different location from where i originally clicked. Please bear in mind that i am implementing the mouse action listener to capture the x,y of user clicks.
I also would like to note that the shape that i am drawing, is drawn on a panel which is added to the frame. here is the code:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class Test{
Node node;
int lastx, lasty;
JFrame f;
MouseListener m;
public Test(){
}
public void create()
{f = new JFrame();
f.setTitle("window one");
f.setLocation(0,0);
f.setSize(500, 500);
f.setBackground(Color.pink);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.addMouseListener(m);
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
lastx= e.getX();
lasty = e.getY();
System.out.println(e.getX()+ ""+e.getY());
if (e.getButton()==1)
{
node = new Node(lastx,lasty);
System.out.println(node.getX()+ ""+node.getY());
f.getContentPane().add(node);
}
else
if(e.getButton() == 2)
{
}
else
if(e.getButton() == 3)
{
}
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public static void main(String[] args)
{
Test test = new Test();
test.create();
}
Thank you
Regards

