2011-11-23 102 views
0

我想用我的程序做的事情是,当我点击图像时,矩形会与JOptionPane一起出来。但是,JOptionPane是唯一弹出的东西。为什么我的paintComponent不工作?

我试着改变方法并添加更多的类,没有任何工作>。 <任何人都可以解决我的问题吗?这是我的代码片段。

下面是我所说的filechooser,它允许我选择我的照片。另外,一些其他的东西,如标签在这里。

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); 
    } 

最后,我的ImagePanel类,其中包含麻烦的paintComponent。此外,还有一些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 
     } 
    } 

您可以编译代码并亲自查看。如果你知道该怎么做,请指教我:)非常感谢!

+1

只需要注意:确保你确实通过添加了@ @ Override注解来覆盖'paintComponent'。另外,不要*在'paintComponent'中做任何事情,比如添加监听器等等.-) – aioobe

+0

无论如何,压倒性的做什么?大声笑我没有得到这一点xD(去表明我真的是小白)。感谢您的答复。 – alicedimarco

+1

测试时尽量简化问题:删除所有不必要的方法,像'g.setColor()一样制作非常简单的'paintComponent'; g.drawRect()'无条件地。告诉我们结果 – pajton

回答

1

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

指定的鼠标事件的X和Y在ImagePanel自定义字段,与其他的名字,如:

int mx; 
int my; 

其他的东西来试验,离开了super.paintComponent方法。 而且也许你想使用g以下方法:

Graphics2D g2 = (Graphics2D)g; 

(分配给基类x和y是不是一个好主意,这样的事情改变坐标更好地利用的setBounds。)

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); 
} 

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

2

各种奇怪的东西:

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

你不应该创建一个新JPanel只需一个监听器添加到组件。你已经有了一个面板的实例。

addMouseMotionListener(this); 

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

+0

了解。我其实改变了我的代码。感谢您的小费 :) – alicedimarco