20130511【Java】マウスで線をひく
お題
(アプレット)マウスで線を引く
ソース
import java.awt.*;
public class MouseDragSample2 extends java.applet.Applet {
int xpos_s, ypos_s; // 始点のx,y座標
int xpos_e, ypos_e; // 終点のx,y座標
public void init() {
setForeground(Color.blue);
}
public boolean mouseDown(Event e, int x, int y) {
xpos_s = x;
ypos_s = y;
return true;
}
public boolean mouseDrag(Event e, int x, int y) {
xpos_e = x;
ypos_e = y;
repaint();
return true;
}
public void paint(Graphics g) {
g.drawLine(xpos_s, ypos_s, xpos_e, ypos_e);
}
}
実行結果
