public class SpaceShip extends Applet implements MouseListener, MouseMotionListener, ActionListener{ AudioClip beams; private Point point = null; private final int APPLET_WIDTH = 400, APPLET_HEIGHT = 400; int x = 0, y = 0; int count = 0; Button b1 = new Button("sound on/off"); Button b2 = new Button("count reset"); Label label = new Label("shots: " + count); Image atom;
public void init(){ addMouseListener(this); addMouseMotionListener(this); b1.addActionListener(this); b2.addActionListener(this); this.add(b1); add(b2); add(label); beams = getAudioClip(getDocumentBase(),"Bullet.m4r"); setBackground(Color.blue); setSize(APPLET_WIDTH, APPLET_HEIGHT); atom = getImage(getDocumentBase(),"cg_atom.jpg"); }
public void mouseEntered(MouseEvent e){ x = e.getX(); y = e.getY(); repaint(); } public void mouseMoved(MouseEvent e){ x = e.getX(); y = e.getY(); repaint(); }
public void mouseClicked(MouseEvent e){ point = e.getPoint(); int x = e.getX(); int y = e.getY(); beams = e.getSource(); repaint(); }
public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseDragged(MouseEvent e){}
public class SpaceShip extends Applet implements MouseListener, MouseMotionListener{
AudioClip beams;
private Point point = null;
private final int APPLET_WIDTH = 400, APPLET_HEIGHT = 400;
int x = 0 , y =0 ;
int x_end = 0, y_end = 0;
int count = 0;
Button b1 = new Button("");
Button b2 = new Button("count reset");
Label label = new Label("shots: " + count);
Image atom;
public void mouseEntered(MouseEvent e){
x = e.getX();
y = e.getY();
repaint();
}
//if a mosue moves, atom follows it
public void mouseMoved(MouseEvent e){
x = e.getX();
y = e.getY();
repaint();
}
//draw the lines if a mouse is clicled
public void mousePressed(MouseEvent e){
point = e.getPoint();
repaint();
count++;
label.setText("shots: " + count);
beams.play();
}
public void mouseClicked(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseDragged(MouseEvent e){}
class b2Handler implements ActionListener{
public void actionPerformed(ActionEvent e){
//change a count to zero
count = 0;
label.setText("shots: " + count);
}
}
class b1Handler implements ActionListener{
public void paint(Graphics g){
//put atom as a space ship
int width = atom.getWidth(this);
int height = atom.getHeight(this);
g.drawImage(atom,x,y,width/2,height/2,this);
//draw four colors of lines
Random generator = new Random();
if (point !=null){
int beam = generator.nextInt();
switch (beam%4){