I have an image and I use pre-defined positions on it to create graphic objects with colors . With a mouse click I try to create some ovals on it with colors . Actually I couldn't achieve this objective. Because , when I click on a pre-defined position I could create an oval on it but when I click on another pre-defined position first oval has gone.
An oval can be removed by click on it twice.
Have look at this ,
public class PrintDialog extends javax.swing.JDialog{
private int count = 0;
private int count_1 = 0;
/**
* Creates new form PrintFeetDialog
*/
public PrintDialog(java.awt.Frame parent, boolean modal)
{
super(parent, modal);
initComponents();
ImagePanel panel = new ImagePanel("Areas.jpg");
this.getContentPane().add(panel);
this.setResizable(false);
this.setLocationRelativeTo(panel);
this.pack();
}
private void formMouseClicked(java.awt.event.MouseEvent evt)
{
// TODO add your handling code here:
System.out.println("Print y - " + evt.getY());
System.out.println("Print x - " + evt.getX());
if ((evt.getX() >= 68 && evt.getX() <= 84) && (evt.getY() >= 44 && evt.getY() <= 72))
{
Graphics g = getGraphics();
count++;
if (count == 1)
{
g.setColor(Color.red);
g.fillOval(66, 52, 20, 20);
// repaint();
} else if (count > 1)
{
g.setColor(new Color(-3692899));
g.fillOval(66, 52, 20, 20);
repaint();
count = 0;
}
g.dispose();
}
if ((evt.getX() >= 137 && evt.getX() <= 157) && (evt.getY() >= 50 && evt.getY() <= 75))
{
Graphics g1 = getGraphics();
count_1++;
if (count_1 == 1)
{
g1.setColor(Color.RED);
g1.fillOval(137, 54, 20, 20);
} else if (count_1 > 1)
{
g1.setColor(new Color(-3692899));
g1.fillOval(66, 52, 20, 20);
repaint();
count_1 = 0;
}
g1.dispose();
}
}
}
image panel class
public class ImagePanel extends JPanel{
private Image img;
public ImagePanel(String img, String str)
{
//this(new ImageIcon(img).getImage());
}
public ImagePanel(String path)
{
Image img = new ImageIcon(path).getImage();
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
try
{
BufferedImage image = ImageIO.read(new File(path));
int rgb = image.getRGB(66, 52);
System.out.println("Colour is: "+rgb);
}
catch(IOException e)
{
e.printStackTrace();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(img, 0, 0, null);
}
}
Have any ideas please ?
Thank you .