Java AppletはJava言語で作成したアップリケショーンソース、直接画面に挿入し、Jabaサポートするブラウザで実行できます。Appletはユーザのパソコンで実行するため、実行スピードはバンド幅とモデムスピードに影響されません。今日の例は二つがあります。例の1は、マウスをクリックしてから、文字を入力できます。例の2は文字を入力すると、多彩に表現されて、マウスを移動すると、移動軌跡も書かされます。各種の構図は自由に出来ます。

 

 

 

例の1:

  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class Keyboard1 extends Applet
  6.    implements KeyListener, MouseListener {
  7.  
  8.    int width, height;
  9.    int x, y;
  10.    String s = "";
  11.  
  12.    public void init() {
  13.       width = getSize().width;
  14.       height = getSize().height;
  15.       setBackground( Color.black );
  16.  
  17.       x = width/2;
  18.       y = height/2;
  19.  
  20.       addKeyListener( this );
  21.       addMouseListener( this );
  22.    }
  23.  
  24.    public void keyPressed( KeyEvent e ) { }
  25.    public void keyReleased( KeyEvent e ) { }
  26.    public void keyTyped( KeyEvent e ) {
  27.       char c = e.getKeyChar();
  28.       if ( c != KeyEvent.CHAR_UNDEFINED ) {
  29.          s = s + c;
  30.          repaint();
  31.          e.consume();
  32.       }
  33.    }
  34.  
  35.    public void mouseEntered( MouseEvent e ) { }
  36.    public void mouseExited( MouseEvent e ) { }
  37.    public void mousePressed( MouseEvent e ) { }
  38.    public void mouseReleased( MouseEvent e ) { }
  39.    public void mouseClicked( MouseEvent e ) {
  40.       x = e.getX();
  41.       y = e.getY();
  42.       s = "";
  43.       repaint();
  44.       e.consume();
  45.    }
  46.  
  47.    public void paint( Graphics g ) {
  48.       g.setColor( Color.gray );
  49.       g.drawLine( x, y, x, y-10 );
  50.       g.drawLine( x, y, x+10, y );
  51.       g.setColor( Color.green );
  52.       g.drawString( s, x, y );
  53.    }
  54. }

結果:

( 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.Vector;
  5.  
  6. public class Keyboard2 extends Applet
  7.    implements KeyListener, MouseListener, MouseMotionListener {
  8.  
  9.    int width, height;
  10.    int N = 25;
  11.    Color[] spectrum;
  12.    Vector listOfPositions;
  13.    String s = "";
  14.    int skip = 0;
  15.  
  16.    public void init() {
  17.       width = getSize().width;
  18.       height = getSize().height;
  19.       setBackground( Color.black );
  20.  
  21.       spectrum = new Color[ N ];
  22.       for ( int i = 0; i < N; ++i ) {
  23.          spectrum[i] = new Color( Color.HSBtoRGB(i/(float)N,1,1) );
  24.       }
  25.  
  26.       listOfPositions = new Vector();
  27.  
  28.       addKeyListener( this );
  29.       addMouseListener( this );
  30.       addMouseMotionListener( this );
  31.    }
  32.  
  33.    public void keyPressed( KeyEvent e ) { }
  34.    public void keyReleased( KeyEvent e ) { }
  35.    public void keyTyped( KeyEvent e ) {
  36.       char c = e.getKeyChar();
  37.       if ( c != KeyEvent.CHAR_UNDEFINED ) {
  38.          s = s + c;
  39.          repaint();
  40.          e.consume();
  41.       }
  42.    }
  43.  
  44.    public void mouseEntered( MouseEvent e ) { }
  45.    public void mouseExited( MouseEvent e ) { }
  46.    public void mouseClicked( MouseEvent e ) {
  47.       s = "";
  48.       repaint();
  49.       e.consume();
  50.    }
  51.    public void mousePressed( MouseEvent e ) { }
  52.    public void mouseReleased( MouseEvent e ) { }
  53.    public void mouseMoved( MouseEvent e ) {
  54.  
  55.       // only process every 5th mouse event
  56.       if ( skip > 0 ) {
  57.          -- skip;  // this is shorthand for "skip = skip-1;"
  58.          return;
  59.       }
  60.       else skip = 5;
  61.  
  62.       if ( listOfPositions.size() >= N ) {
  63.          // delete the first element in the list
  64.          listOfPositions.removeElementAt( 0 );
  65.       }
  66.  
  67.       // add the new position to the end of the list
  68.       listOfPositions.addElement( new Point( e.getX(), e.getY() ) );
  69.  
  70.       repaint();
  71.       e.consume();
  72.    }
  73.    public void mouseDragged( MouseEvent e ) { }
  74.  
  75.    public void paint( Graphics g ) {
  76.       if ( s != "" ) {
  77.          for ( int j = 0; j < listOfPositions.size(); ++j ) {
  78.             g.setColor( spectrum[ j ] );
  79.             Point p = (Point)(listOfPositions.elementAt(j));
  80.             g.drawString( s, p.x, p.y );
  81.          }
  82.       }
  83.    }
  84. }

結果:

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

 

.

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

Posted on Sunday, 8th August 2010 by admin

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

Leave a Reply