今日はマウスのクリックやドラッグなど関数を中心にして、Appletの例を挙げます。ソースを実行する前に、JDKをSunのサイトからDownloadしてください。例の1には、showStatus()がコールされて、マウスの位置はウェブブラウザのステータスバーで表示されます。例の2には、マウスはペンにされて、移動すると、線が書き出されます。

例の1:

.

.

.

  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class Mouse1 extends Applet
  6.    implements MouseListener, MouseMotionListener {
  7.  
  8.    int width, height;
  9.    int mx, my// the mouse coordinates
  10.    boolean isButtonPressed = false;
  11.  
  12.    public void init() {
  13.       width = getSize().width;
  14.       height = getSize().height;
  15.       setBackground( Color.black );
  16.  
  17.       mx = width/2;
  18.       my = height/2;
  19.  
  20.       addMouseListener( this );
  21.       addMouseMotionListener( this );
  22.    }
  23.  
  24.    public void mouseEntered( MouseEvent e ) {
  25.       // called when the pointer enters the applet's rectangular area
  26.    }
  27.    public void mouseExited( MouseEvent e ) {
  28.       // called when the pointer leaves the applet's rectangular area
  29.    }
  30.    public void mouseClicked( MouseEvent e ) {
  31.       // called after a press and release of a mouse button
  32.       // with no motion in between
  33.       // (If the user presses, drags, and then releases, there will be
  34.       // no click event generated.)
  35.    }
  36.    public void mousePressed( MouseEvent e ) {  // called after a button is pressed down
  37.       isButtonPressed = true;
  38.       setBackground( Color.gray );
  39.       repaint();
  40.       // "Consume" the event so it won't be processed in the
  41.       // default manner by the source which generated it.
  42.       e.consume();
  43.    }
  44.    public void mouseReleased( MouseEvent e ) {  // called after a button is released
  45.       isButtonPressed = false;
  46.       setBackground( Color.black );
  47.       repaint();
  48.       e.consume();
  49.    }
  50.    public void mouseMoved( MouseEvent e ) {  // called during motion when no buttons are down
  51.       mx = e.getX();
  52.       my = e.getY();
  53.       showStatus( "Mouse at (" + mx + "," + my + ")" );
  54.       repaint();
  55.       e.consume();
  56.    }
  57.    public void mouseDragged( MouseEvent e ) {  // called during motion with buttons down
  58.       mx = e.getX();
  59.       my = e.getY();
  60.       showStatus( "Mouse at (" + mx + "," + my + ")" );
  61.       repaint();
  62.       e.consume();
  63.    }
  64.  
  65.    public void paint( Graphics g ) {
  66.       if ( isButtonPressed ) {
  67.          g.setColor( Color.black );
  68.       }
  69.       else {
  70.          g.setColor( Color.gray );
  71.       }
  72.       g.fillRect( mx-20, my-20, 40, 40 );
  73.    }
  74. }

結果:

( You need to enable Java to see this applet. )

 

.

例の2:

  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.*;
  5.  
  6. public class Mouse3 extends Applet
  7.    implements MouseListener, MouseMotionListener {
  8.  
  9.    int width, height;
  10.  
  11.    // We need a place to store a list of mouse positions.
  12.    // Rather than use an array, we'll use a Vector, because
  13.    // it allows elements to be easily appended and deleted.
  14.    // (Technically, it would probably be more appropriate to
  15.    // use a LinkedList, but they're only supported by Java 1.2)
  16.    Vector listOfPositions;
  17.  
  18.    public void init() {
  19.       width = getSize().width;
  20.       height = getSize().height;
  21.       setBackground( Color.black );
  22.  
  23.       listOfPositions = new Vector();
  24.  
  25.       addMouseListener( this );
  26.       addMouseMotionListener( this );
  27.    }
  28.  
  29.    public void mouseEntered( MouseEvent e ) { }
  30.    public void mouseExited( MouseEvent e ) { }
  31.    public void mouseClicked( MouseEvent e ) { }
  32.    public void mousePressed( MouseEvent e ) { }
  33.    public void mouseReleased( MouseEvent e ) { }
  34.    public void mouseMoved( MouseEvent e ) {
  35.  
  36.       if ( listOfPositions.size() >= 50 ) {
  37.          // delete the first element in the list
  38.          listOfPositions.removeElementAt( 0 );
  39.       }
  40.  
  41.       // add the new position to the end of the list
  42.       listOfPositions.addElement( new Point( e.getX(), e.getY() ) );
  43.  
  44.       repaint();
  45.       e.consume();
  46.    }
  47.    public void mouseDragged( MouseEvent e ) { }
  48.  
  49.    public void paint( Graphics g ) {
  50.       g.setColor( Color.white );
  51.       for ( int j = 1; j < listOfPositions.size(); ++j ) {
  52.          Point A = (Point)(listOfPositions.elementAt(j-1));
  53.          Point B = (Point)(listOfPositions.elementAt(j));
  54.          g.drawLine( A.x, A.y, B.x, B.y );
  55.       }
  56.    }
  57. }

結果:

( You need to enable Java to see this applet. )

 

メインコンテンツEND ■
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Posted on Sunday, 1st August 2010 by admin

Tags: ,
Posted in Java | Comments (0) | 1,150 views

Leave a Reply