What I want to do with my program is when I click the image, the rectangle will come out, together with the JOptionPane. However, the JOptionPane is the only thing popping up.
I tried changing methods and adding more classes, nothing worked >.< Can anyone shed some light to my problem? Here's a snippet of my code.
Below is where I call the filechooser which allows me to select my photo. Also, a bunch of other stuff like labels are here.
public Help(){
fc.setDialogTitle("Choose an image file to begin:");
int returnval = fc.showOpenDialog(null);
if (returnval == JFileChooser.APPROVE_OPTION){ //when user selects a file, value returned will be JFileChooser.APPROVE_OPTION
File file = fc.getSelectedFile(); //the File value of the selection is returned from a call on getSelectedFile
try{
image = ImageIO.read(file); //reads and loads File as image
}
catch (IOException e){}
System.out.println("You chose to open this file: " + file.getName());
}else
System.out.println("No file selected.");
icon = new ImageIcon(image);
label = new JLabel(icon);
tagName = new JLabel(input);
label.addMouseListener(new ImagePanel());
label.addMouseMotionListener(new ImagePanel());
panel.add(tagName);
}
And finally, my ImagePanel class, which contains the troublesome paintComponent. Also, a couple of mouseListeners.
class ImagePanel extends JPanel implements MouseListener, MouseMotionListener{
@Override
public void mouseClicked(MouseEvent event) {
// TODO Auto-generated method stub
x = event.getX();
y = event.getY();
input = JOptionPane.showInputDialog("Enter tag name");
tagName.setText("You have tagged: " + input);
System.out.println(input);
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(image != null && isRectPresent){
g.setColor(Color.DARK_GRAY);
g.drawRect(x-50, y-50, 100, 100);
}
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
}
You can compile the code and see for yourself. Give me a heads up if you know what to do :) Thank you so much!