I have a problem to draw a point on JPanel
. I want to put point with her position.
Because on the panel I load a picture. For each click on button I would to add point on panel.
But i don't appear on the panel.
File simpleIHM :
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class simpleIHM extends JFrame{
JPanel JpLeft = new JPanel();
JPanel JpRight = new JPanel();
JButton btn1 = new JButton("Show");
JLabel msgX = new JLabel("X :");
JLabel msgY = new JLabel("Y :");
JTextField textX = new JTextField(5);
JTextField textY = new JTextField(5);
public static int x,y = 0;
JLabel img = null;
BufferedImage image;
public simpleIHM(){
img = new JLabel(new ImageIcon("Centre.png"));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JpLeft.add(img);
PanelIMG imgPanel = new PanelIMG();
JpLeft.add(imgPanel);
getContentPane().add(JpLeft, BorderLayout.WEST);
JpRight.add(msgX);
JpRight.add(textX);
JpRight.add(msgY);
JpRight.add(textY);
JpRight.add(btn1);
JpRight.setLayout(new GridLayout(3, 2));
getContentPane().add(JpRight, BorderLayout.EAST);
pack();
setVisible(true);
showIMG("./Centre.png");
//!!
btn1.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
System.out.println("CLICK!");
x = Integer.parseInt(textX.getText());
y = Integer.parseInt(textY.getText());
System.out.println("X "+x+ "| Y "+y); img.repaint();
}
});
}
public void showIMG(String test){
try
{
File input = new File("Centre.png");
image = ImageIO.read(input);
}
catch (IOException ie)
{
System.out.println("Error:" + ie.getMessage());
}
}
public static void main(String [ ] arg) {
simpleIHM IHM = new simpleIHM();
}
}
File PanelIMG
import javax.swing.JPanel;
import java.awt.*;
import javax.swing.*;
public class PanelIMG extends JPanel{
public PanelIMG()
{
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawOval(simpleIHM.x, simpleIHM.y, 5, 5);
System.out.println("Paint Component");
}
}
Thanks in advance