我有一个图像,我使用其上的预定义位置来创建带有颜色的图形对象。单击鼠标,我尝试在其上用颜色创建一些椭圆形。事实上我没能达到这个目的。因为,当我单击预定义位置时,我可以在其上创建一个椭圆形,但是当我单击另一个预定义位置时,第一个椭圆形消失了。
单击椭圆形两次即可将其删除。
看看这个,
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();
}
}
}
图像面板类
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);
}
}
请问有什么想法吗?
谢谢 。