Java Code Examples for java.awt.Canvas#addMouseListener()

The following examples show how to use java.awt.Canvas#addMouseListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GetScreenLocTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(final String[] args) throws AWTException {
    robot = new Robot();
    Frame bigOne = new Frame();
    bigOne.setSize(200, 200);
    bigOne.setLocationRelativeTo(null);
    bigOne.setVisible(true);
    Frame f = new Frame();
    f.setLayout(new BorderLayout());
    f.setSize(120, 150);
    f.setLocationRelativeTo(null);
    Canvas c = new MyCanvas();
    f.add(c, BorderLayout.CENTER);
    c.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            switch(state) {
                case 0: // the first event should be (0,0)
                    if (e.getX() != 0 || e.getY() != 0) {
                        System.out.println("state 0: wrong location" + e);
                        break;
                    }
                    state++;
                    break;
                case 1: // the second event should be (1,1)
                    if (e.getX() != 1 || e.getY() != 1) {
                        System.out.println("state 1: wrong location " + e);
                        break;
                    }
                    state++;
                    break;
                case 2: // this should never happen
                    System.out.println("state 2: wrong location " + e);
            }
        }
    });
    f.pack();
    f.setVisible(true);
    bigPause();

    Point p = c.getLocationOnScreen();
    doPress(p);
    p.x += 1;
    p.y += 1;
    doPress(p);
    p.x -= 2;
    p.y -= 2;
    doPress(p);
    bigPause();

    f.dispose();
    bigOne.dispose();

    // ...and at the end the state should be 2
    if (state != 2) {
        throw new RuntimeException("wrong state: " + state);
    }
}
 
Example 2
Source File: WebcamCanvas.java    From MtgDesktopCompanion with GNU General Public License v3.0 5 votes vote down vote up
public WebcamCanvas(Webcam w, AbstractRecognitionArea s) {
	super();
	cam = w;
	strat = s;
	canvas = new Canvas();
	setSize(w.getViewSize());
	canvas.setPreferredSize(w.getViewSize());
	setBackground(Color.BLACK);
	add(canvas);
	canvas.addMouseListener(strat);
	canvas.addMouseMotionListener(strat);
	strat.init(cam.getViewSize().width, cam.getViewSize().height);
}