Paper.java
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
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() によって呼び出される
* このアプレットでは通常とは異なり、以前の図形のクリアや、
* paint() の呼び出しを行わない
*/
public void update( Graphics g ) {
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;
}
}