Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.applet.*;
- import java.awt.*;
- import java.awt.event.*;
- //<applet code="Circle" height="600" width="600"> </applet>
- public class Circle extends Applet implements Runnable,KeyListener
- {
- int x=50,y=50;
- String str="Hi";
- int direction=6;
- Thread t=new Thread(this);
- public void init()
- {
- addKeyListener(this);
- setBackground(Color.yellow);
- setForeground(Color.red);
- t.start();
- }
- public void run()
- {
- for(;;)
- {
- //System.out.println("run"+i);
- repaint();
- switchDir();
- try
- {
- Thread.sleep(100);
- }
- catch(InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- }
- void switchDir()
- {
- if(x<1)
- {
- direction=6;
- return;
- }
- if(x+55>bounds().width)
- {
- direction=4;
- return;
- }
- if(y<1)
- {
- direction=2;
- return;
- }
- if(y+55>bounds().height)
- {
- direction=8;
- return;
- }
- }
- public void paint(Graphics g)
- {
- switch(direction)
- {
- case 6:
- x+=10;
- break;
- case 8:
- y-=10;
- break;
- case 2:
- y+=10;
- break;
- case 4:
- x-=10;
- break;
- }
- //g.drawString(str,x,y);
- //g.setForeground(Color.red);
- g.fillOval(x,y,50,50);
- }
- public void keyPressed( KeyEvent e )
- {
- switch(e.getKeyCode())
- {
- case KeyEvent.VK_UP: direction=8; break;
- case KeyEvent.VK_DOWN: direction=2; break;
- case KeyEvent.VK_LEFT: direction=4; break;
- case KeyEvent.VK_RIGHT: direction=6; break;
- case KeyEvent.VK_E: t.stop(); break;
- \
- }
- }
- public void keyReleased( KeyEvent e )
- {
- }
- public void keyTyped( KeyEvent e )
- {
- }
- }
Add Comment
Please, Sign In to add comment