Tom Yeh avatar Tom Yeh committed 1df4dd3

Improved MouseExample

Comments (0)

Files changed (1)

examples/src/main/java/org/sikuli/api/examples/MouseExample.java

 package org.sikuli.api.examples;
 
+import java.awt.Dimension;
+import java.awt.FlowLayout;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+import java.net.URL;
+
+import javax.swing.BoxLayout;
+import javax.swing.DefaultListModel;
+import javax.swing.ImageIcon;
+import javax.swing.JButton;
+import javax.swing.JList;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+
 import org.sikuli.api.DesktopScreenRegion;
 import org.sikuli.api.ImageTarget;
 import org.sikuli.api.ScreenRegion;
 	static Mouse mouse = new DesktopMouse();
 	static Keyboard keyboard = new DesktopKeyboard();
 	static Canvas canvas = new DesktopCanvas();
-	
+
 	static ScreenSimulator simulator = new ScreenSimulator(){
 		public void run(){
-			addImageButton(Images.Dog, "Dog");
-			addImageButton(Images.Cat, "Cat");
-			wait(8000);
+			showComponent(new TwoImageButtons());
+			wait(10000);
 			close();
 		}
-	};
-	
+	};	
+
+	static class TwoImageButtons extends JPanel {
+		TwoImageButtons(){
+			setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
+			add(new ButtonEventViewer(Images.Dog));
+			add(new ButtonEventViewer(Images.Cat));
+		}
+	}
+
+	static class ButtonEventViewer extends JPanel {
+
+		DefaultListModel eventListModel = new DefaultListModel();
+		JList eventList;
+
+		ButtonEventViewer(URL image){
+			setLayout(new FlowLayout());
+			add(new ImageButton(image));
+
+			eventList = new JList(eventListModel);
+			JScrollPane scrollpane = new JScrollPane(eventList);
+			scrollpane.setPreferredSize(new Dimension(400,150));
+			add(scrollpane);
+			
+		}
+
+
+		class ImageButton extends JButton implements MouseListener {
+
+			ImageButton(URL image){				
+				this.setIcon(new ImageIcon(image));
+				this.addMouseListener(this);			
+			}
+
+			private void log(MouseEvent e, String event){
+
+				String button = "";
+				if (e.getButton() == MouseEvent.BUTTON1){
+					button = "left";
+				}else if (e.getButton() == MouseEvent.BUTTON3){
+					button = "right";
+				}
+
+				String eventText = event + " " + button;
+				eventListModel.addElement(eventText);
+				eventList.ensureIndexIsVisible(eventListModel.getSize()-1);
+			}
+
+
+			@Override
+			public void mouseClicked(MouseEvent arg0) {
+				log(arg0, "clicked");
+			}
+
+			@Override
+			public void mouseEntered(MouseEvent arg0) {
+				log(arg0, "entered");
+			}
+
+			@Override
+			public void mouseExited(MouseEvent arg0) {
+				log(arg0, "exited");
+			}
+
+			@Override
+			public void mousePressed(MouseEvent arg0) {
+				log(arg0, "pressed");			
+			}
+
+			@Override
+			public void mouseReleased(MouseEvent arg0) {
+				log(arg0, "released");			
+			}
+		}
+	}
+
+
+
+
 	public static void main(String[] args) {
 		simulator.start();
-		
+
 		ScreenRegion s = new DesktopScreenRegion();	
 
 		ScreenRegion dog = s.find(new ImageTarget(Images.Dog));
 		ScreenRegion cat = s.find(new ImageTarget(Images.Cat));
 		
-		System.out.println("Click the dog");
 		mouse.click(dog.getCenter());
-		System.out.println("Click the cat");
 		mouse.click(cat.getCenter());
 
-		System.out.println("Right-click the dog");
 		mouse.rightClick(dog.getCenter());
-		System.out.println("Right-click the cat");
 		mouse.rightClick(cat.getCenter());
 
-		
-		System.out.println("Hover over the dog");
 		mouse.hover(dog.getCenter());
-		System.out.println("Hover over the cat");
 		mouse.hover(cat.getCenter());
+		mouse.hover(dog.getCenter());
 		
-		System.out.println("Press left");
+		mouse.hover(cat.getCenter());		
 		mouse.press();		
-		System.out.println("Release left");
 		mouse.release();
 		
-		System.out.println("Press right");
+		mouse.hover(dog.getCenter());		
 		mouse.rightPress();		
-		System.out.println("Release right");
 		mouse.rightRelease();
-
 	}
 
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.