Java - 布尔值设置为false,没有告诉它这样做

时间:2015-05-05 21:09:28

标签: java boolean

我的boolean drawFlag在我的代码中被设置为false,没有任何可见的实际告诉它将值更改为false。这阻止了方法mouseDragged(mouseEvent)中的代码执行。如果有人能够指出是什么让国旗变得虚假所以这会停止发生?感谢。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class DrawAndDrag {

    public static void main(String[] args) throws InterruptedException {
        GraphicsFrame window = new GraphicsFrame("Draw Rectangle");
        window.init();
    }
}

class GraphicsFrame extends JFrame {

    public GraphicsFrame(String title) {
        super(title);
    }

    public void init() {
        Container pane = this.getContentPane();
        pane.add(new GraphicsContent().init());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(600, 600);
        this.setVisible(true);
    }
}

class GraphicsContent extends JPanel {
    private int xStart, yStart;
    private int width, height;

    public JPanel init() {
        this.setBackground(Color.WHITE);
        this.addMouseListener(new MouseDrag());
        this.addMouseMotionListener(new MouseDrag());
        return this;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.fillRect(xStart, yStart, width, height);
    }

    class MouseDrag implements MouseListener, MouseMotionListener {
        private boolean drawFlag;

        public void mousePressed(MouseEvent e) {
            if(isOutside(e)) {
                this.drawFlag = true;
                xStart = e.getX();
                yStart = e.getY();
                width = 0; height = 0;
                System.out.println(drawFlag);
            }else {
//              this.drawFlag = false;
            }
        }

        public void mouseDragged(MouseEvent e) {
            System.out.println(drawFlag);
            if(drawFlag) {
                width = e.getX() - xStart;
                height = e.getY() - yStart;
                repaint();
            }
        }

        public boolean isOutside(MouseEvent e) {
            int xMin = Math.min(xStart, xStart + width);    int yMin = Math.min(yStart, yStart + height);
            int xMax = Math.max(xStart, xStart + width);    int yMax = Math.max(yStart, yStart + height);

            if((e.getX() < xMin || e.getX() > xMax)
            || (e.getY() < yMin || e.getY() > yMax)) {
                return true;
            }else {
                return false;
            }
        }

        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
        public void mouseClicked(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public void mouseMoved(MouseEvent e) {}
    }
}

3 个答案:

答案 0 :(得分:0)

尝试将drawFlag属性从内部类MouseDrag移至外部类GraphicsContent,如下所示

class GraphicsContent extends JPanel {
    private int xStart, yStart;
    private int width, height;
    private volatile boolean drawFlag;

    // other code

}

答案 1 :(得分:0)

  

然而拖动时,该值会不断报告为false

你有两个听众。一个是动作听众:

this.addMouseListener(new MouseDrag());
this.addMouseMotionListener(new MouseDrag());

mousePressed将不会在动画侦听器上调用,因此在拖动事件中它将始终为false。

我希望你的意思是:

MouseDrag listener = new MouseDrag();
this.addMouseListener(listener);
this.addMouseMotionListener(listener);

答案 2 :(得分:-1)

您确定需要两个MouseDrag元素吗?即使用像

这样的东西
public JPanel init() {
    this.setBackground(Color.WHITE);
    MouseDrag md = new MouseDrag();
    this.addMouseListener(md);
    this.addMouseMotionListener(md);
    return this;
}

我至少可以画一个蓝色方块......