Graphic PIZiadas

Graphic PIZiadas

My world is in..

Graphical Programming: Interaction : Mouse Events [JAVA]

mouseEventAfter create the basic GUI and animation engine, introduce the rudiments of interaction by incorporating mouse event model.

The technique will be to generate an event or happening every time you interact with the mouse. This event will be captured by one of our software modules, process it and generate some action.

The first step will be to understand how to generate these events and, later, incorporate more elaborate actions. For now it is sufficient to determine the type of event that occurs and the position on the screen where you have made.

The event-driven programming es un Programming Paradigm in which both the structure and the execution of the programs are determined by the events that occur in the system, user-defined or that they cause.

To understand the event-driven programming, we oppose it to what is not: while sequential programming (or structured) is the programmer who defines what will be the program flow, in the event-driven programming is the user-or whatever you are operating the program, which directs the program flow. Although sequential programming can be external agent intervention program, these interventions occur when the programmer has determined, and not at any time such as in the case of event-driven programming.(W)

Implement the event listener interface

We will implement the event system in a single graphics library class to avoid complicating the model initially. The first thing we will do is change the declaration of the class responsible for maintaining the database and graphics painting objects: “BlackBoard”. Indicate labeled “implements” that this class is able to listen to the mouse.

public class Blackboard extends Canvas implements MouseListener{

The new interface requires us to add a new call Java libraries to locate the files that implement this interface. What we do with the reserved word “import”.

import java.awt.event.MouseListener;

Will need to implement the methods defined in the interface, and import the class “MouseEvent” that is passed as a parameter to these methods. For now simply declare without adding any functionality.

	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

Event Registration

Will register a mouse event occurs whenever. In the constructor of the class or in a start method will join the call “addMouseListener” to indicate that we want to add the listener.

The parameter passed is the name of the class that listens to the mouse. In our case, it will indicate the same class as this event is recorded to be alerted when there is. This is done with the label “this“.

	public Blackboard(){
		this.addMouseListener(this);
	}

Event Management

Define a couple of variables that will store the coordinates of the screen position where the mouse was pressed

	private int lastx = 0;
	private int lasty=0;

Implement one of the above functions, for example that runs every time you pressed the mouse button “mouseClicked”. For now just save it the coordinates where you pressed, although we could have complicated the program with added functionality.

These coordinates are the object or event that was passed to the function, object “MouseEvent”

	public void mouseClicked(MouseEvent arg0) {
		lastx = arg0.getX();
		lasty = arg0.getX();
	}

Modify the method responsible for drawing graphics on the screen to paint something on the position where the mouse was pressed. It is the simplest implementation allows us to verify graphically that we captured and processed the mouse event.

	public void paintOffScreen(Graphics g){
		Enumeration enum=bd.elements();
		Object ob;
		while(enum.hasMoreElements()){
			ob = enum.nextElement();
			if(ob instanceof SimpleDrawable){
				((SimpleDrawable) whether).paint(g);
			}
		}
		paintMousePosition(g);
	}

Responsible for this function is “paintMousePosition”, whose implementation is to write graphically the stored coordinates of the mouse position, by the function “drawString”.

	private void paintMousePosition(Graphics g) {
		g.drawString("("+lastx  ","+lasty ")", lastx, lasty);
	}
JAVA

JAVA Course