Thu Jul 23, 2009 4:18 pm
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JApplet;
import java.util.Random;
class shootApplet extends JApplet
{
public void init()
{
setContentPane(new game() );
}
class game extends JPanel implements MouseListener
{
private int width,height;
int score=0;
int x1,x2,x3,y1,y2,y3;
int shot=0;
int b_no[]=new int[3];
int x[]=new int[3];
int y[]=new int[3];
private Ball ball;
game()
{
setBackground(Color.yellow);
ActionListener al=new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if(ball!=null)
{
ball.generateCoord();
}
repaint();
}
};
}
public void mouseClicked(MouseEvent me)
{
int x,y;
x=me.getX();
y=me.getY();
if(x==x1&&y==y1)
{
b_no[0]=0;
score=score+5;
shot++;
}
else if(x==x2&&y==y2)
{
b_no[1]=0;
score=score+5;
shot++;
}
else if(x==x3&&y==y3)
{
b_no[2]=0;
score=score+5;
shot++;
}
}
public void mousePressed(MouseEvent me) {}
public void mouseReleased(MouseEvent me){}
public void mouseEntered(MouseEvent me){}
public void mouseExited(MouseEvent me){}
public void paintComponent(Graphics g)
{
super.paintComponent(g); // Fill panel with background color, green.
if (ball==null)
{
// The first time that paintComponent is called, it assigns
// values to the instance variables.
width = getWidth();
height = getHeight();
ball=new Ball();
}
if (hasFocus())
{
g.setColor(Color.BLUE);
}
else
{
g.setColor(Color.RED);
g.drawString("Target Balls",230,300);
g.drawString("By Shilpa Dahiya",220,350);
g.drawString("CLICK TO ACTIVATE", 210, 400);
g.setColor(Color.GRAY);
}
g.drawRect(0,0,width-1,height-1);
g.drawRect(1,1,width-3,height-3);
g.drawRect(2,2,width-5,height-5);
g.setColor(Color.BLACK);
g.drawString("Score " + score, 15, 24);
}
class Ball
{
Random rand =new Random();
int start;
int z;
Ball()
{
b_no[0]=1;
b_no[1]=1;
b_no[2]=1;
this.start=1;
}
void generateCoord()
{
if(shot==3||start==1||y[0]>500||y[1]>500||y[2]>500)
{
shot=0;
z=0;
while(z<3)
{
x[z]=(int)(Math.random()*400);
y[z]=(int)(Math.random()*400);
z++;
b_no[0]=1;
b_no[1]=1;
b_no[2]=1;
}
}
}
void draw(Graphics g)
{
if(b_no[0]!=0)
{
g.setColor(Color.red);
g.drawOval(x[0],y[0],30,30);
g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
g.fillOval(x[0],y[0],30,30);
x1=x[0];
y1=y[0];
}
if(b_no[1]!=0)
{
g.setColor(Color.red);
g.drawOval(x[1],y[1],30,30);
g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
g.fillOval(x[1],y[1],30,30);
x2=x[1];
y2=y[1];
}
if(b_no[2]!=0)
{
g.setColor(Color.red);
g.drawOval(x[2],y[2],30,30);
g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
g.fillOval(x[2],y[2],30,30);
x3=x[2];
y3=y[2];
}
}
}
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.