- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
package t;import java.awt.*;import java.awt.event.*;import javax.swing.*;class M
extends JFrame implements Runnable{int T=100,s,W=500,H=W,x=T,l=10,y,i,S,L;M(){
setBounds(T,T,W,H);new Thread(this).start();}public void paint(Graphics g){super
.paint(g);g.drawOval(x,y,l,l);g.drawRect(i,H-l,T,l);g.drawString("Sco "+s,l,T);}
public void processKeyEvent(KeyEvent e){int k=e.getKeyCode();if(k==37)i-=l;if(k
==39)i+=l;super.processKeyEvent(e);}public static void main(String[]args){new M(
).setVisible(true);}public void run(){while(true){try{Thread.sleep(5);}catch
(Exception e){}x+=S==0?1:-1;y+=L==0?1:-1;if(y>H-l){if(x>i&&x<i+T){s++;}else{S=0;
L=0;x=T;y=0;}}if(x>W-l)S=1;if(x<l)S=0;if(y>H-l)L=1;if(y<l)L=0;repaint();}}}