Total members 11889 |It is currently Thu Mar 28, 2024 10:56 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Applet Game to Shot a Random ball
java code
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];
}

}
}
}
}





Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Applet Game To Shot a Random Ball
 ping pong game - java-Sticker-ball game (modified v1.1)     -  
 Ball sticker applet     -  
 Ball Sticker{Applet}     -  
 Bouncing Coloring Ball Game(V1.0)     -  
 Ball Color Match 2D Java Game Version 1.0     -  
 ping pong java code-Sticker-ball game     -  
 Applet Dice Game Fruit Machine simulation     -  
 fill all Squares of game board - 2d Java Game Example     -  
 Sticker Ball     -  
 bouncing ball in flash     -  



Topic Tags

Java Applet
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
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