Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用鼠标标记方法重新绘制时图像闪烁_Java_Swing_Jframe_Repaint - Fatal编程技术网

Java 使用鼠标标记方法重新绘制时图像闪烁

Java 使用鼠标标记方法重新绘制时图像闪烁,java,swing,jframe,repaint,Java,Swing,Jframe,Repaint,正如标题所示,我的问题是我希望能够拖动图像。 在这个特定的例子中,我想将一个图像从一个JPanel(或者更确切地说是我自己的子类)拖到另一个(不同的)JPanel子类中。因此,我在我的JPanel子类中添加了一个MouseListener,这样在单击面板中的某个区域时,就会选择一个图像绘制在JFrame(子类)上。以下是一些代码,以便您了解我的问题: public void mousePressed(MouseEvent e) { int x = e.getX(); int y

正如标题所示,我的问题是我希望能够拖动图像。
在这个特定的例子中,我想将一个图像从一个JPanel(或者更确切地说是我自己的子类)拖到另一个(不同的)JPanel子类中。因此,我在我的JPanel子类中添加了一个
MouseListener
,这样在单击面板中的某个区域时,就会选择一个图像绘制在JFrame(子类)上。以下是一些代码,以便您了解我的问题:

public void mousePressed(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    if (x >= 10 && x < 42 && y >= 10 && y < 42) {
        image = barracks;   //barracks is a predefined image, created in the constructor
        dragBuilding = true;
        PixelMain.pixelMain.repaint(); //pixelMain is an instance of the JFrame subclass
    }
}
//irrelevant code, e.g mouseMoved, ...
public void mouseDragged(MouseEvent e) {
    if (dragBuilding) {
        //System.out.println("GPanel mouseDragged");
        PixelMain.pixelMain.repaint();
    }
}
在您提问之前,我确实将一些代码移到了其他类中,因此调用它的频率较低,但这不是问题所在。即使绘制方法仅绘制矩形(直接在图形g上,而不是图形2D上),矩形也会闪烁。
如果有人能帮我找到解决办法,我会非常感激的

注意:我知道在JFrame或JFrame的子类上绘制可能不是很优雅,但我个人不知道有什么替代方法。
注2:根据我读到的google/stackoverflow结果或线程,我应该使用一个JPanel,它似乎是双缓冲的(不管是什么,我真的不明白。但是,这里几乎是晚上11点)。因此,我可能会将所有组件移动到JPanel来解决问题,但我想尝试在不这样做的情况下解决问题。

注3:是的,代码属于我正在编写的一个(策略)游戏,但考虑到这个问题并不完全与游戏开发有关,我决定将其发布在这里,而不是在游戏开发堆栈交换上。

“不管是什么”这就是你重画图像时图像闪烁的原因。@AndyTurner我猜你的意思是它的缺失是原因。。。?明天我会再看一遍,现在我要睡觉了,因为我要在大约7个半小时后去上学。是的<代码> JPANT/COD>支持双缓冲,但默认情况下无法启用(因为它需要两倍的内存)。请考虑提供一个演示您的问题的方法。这不是一个代码转储,而是您正在做的一个示例,它突出了您所遇到的问题。这将减少混乱和更好的响应
JFrame
不是双缓冲的,因此它可能负责生成闪烁。一般来说,您不应该覆盖
paint
,尤其是像
JFrame
这样的顶级容器,而是应该使用
JPanel
并覆盖它的
paintComponent
方法
public void paint(Graphics g) {   //i would have used paintComponent, but it seems like JFrame does not have this method ...?
    super.paint(g);
    if (PixelMain.panelOffense.getDragBuilding()) {  //panelOffense is an instance of the JPanel subclass, getDragBuilding returns a boolean that depends on whether the mouse is held down at the moment
        Graphics2D g2 = (Graphics2D) g;
        Rectangle2D tr = new Rectangle2D.Double((int)getMousePosition().getX(), (int)getMousePosition().getY(), 16, 16); //size of the texture
        TexturePaint tp = new TexturePaint(PixelMain.panelOffense.getImg(), tr);
        g2.setPaint(tp);
        Rectangle2D r = (Rectangle2D) new Rectangle((int)getMousePosition().getX(), (int)getMousePosition().getY(), 16, 16); //area to fill with texture
        g2.fill(r);

        System.out.println("test");
    }
}