About a year ago, I was brushing my java programming skills. Mr. K, my colleague from the next cubicle, a seasoned java professional was helping me.

At this very time, my mom came to visit us and she brought this game for my daughter.

So getting bored of creating dummy classes for fictitious banks and its clients to learn java oops programming, I decided to make the game my daughter was playing with physical skicks to something that can be played on the browser.

This is the code that I developed. Not **complete game** but a decent effort that taught me more than I ever learnt from those dry tutorial examples.

**And if you would like to play then, visit this page. **

Here’s the full code of the game for anyone to play and improve upon.

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Stroke;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Line2D;
import javax.swing.JApplet;
public class StickFree extends JApplet implements MouseListener {
/**
*
*/
private static final long serialVersionUID = 1L;
Image img;
Graphics off;
int w,h,nsticks,stickdetected;
Stick sticks[];
public void init(){
int x1,y1,x2,y2,tmp;
w=400;
h=320;
nsticks=8;
stickdetected = 0;
setBackground(Color.black);
setSize(w,h);
img = createImage(w,h);
off = img.getGraphics();
addMouseListener(this);
sticks = new Stick[nsticks];
for (int i=0;i<nsticks;i++){ //for (int i=nsticks-1;i>=0;i--){
x1 = (int) (Math.random()*w/2-1);
y1 = (int) (Math.random()*h-1);
x2 = (int) x1+w/2 ;//(Math.random()*w-1);
y2 = (int) (Math.random()*h-1);
tmp = (int) (Math.random()*5+1);
switch(tmp) {
case 1:sticks[i] = new Stick(i,x1,y1,x2,y2, Color.red, off);break;
case 2:sticks[i] = new Stick(i,x1,y1,x2,y2, Color.green, off);break;
case 3:sticks[i] = new Stick(i,x1,y1,x2,y2, Color.white, off);break;
case 4:sticks[i] = new Stick(i,x1,y1,x2,y2, Color.yellow, off);break;
default:sticks[i] = new Stick(i,x1,y1,x2,y2, Color.pink, off);break;
}
sticks[i].paint();
}
}
// public boolean isontop(int k){
// boolean ans =true;
// int i=0;
//
// for (i=nsticks-1;i>=0;i--){
// if(i == k) continue;
// if(sticks[i].isVisible()){
//
// }
// }
//
//
// return ans;
//
// }
public boolean isontop(Stick sk1){
int index = sk1.getId();
System.out.println("Index ...from isontop ....."+index);
//boolean ans;
Stick sk2 = null;
double x ;
double y ;
for (int i= index; i<nsticks-2;i++){
sk2 = sticks[i+1];
if(sk2.isVisible()){
x = (sk2.intercept - sk1.intercept) / (sk1.slope - sk2.slope);
y = (sk2.slope * x) + sk2.intercept;
////
double a,b,c;
a = sk1.slope*x+sk1.intercept-y;
b = Math.pow(Math.pow(sk1.slope,2.0)+1.0,0.5);
c = Math.abs(a/b);
// System.out.println("c = "+c);
if(c <= 2.0) {
return false;
}
// System.out.println("cccc"+c+" "+b+" "+a+"slope "+slope+"in "+intercept);
//return ans;
////
}
}
return true;
}
public void paint(Graphics g){
g.drawImage(img, 0, 0, this);
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
int xx,yy;
xx=e.getX();
yy=e.getY();
System.out.println(xx+ " "+ yy+" n "+stickdetected);
if(stickdetected >=nsticks) return;
for(int k=0;k<nsticks;k++){
System.out.println(k+" .... order ... ....."+sticks[k].getId());
if(sticks[k].isVisible()){
if(sticks[k].isinside(xx,yy)){
System.out.println("Index ... ....."+k);
if(isontop(sticks[k])){
sticks[k].setVisible(false);
stickdetected++;
break;
}
}
}
}
repaint();
paintall();
}
public void paintall(){
for(int i=0;i<nsticks;i++){
sticks[i].paint();
}
}
}
class Stick {
private int x0,y0,x1,y1,id,top,down;
private Color col;
private Graphics g;
private boolean visible;
double slope,intercept;
Line2D lineShape = null;
public Stick(int id_,int x0_,int y0_,int x1_,int y1_,Color col_,Graphics g_){
id = id_;
x0=x0_;
y0=y0_;
x1=x1_;
y1=y1_;
col =col_;
g=g_;
visible = true;
slope = ((double)(y1-y0)/(double)(x1-x0));
intercept = y0-slope*x0;
top=-1;
down=-1;
// System.out.println("id "+id+" x0 "+x0+" x1 "+x1+" y0 "+y0+" y1 "+y1+" slop "+slope+ " in "+ intercept);
lineShape = new Line2D.Float((float)x0,(float)y0,(float)x1,(float)y1);
}
public void paint(){
if(visible){
//((Graphics2D)g).
g.setColor(col);
((Graphics2D)g).setStroke(new BasicStroke(3));
for(int i=0;i<2;i++){
g.drawLine(x0+i, y0+i, x1+i, y1+i);
}
g.drawString(""+this.id, x0, y0);
}else{
g.setColor(Color.black);
}
}
public void clear(){
g.setColor(Color.black);
for(int i=0;i<2;i++){
g.drawLine(x0+i, y0+i, x1+i, y1+i);
}
}
public boolean isVisible() {
return visible;
}
public void setVisible(boolean visible) {
this.clear();
this.visible = visible;
}
public boolean isinside(int x,int y){
boolean ans=false;
double a,b,c;
a = slope*x+intercept-y;
b = Math.pow(Math.pow(slope,2.0)+1.0,0.5);
c = Math.abs(a/b);
// System.out.println("c = "+c);
if(c <= 2.0) ans=true;
// System.out.println("cccc"+c+" "+b+" "+a+"slope "+slope+"in "+intercept);
return ans;
// double yyyy = slope * (x0-x) + y;
//
// System.out.println("x0 "+x0+" y0 "+y0+" & x1 "+x1+" y1 "+y1+" ------------ x "+x+" y "+y+" ??? "+ yyyy);
//return (y0==yyyy);
}
public Line2D getLineShape() {
return lineShape;
}
public int getId() {
return id;
}
}

And if you would like to play then, visit this page.

I wish WordPress.com allowed embedding java applets. ;(

### Like this:

Like Loading...