0

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!

4

3 回答 3

2

各种奇怪的东西:

label.addMouseListener(new ImagePanel());
label.addMouseMotionListener(new ImagePanel()); 

您不应该仅仅为了向组件添加侦听器而创建新的 JPanel。您已经有一个面板实例。

addMouseMotionListener(this);  

永远不要在绘画方法中向组件添加侦听器。您永远无法控制何时调用绘制方法,并且最终会多次添加相同的侦听器。

于 2011-11-23T16:31:49.740 回答
1

一个注意事项:一个较小的例子会更早得到回答。

将鼠标事件 x 和 y 分配给 ImagePanel 中的自定义字段,使用其他名称,例如:

int mx;
int my;

其他要试验的东西是省略 super.paintComponent。此外,也许您想在 g 上使用更多方法:

Graphics2D g2 = (Graphics2D)g;

(分配给基类 x 和 y 绝不是一个好主意;最好使用 setBounds 来更改坐标等。)

于 2011-11-23T13:52:18.787 回答
0
  try{
       image = ImageIO.read(file); //reads and loads File as image
   }
   catch (IOException e){}

这里的代码说:“让我们尝试读取图像。如果失败(抛出异常)然后忽略问题并继续没有图像。”。忽视问题总是不好的。我们至少可以打印问题并继续。

  try{
      image = ImageIO.read(file); //reads and loads File as image
  }
  catch (IOException e){
     e.printStackTrace();//print the exception
  }

或者打印问题并停止:

  try{
      image = ImageIO.read(file); //reads and loads File as image
  }
  catch (IOException e){
     e.printStackTrace();//print the exception
     System.exit(0);//stop executing
  }

实际问题很可能在这里:

if(image != null && isRectPresent){
   g.setColor(Color.DARK_GRAY);
   g.drawRect(x-50, y-50, 100, 100);
}

我认为问题在于 if 条件是false(没有图像(可能有异常读取它......?)和/或 isRectPresent 是false)因此它什么都不做!在 处包含一个断点if,以调试模式启动程序并检查变量image以及isRectPresent程序何时到达该点。(如果它没有到达那里,你知道你有一个不同的问题。)祝你好运!

于 2011-11-23T14:13:48.713 回答