Powered by Blogger.
Showing posts with label Mouse Example. Show all posts
Showing posts with label Mouse Example. Show all posts

Colliding Using Mouse Example

This section covers tasks associated with mouse input:

1. Tracking the Mouse Cursor

2. Drawing Lines with the Mouse

3. Processing a Double Click Message

4. Selecting a Line of Text

5. Using a Mouse Wheel in a Document with Embedded Objects

6. Retrieving the Number of Mouse Wheel Scroll Lines

Mouse Class Definition:

The mouse class inherits from QGraphicsItem. The QGraphicsItem class is the base class for all graphical items in the Graphics View framework, and provides a light-weight foundation for writing your own custom items.

class Mouse : public QGraphicsItem
 {
 public:
     Mouse();

     QRectF boundingRect() const;
     QPainterPath shape() const;
     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                QWidget *widget);

 protected:
     void advance(int step);

 private:
     qreal angle;
     qreal speed;
     qreal mouseEyeDirection;
     QColor color;
 };

When writing a custom graphics item, you must implement QGraphicsItem's two pure virtual public functions: boundingRect(), which returns an estimate of the area painted by the item, and paint(), which implements the actual painting. In addition, we reimplement the shape() and advance(). We reimplement shape() to return an accurate shape of our mouse item; the default implementation simply returns the item's bounding rectangle. We reimplement advance() to handle the animation so it all happens on one update.

Mouse Example:

import java.awt.*;
 import java.awt.event.*;

 public class MouseExample implements WindowListener, MouseMotionListene
   private Frame f;
   private TextField tf;

   public MouseExample(){
     f = new Frame(”Mouse Example”);
     tf = new TextField(30);
   }

   public void launchFrame(){
     Label label = new Label(”Click and drag the mouse”);
     f.add(label, BorderLayout.NORTH);
     f.add(tf, BorderLayout.SOUTH);
     f.addMouseMotionListener(this);
     f.addMouseListener(this);
     f.addWindowListener(this);
     f.setSize(300, 200);
     f.setVisible(true);
   }

   public void mouseDragged (MouseEvent e){
     String s = ”Mouse dragged : X= ”+e . getX()+” Y=”+e.getY();
     tf.setText(s);
   }

   public void mouseEntered(MouseEvent e){
     String s = ”The mouse entered”;
     tf.setText(s);
   }

   public void mouseExited (MouseEvent e){
     String s = ”The mouse has left the building”;
     tf.setText(s);
   }

   public void mousePressed(MouseEvent e){}
   public void mouseReleased(MouseEvent e){}
   public void mouseMoved(MouseEvent e){}
   public void mouseClicked(MouseEvent e){}

   public void windowClosing(WindowEvent e){
     System.exit(0);
   }

   public void windowOpened(WindowEvent e){}
   public void windowIconified(WindowEvent e){}
   public void windowDeiconified(WindowEvent e){}
   public void windowClosed(WindowEvent e){}
   public void windowActivated(WindowEvent e){}
   public void windowDeactivated(WindowEvent e){}

   public static void main(String[]args){
     MouseExample me = new MouseExample();
     me.launchFrame();
   }
 }