今日はマウスのクリックやドラッグなど関数を中心にして、Appletの例を挙げます。ソースを実行する前に、JDKをSunのサイトからDownloadしてください。例の1には、showStatus()がコールされて、マウスの位置はウェブブラウザのステータスバーで表示されます。例の2には、マウスはペンにされて、移動すると、線が書き出されます。
例の1:
.
.
.
- import java.applet.*;
- import java.awt.*;
- import java.awt.event.*;
- public class Mouse1 extends Applet
- implements MouseListener, MouseMotionListener {
- int width, height;
- int mx, my; // the mouse coordinates
- boolean isButtonPressed = false;
- public void init() {
- width = getSize().width;
- height = getSize().height;
- setBackground( Color.black );
- mx = width/2;
- my = height/2;
- addMouseListener( this );
- addMouseMotionListener( this );
- }
- public void mouseEntered( MouseEvent e ) {
- // called when the pointer enters the applet's rectangular area
- }
- public void mouseExited( MouseEvent e ) {
- // called when the pointer leaves the applet's rectangular area
- }
- public void mouseClicked( MouseEvent e ) {
- // called after a press and release of a mouse button
- // with no motion in between
- // (If the user presses, drags, and then releases, there will be
- // no click event generated.)
- }
- public void mousePressed( MouseEvent e ) { // called after a button is pressed down
- isButtonPressed = true;
- setBackground( Color.gray );
- repaint();
- // "Consume" the event so it won't be processed in the
- // default manner by the source which generated it.
- e.consume();
- }
- public void mouseReleased( MouseEvent e ) { // called after a button is released
- isButtonPressed = false;
- setBackground( Color.black );
- repaint();
- e.consume();
- }
- public void mouseMoved( MouseEvent e ) { // called during motion when no buttons are down
- mx = e.getX();
- my = e.getY();
- showStatus( "Mouse at (" + mx + "," + my + ")" );
- repaint();
- e.consume();
- }
- public void mouseDragged( MouseEvent e ) { // called during motion with buttons down
- mx = e.getX();
- my = e.getY();
- showStatus( "Mouse at (" + mx + "," + my + ")" );
- repaint();
- e.consume();
- }
- public void paint( Graphics g ) {
- if ( isButtonPressed ) {
- g.setColor( Color.black );
- }
- else {
- g.setColor( Color.gray );
- }
- g.fillRect( mx-20, my-20, 40, 40 );
- }
- }
結果:
.
例の2:
- import java.applet.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- public class Mouse3 extends Applet
- implements MouseListener, MouseMotionListener {
- int width, height;
- // We need a place to store a list of mouse positions.
- // Rather than use an array, we'll use a Vector, because
- // it allows elements to be easily appended and deleted.
- // (Technically, it would probably be more appropriate to
- // use a LinkedList, but they're only supported by Java 1.2)
- Vector listOfPositions;
- public void init() {
- width = getSize().width;
- height = getSize().height;
- setBackground( Color.black );
- listOfPositions = new Vector();
- addMouseListener( this );
- addMouseMotionListener( this );
- }
- public void mouseEntered( MouseEvent e ) { }
- public void mouseExited( MouseEvent e ) { }
- public void mouseClicked( MouseEvent e ) { }
- public void mousePressed( MouseEvent e ) { }
- public void mouseReleased( MouseEvent e ) { }
- public void mouseMoved( MouseEvent e ) {
- if ( listOfPositions.size() >= 50 ) {
- // delete the first element in the list
- listOfPositions.removeElementAt( 0 );
- }
- // add the new position to the end of the list
- listOfPositions.addElement( new Point( e.getX(), e.getY() ) );
- repaint();
- e.consume();
- }
- public void mouseDragged( MouseEvent e ) { }
- public void paint( Graphics g ) {
- g.setColor( Color.white );
- for ( int j = 1; j < listOfPositions.size(); ++j ) {
- Point A = (Point)(listOfPositions.elementAt(j-1));
- Point B = (Point)(listOfPositions.elementAt(j));
- g.drawLine( A.x, A.y, B.x, B.y );
- }
- }
- }
結果:
メインコンテンツEND ■
Posted on Sunday, 1st August 2010 by admin
Tags: Applet, Tutorial
Posted in Java | Comments (0) | 1,150 views
