ClickTst.java
import java.awt.*;
import java.applet.Applet;
public class ClickTst extends Applet implements Runnable {
int FontSize=12;
int intStrPnt=3;
String zahyo="Start";
String NowZahyo="";
String mUpZahyo;
Thread th=null;
Font f=new Font("TimesRoman",Font.PLAIN,FontSize);
FontMetrics fm=getFontMetrics(f);
int FontH=fm.getHeight();
int px=300, py=150;
int qx=FontH, qy=FontH;
int rx=px+intStrPnt, ry=py+FontH/2;
int sx=qx+intStrPnt, sy=qy-FontH/2;
int tx=px, ty=py;
int ax=2, ay=2;
int RecLen=30;
int flag=0;
Button bt,bt2;
Label lb;
TextField tf,tf2,tf3;
Label lb2;
int slptime=100;
int mDownflg=0;
int vx,vy,wx,wy;
public void init() {
Choice c=new Choice();
c.addItem("5");
c.addItem("10");
c.addItem("100");
c.addItem("500");
c.addItem("1000");
c.select("100");
add(new Label("ax="));
add(tf=new TextField("2"));
add(lb=new Label("ay="));
add(tf2=new TextField("2"));
add(bt=new Button("Change"));
add(lb2=new Label("sleep time="));
add(c);
add(new Label("Font Size="));
add(tf3=new TextField("10"));
add(bt2=new Button("Change"));
}
public void start() {
if (th==null) {
th=new Thread(this);
th.start();
}
}
public void run() {
while (true) {
if (mDownflg==0) {
if (tx<=0 || tx>=size().width-RecLen-2) ax=-ax;
if (ty<=0 || ty>=size().height-RecLen-2) ay=-ay;
if (tx<0) tx=1;
if (ty<0) ty=1;
if (tx>size().width-RecLen-2) tx=size().width-RecLen-3;
if (ty>size().height-RecLen-2) ty=size().height-RecLen-3;
tx+=ax;
ty+=ay;
}
repaint();
try {
th.sleep(slptime);
}
catch (InterruptedException e) {}
}
}
/*
public void update(Graphics g) {
setBackground(Color.gray); repaint();
paint(g);
}
*/
public void paint(Graphics g) {
g.setFont(f);
if (flag!=0) {
g.drawRect(qx-2,qy-2,4,4);
g.drawLine(qx,0,qx,size().height);
g.drawLine(0,qy,size().width,qy);
g.drawString(NowZahyo,sx,sy);
g.drawOval(vx,vy,4,4);
g.drawString(mUpZahyo,wx,wy);
}
g.drawRect(px-2,py-2,4,4);
g.drawString(zahyo,rx,ry);
g.drawLine(0,0,size().width,0);
g.drawLine(size().width-1,0,size().width-1,size().height-1);
g.drawLine(size().width-1,size().height-1,0,size().height-1);
g.drawLine(0,size().height,0,0);
g.drawString("(" + size().width + "," + size().height + ")",FontH,FontH*3);
if (ax*ay>0)
g.drawRect(tx,ty,RecLen,RecLen);
else
g.fillRect(tx,ty,RecLen,RecLen);
g.drawString(tf.getText(),50,50);
g.drawString(" "+ax+" "+ay,50,70);
showStatus("("+(tx+RecLen/2)+","+(ty+RecLen/2)+")");
}
public boolean action(Event e, Object o) {
if (e.target==bt && (e.id&Event.MOUSE_DOWN)!=0) {
ax=Integer.parseInt(tf.getText());
ay=Integer.parseInt(tf2.getText());
}
if (e.target instanceof Choice) {
slptime=Integer.parseInt((String)o);
}
if (e.target==bt2 && (e.id&Event.MOUSE_DOWN)!=0) {
FontSize=Integer.parseInt(tf3.getText());
f=new Font("TimesRoman",Font.PLAIN,FontSize);
fm=getFontMetrics(f);
}
return true;
}
public boolean mouseDown(Event e, int x, int y){
mDownflg=1;
px=x; py=y;
tx=x-RecLen/2; ty=y-RecLen/2;
rx=px+intStrPnt; ry=py+FontH/2;
zahyo="("+px+","+py+")";
int w=fm.stringWidth(zahyo);
if (py<FontH/2) ry=FontH;
if (py>size().height-FontH/2-2) ry=size().height-2;
if (size().width-px<w+intStrPnt+1) {
rx=px-w-intStrPnt;
}
//repaint();
return true;
}
public boolean mouseDrag(Event e, int x, int y){
tx=x-RecLen/2;
ty=y-RecLen/2;
mouseMove(e,x,y);
return true;
}
public boolean mouseUp(Event e,int x,int y) {
mDownflg=0;
mUpZahyo="("+x+","+y+")";
int w=fm.stringWidth(mUpZahyo);
wx=x+intStrPnt; wy=y+FontH/2;
if (y<FontH/2) wy=FontH;
if (y>size().height-FontH/2-2) wy=size().height-2;
if (size().width-x<w+intStrPnt+1) {
wx=x-w-intStrPnt;
}
vx=x-2; vy=y-2;
repaint();
return true;
}
public boolean mouseMove(Event e, int x, int y){
flag=1;
qx=x; qy=y;
sx=x+intStrPnt; sy=y-FontH/2;
NowZahyo="(" + qx + "," + qy + ")";
int w=fm.stringWidth(NowZahyo);
if (qy<FontH+intStrPnt) {
sy=qy+FontH;
if (qx<w+intStrPnt+1)
sx=qx+FontH*2;
else
sx=qx-w-intStrPnt;
}
if (size().width-qx<w+intStrPnt+1) {
sx=qx-w-intStrPnt;
}
if ((x==tx || x==tx+RecLen) && (y>ty && y<ty+RecLen)) ax=-ax;
if ((y==ty || y==ty+RecLen) && (x>tx && x<tx+RecLen)) ay=-ay;
repaint();
return true;
}
public void stop() {
if (th!=null) {
th.stop();
th=null;
}
}
}