I am working on drawing a line or dot on the current Jframe.
I have already generated it and now i want to draw on it, without losing the frame data. I have gone through some suggestions but most of them suggests to redraw the Jframe. Some of them suggested to use Jpanel. Problem is that I am new here, and I dont think I am getting anywhere now without any help. So please can anyone help me with this..?
Sample code is given below
private static JFrame showImage(BufferedImage src){
ImageIcon icon = new ImageIcon(img);
JLabel lbl = new JLabel();
lbl.setIcon(icon);
JFrame frame = new JFrame("JFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(lbl, BorderLayout.CENTER);
frame.setSize(800, 600);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
return frame;
}
public static void main(String[] args) {
try {
//read image
try{
File f = new File("/home/amit/workspacenew/autocrop/screenshot_full_solid.png");
img = ImageIO.read(f);
}catch(IOException e){
System.out.println(e);
}
JFrame frame = showImage(img);
frame.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
int x=e.getX();
int y=e.getY();
//need suggestion here
drawdot(x, y);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}