Skip to content

Instantly share code, notes, and snippets.

@axayjha
Created October 5, 2017 14:12
Show Gist options
  • Save axayjha/7d5eba258de831273d45ff936ed8ec4f to your computer and use it in GitHub Desktop.
Save axayjha/7d5eba258de831273d45ff936ed8ec4f to your computer and use it in GitHub Desktop.
/*
<applet code = "ball.class" width = "300" height = "300">
</applet>
*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class ball extends Applet{
Button right = new Button("Right");
Button left = new Button("Left");
Button up = new Button("Up");
Button down = new Button("Down");
public int location_x =this.getSize().width/2-30;
public int location_y =(this.getSize().height-70)/2;
public void init(){
this.setLayout(null);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(dim.width, dim.height);
down.setBounds(this.getSize().width/2 - 40, this.getSize().height-40, 80, 30);
add(down);
down.addActionListener(new downHandler());
up.setBounds(this.getSize().width/2 - 40, this.getSize().height-70, 80, 30);
add(up);
up.addActionListener(new upHandler());
left.setBounds(this.getSize().width/2-120, this.getSize().height-40, 80, 30);
add(left);
left.addActionListener(new leftHandler());
right.setBounds(this.getSize().width/2+40, this.getSize().height-40, 80, 30);
add(right);
right.addActionListener(new rightHandler());
location_x =this.getSize().width/2-30;
location_y =(this.getSize().height-70)/2;
}
public void paint(Graphics g)
{
down.setBounds(this.getSize().width/2 - 40, this.getSize().height-40, 80, 30);
up.setBounds(this.getSize().width/2 - 40, this.getSize().height-70, 80, 30);
left.setBounds(this.getSize().width/2-120, this.getSize().height-40, 80, 30);
right.setBounds(this.getSize().width/2+40, this.getSize().height-40, 80, 30);
g.setColor(Color.yellow);
g.fillOval(location_x,location_y,60,60);
}
class upHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
if(location_y>0)location_y -= 5;
repaint();
}
}
class downHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
if(location_y<Toolkit.getDefaultToolkit().getScreenSize().height-200)location_y += 5;
repaint();
}
}
class leftHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
if(location_x>0)location_x -= 5;
repaint();
}
}class rightHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
if(location_x<Toolkit.getDefaultToolkit().getScreenSize().width-200)location_x += 5;
repaint();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment