classes/Canvas.java
import browser.Applet;
import awt.Graphics;
import awt.Color;
/**
マウスの操作で絵を描くことができる Canvas のクラス定義
*/
class Canvas extends Applet {
/**
描きたい線の両端の座標
*/
private int x1=0, y1=0, x2=0, y2=0;
/**
初期設定を行う。サイズの設定のみ。
*/
protected void init() {
resize( 600, 400 );
}
/**
アプレットにグラフィックを描く。このアプレットでは、
update() を書き換えたため、最初の表示の時にだけ呼び出される。
*/
public void paint( Graphics g ) {
g.setForeground( Color.white );
g.paint3DRect( 0, 0, 600, 400, true, true );
}
/**
アプレットにグラフィックを描く。repaint() によって呼び出される。
このアプレットでは通常とは異なり、以前の図形のクリアや、
paint() の呼び出しを行わない。
*/
public void update( Graphics g ) {
g.drawLine( x1, y1, x2, y2 ); // 線を描く
x1 = x2;
y1 = y2;
}
/**
マウスボタンが押された時に自動的に呼び出される。
*/
public void mouseDown( int x, int y ) {
x1 = x;
y1 = y;
}
/**
マウスボタンが放された時に自動的に呼び出される。
*/
public void mouseDrag( int x, int y ) {
x2 = x;
y2 = y;
repaint(); // update() を呼び出す
}
}