Keyboard入力(Applet)

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

 

 

 

例の1:


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

public class Keyboard1 extends Applet
implements KeyListener, MouseListener {

int width, height;
int x, y;
String s = “”;

public void init() {
width = getSize().width;
height = getSize().height;
setBackground( Color.black );

x = width/2;
y = height/2;

addKeyListener( this );
addMouseListener( this );
}

public void keyPressed( KeyEvent e ) { }
public void keyReleased( KeyEvent e ) { }
public void keyTyped( KeyEvent e ) {
char c = e.getKeyChar();
if ( c != KeyEvent.CHAR_UNDEFINED ) {
s = s + c;
repaint();
e.consume();
}
}

public void mouseEntered( MouseEvent e ) { }
public void mouseExited( MouseEvent e ) { }
public void mousePressed( MouseEvent e ) { }
public void mouseReleased( MouseEvent e ) { }
public void mouseClicked( MouseEvent e ) {
x = e.getX();
y = e.getY();
s = “”;
repaint();
e.consume();
}

public void paint( Graphics g ) {
g.setColor( Color.gray );
g.drawLine( x, y, x, y-10 );
g.drawLine( x, y, x+10, y );
g.setColor( Color.green );
g.drawString( s, x, y );
}
}

結果:

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

 

.

例の2:


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

public class Keyboard2 extends Applet
implements KeyListener, MouseListener, MouseMotionListener {

int width, height;
int N = 25;
Color[] spectrum;
Vector listOfPositions;
String s = “”;
int skip = 0;

public void init() {
width = getSize().width;
height = getSize().height;
setBackground( Color.black );

spectrum = new Color[ N ];
for ( int i = 0; i < N; ++i ) { spectrum[i] = new Color( Color.HSBtoRGB(i/(float)N,1,1) ); } listOfPositions = new Vector(); addKeyListener( this ); addMouseListener( this ); addMouseMotionListener( this ); } public void keyPressed( KeyEvent e ) { } public void keyReleased( KeyEvent e ) { } public void keyTyped( KeyEvent e ) { char c = e.getKeyChar(); if ( c != KeyEvent.CHAR_UNDEFINED ) { s = s + c; repaint(); e.consume(); } } public void mouseEntered( MouseEvent e ) { } public void mouseExited( MouseEvent e ) { } public void mouseClicked( MouseEvent e ) { s = ""; repaint(); e.consume(); } public void mousePressed( MouseEvent e ) { } public void mouseReleased( MouseEvent e ) { } public void mouseMoved( MouseEvent e ) { // only process every 5th mouse event if ( skip > 0 ) {
— skip; // this is shorthand for “skip = skip-1;”
return;
}
else skip = 5;

if ( listOfPositions.size() >= N ) {
// 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 ) {
if ( s != “” ) {
for ( int j = 0; j < listOfPositions.size(); ++j ) { g.setColor( spectrum[ j ] ); Point p = (Point)(listOfPositions.elementAt(j)); g.drawString( s, p.x, p.y ); } } } }
結果:

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

 

.

Leave a Reply

Your email address will not be published. Required fields are marked *