我正在做一些需要在屏幕上拖动对象的项目。在我的主要的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)为了更快地获得更好的帮助,请发布[MCVE](http://stackoverflow.com/help/mcve)(最小完整和可验证示例)。 2)获取图像的一种方法是通过热链接到[本答案](http://stackoverflow.com/a/19209651/418556)中看到的图像。 3)你忘了问一个问题。你的问题是什么? –
我的问题...我打算问是什么原因导致我的问题。现在它不再相关了,因为您对MCVE的建议感谢我在准备示例发布时发现了错误。问题出在其中一个对象构造函数中。感谢您的帮助!! – user3107531
我选择写一个答案,以防万一有类似的问题 - 我可以在两天内接受它。 – user3107531