Paper.java
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Event;
/** マウスの操作で絵を描くことができる Paper のクラス定義 */
public class Paper extends Applet {
/** 描きたい線の両端の座標 */
protected int x1=0, y1=0, x2=0, y2=0;
/** アプレットにグラフィックを描く。このアプレットでは、
update() を書き換えたため、最初の表示の時にだけ呼び出される。*/
public void paint( Graphics g ) {
g.setColor( Color.white );
g.fillRect( 0, 0, size().width, size().height );
}
/** アプレットにグラフィックを描く。repaint() によって呼び出される。
このアプレットでは、以前の図形のクリアやを行わない。*/
public void update( Graphics g ) {
g.setColor( Color.black ); // 色を変更
g.drawLine( x1, y1, x2, y2 ); // 線を描く
x1 = x2; // 終点を新しい始点にする
y1 = y2;
}
/** マウスボタンが押された時に自動的に呼び出される。*/
public boolean mouseDown( Event evt, int x, int y ) {
x1 = x; // 始点の座標の設定
y1 = y;
return true;
}
/** マウスボタンが押されたまま移動した時に自動的に呼び出される。*/
public boolean mouseDrag( Event evt, int x, int y ) {
x2 = x; // 終点の座標の設定
y2 = y;
repaint(); // update() を呼び出す
return true;
}
}