2014-05-14 90 views
0

我正在做一些需要在屏幕上拖动对象的项目。在我的主要的I类中的对象有这样听者图像拖动鼠标不被绘制

addMouseMotionListener(new MouseMotionAdapter(){ 
      public void mouseDragged(MouseEvent e){ 
       currentX = e.getX() - 10 ; 
       currentY = e.getY() - 5; 

       if(currentNote != null) 
       { 

        if(!editingMode) 
        { 
         graphics2D.setPaint(Color.BLACK); 
         currentNote.setParameters(currentX, currentY, NoteDrawing.SKIP_CHECK); 
         currentNote.paintComponent(graphics2D); 

        } 
        else 
        { 
         graphics2D.setPaint(Color.BLACK); 
         currentNote.setParameters(currentNote.getBallFromX(), currentY, NoteDrawing.SKIP_CHECK); 
         currentNote.paintComponent(graphics2D); 
        } 

       } 
       repaint(); 
      } 
     }) 

查阅(延伸的JComponent),其应涂我有以下的paintComponent方法:

public void paintComponent(Graphics g) { 
     g.drawImage(bmoll,fromX, fromY,null); 
     g.drawLine(fromX, fromY, fromX + 30, fromY); 
     repaint(); 

    } 

和设置参数的方法:

public void setParameters(int x, int y) { 
     this.fromX = x; 
     this.fromY = y; 

    } 

当我按下对象上的按钮然后拖动鼠标时,在paintComponent方法中绘制的线条被正确拖动,但图像不是。图像不为空 - 它在屏幕上显示在其原始位置。如果我用不同的坐标复制g.drawImage(bmoll,fromX, fromY,null);这条线,它应该在两处绘制。但是当我拖动鼠标 - 没有任何反应。如果有人对导致问题的原因有一些建议,我将不胜感激。

+1

1)为了更快地获得更好的帮助,请发布[MCVE](http://stackoverflow.com/help/mcve)(最小完整和可验证示例)。 2)获取图像的一种方法是通过热链接到[本答案](http://stackoverflow.com/a/19209651/418556)中看到的图像。 3)你忘了问一个问题。你的问题是什么? –

+1

我的问题...我打算问是什么原因导致我的问题。现在它不再相关了,因为您对MCVE的建议感谢我在准备示例发布时发现了错误。问题出在其中一个对象构造函数中。感谢您的帮助!! – user3107531

+0

我选择写一个答案,以防万一有类似的问题 - 我可以在两天内接受它。 – user3107531

回答

1

显然问题出在对象的构造函数中。在原始对象的创建方案要求:

public Bmoll(int startX, int startY) 
    { 
     this.fromX = startX; 
     this.fromY = startY; 
     this.inverted = false; 
     this.staff = Staff.getActiveStaff(); 
     this.bmoll = GuiHelper.getImage(B_MOLL_FILE); 
    } 

同时,创造了拖动对象时,空的构造函数被调用,因此图像对象bmoll为空 - 没有什么要绘制。

0
  1. ,请务必让super.paintComponent
  2. 画的油漆方法的上下文中完成的,你不应该需要调用paintpaintComponent自己
  3. 使用paintComponent画的当前状态
  4. 当点击,准备状态并致电repaint
  5. 拖动时更新状态并致电repaint