can any one help!!
Expand Post
(Toggle Plain Text)
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import javax.swing.*;
import java.awt.Frame;
import java.awt.geom.*;
class BallEntity/*THE BALL CLASS*/
{
private int top=0,left=0,height=0,width=0;
//getter methods
public int getTop()
{
return top;
}
public int getLeft()
{
return left;
}
public int getHeight()
{
return height;
}
public int getWidth()
{
return width;
}
//setter methods
public void setTop(int t)
{
top=t;
}
public void setLeft(int l)
{
left=l;
}
public void setHeight(int h)
{
height=h;
}
public void setWidth(int w)
{
width=w;
}
}
class Triangle extends Polygon /*THE TRIANGLE CLASS*/
{
public Triangle(int x1, int y1, int x2, int y2, int x3, int y3)
{
super(new int[] {x1, x2, x3}, new int[] {y1, y2, y3}, 3);
}
}
public class Mygame extends Frame implements Runnable ,KeyListener /*Main class,KeyListner interface implemented to capture keyevents*/
{
Shape circle1 = new Ellipse2D.Float(250.0f, 210.0f, 500.0f, 500.0f);/*MAKING OF OUTER CIRCLE*/
Shape circle2 = new Ellipse2D.Float(270.0f, 230.0f, 460.0f, 460.0f);/*MAKING OF INNER CIRCLE,Shape an interface,Ellipse2D.Float a class*/
Shape triangle = new Triangle(400, 500, 450, 500, 425, 550);
BallEntity ball;
public void initBall()/*INITIALIZATION OF BALL*/
{
ball.setTop(500);
ball.setLeft(500);
ball.setHeight(40);
ball.setWidth(40);
}
public void paint(Graphics g)/*MAKING SHAPES IN THE FRAME CLASS*/
{
Graphics2D ga = (Graphics2D)g;
ga.draw(circle1);
ga.setPaint(Color.black);
ga.setPaint(Color.black);
ga.draw(circle2);
/* ga.drawOval(ball.getLeft(),ball.getTop(),ball.getHeight(),ball.getWidth());*/
ga.setColor(Color.black);
ga.fillOval(ball.getLeft(),ball.getTop(),ball.getHeight(),ball.getWidth());
ga.draw(triangle);
}
public static void main(String[] args)
{
Mygame obj=new Mygame();
obj.addWindowListener(new WindowAdapter()/*closing event*/
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
Mygame()/*constructor,making of frame,starting of thread,*/
{
this.setSize(500,800);
this.setVisible(true);
this.setTitle("My Game");
ball=new BallEntity();
initBall();
Thread th=new Thread(this);
th.start();
this.addKeyListener(this);
}
public void keyPressed(KeyEvent e) /*KEY BOARD MOVEMENTS CAPTURED*/
{
int Key_Code=e.getKeyCode();
int d=5;
switch(Key_Code)
{
case java.awt.event.KeyEvent.VK_LEFT:
triangle= new Triangle(400-d, 500, 450-d, 500, 425-d, 550);
break;
case java.awt.event.KeyEvent.VK_RIGHT:
triangle= new Triangle(400+d, 500, 450+d, 500, 425+d, 550);
break;
case java.awt.event.KeyEvent.VK_UP:
triangle= new Triangle(400, 500-d, 450, 500-d, 425, 550-d);
break;
case java.awt.event.KeyEvent.VK_DOWN:
triangle = new Triangle(400, 500+d, 450, 500+d, 425, 550+d);
break;
}
this.repaint();
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {
}
public void run()
{
//variables for storing direction of the ball and its amount of distance
//by which we move it in the next movement
int direction=5; //1 north
//2 south
//3 east
//4 west
//5 north east
//6 north west
//7 south east
//8 south west
int dist1=10;
int dist2=5;
//continue till the program ends
while(true)
{
//just a temporary variable for storing intermediate ball position
int tempTop=0;
int tempLeft=0;
//if direction is set to go up i.e. 2 then subtract distance
//if direction is set to go down then add distance
switch(direction)
{
case 1: //north
tempTop=ball.getTop()+dist1;
break;
case 2 : //south
tempTop=ball.getTop()-dist1;
break;
case 3: //east
tempTop=ball.getLeft()-dist2;
break;
case 4: //west
tempTop=ball.getLeft()+dist2;
break;
case 5: //north east
tempTop=ball.getTop()-dist1;
tempLeft=ball.getLeft()-dist2;
break;
case 6: //north west
tempTop=ball.getTop()-dist1;
tempLeft=ball.getLeft()+dist2;
break;
case 7: //south east
tempTop=ball.getTop()+dist1;
tempLeft=ball.getLeft()-dist2;
break;
case 8: //south west
tempTop=ball.getTop()+dist1;
tempLeft=ball.getLeft()+dist2;
break;
}
//update the balls position in the datastructure
ball.setTop(tempTop);
ball.setLeft(tempLeft);
//if you have reached bottom change directions
if(ball.getTop()>=350 && direction==1) //coming north now go south
direction=2;
//if you have reached the top change directions
if(ball.getTop()<=50 && direction==2) //coming south now go north
direction=1;
if(ball.getLeft()<=50 && direction==3)
//coming east now go west
direction=4;
if(ball.getLeft()>=450 && direction==4)
//coming east now go west
direction=3;
if( (ball.getLeft()<=50 || ball.getTop()<=50) &&
direction==5)
//coming north east now go south west
direction=8;
if((ball.getLeft()>=450 || ball.getTop()<=50) &&
direction==6)
//coming north west now go south east
direction=7;
if((ball.getLeft()<=50 || ball.getTop()>=350) &&
direction==7)
//coming south east now go north west
direction=6;
if((ball.getLeft()>=450 || ball.getTop()>=350 ) &&
direction==8)
//coming north east now go south west
direction=5;
//redraw the ball at new locations
this.repaint();
//putting the thread to sleep so we can create some delay
//so the ball's motion looks natural.
try
{
Thread.sleep(300);
//maikng the thread sleep for while to make d motion of d ball look a bit real
}
catch(Exception e)
{
System.out.println("Error in running thread " + e);
}
}
}
}
Hi..In this game m trying to make the ball bounce inside the circles and move in random direction.Not able to do so. And also am trying to make the triangle move.When i press the directional keys the movements are there for just one time.Any help is appreciated. And if possible i also have to shoot small balls from the triangle and when it hits the ball the ball should divide into 2 parts. Any ideas will be a help.