In this tutorial we will start with the double buffer from Double Buffer Animation with Java, and just the mouseListener from Create an Expolsion in Java. But before we deal with the main class, let's take a look at the Shuriken class that will control our shurikens.
import java.lang.Math;
public class Shuriken {
private double px = 110;
private double py = 220;
private double angle;
private int speed = 10;
public boolean fired;
public Shuriken() {
}
public void setAngle(double angle){
this.angle = angle;
}
public void moveShuriken(){
px+= speed * Math.cos(angle);
py+= speed * Math.sin(angle);
if(px < 0 || px > 250 || py < 0 || py > 250){
px = 110;
py = 220;
fired = false;
}
}
public double getX(){
return this.px;
}
public double getY(){
return this.py;
}
}
We have variables for the shuriken's x and y positions, angle and speed, along with some getter and setter methods. The moveShuriken() method moves the shuriken at an angle specified by the angle variable using the Math.sin and Math.cos methods. If the shuriken is off-screen, it's position is reset and it's fired Boolean is set to false. Next, let's take a look at the main class.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Projectile extends Applet{
public static final int WIDTH = 250;
public static final int HEIGHT = 250;
Graphics backBuffer;
Image frontBuffer;
Dimension appletSize;
Image [] ninjaStar = new Image[20];
Shuriken [] shurikens = new Shuriken[20];
public void init(){
this.setSize(WIDTH, HEIGHT);
appletSize = getSize();
addMouseListener(new MyMouseListener());
frontBuffer = createImage(appletSize.width,appletSize.height);
backBuffer = frontBuffer.getGraphics();
for(int i=0; i<20; i++){
shurikens[i] = new Shuriken();
ninjaStar[i] = getImage(getDocumentBase(), "shuriken.png");
}
Thread t = new Thread(new MainLoop());
t.start();
}
private class MyMouseListener extends MouseAdapter{
public void mousePressed(MouseEvent evt){
for(int i=0; i<20; i++){
if(!shurikens[i].fired){
shurikens[i].fired = true;
double angle = Math.atan2(evt.getY()-shurikens[i].getY(),
evt.getX()-shurikens[i].getX());
shurikens[i].setAngle(angle);
break;
}
}
}
}
public class MainLoop implements Runnable{
public MainLoop(){
}
public void run(){
while(true){
for(int i=0; i<20; i++){
if(shurikens[i].fired){
shurikens[i].moveShuriken();
}
}
repaint();
try {
Thread.sleep(20);
}
catch (InterruptedException ex){
}
}
}
}
public void drawScreen(Graphics display){
backBuffer.clearRect(0,0,appletSize.width,appletSize.height);
for(int i=0; i<20; i++){
backBuffer.drawImage(ninjaStar[i],(int)shurikens[i].getX(),
(int)shurikens[i].getY(),null);
}
backBuffer.drawString("***** Mouse to aim, click to throw *****",10,10);
display.drawImage(frontBuffer,0,0,this);
}
public void update(Graphics display){
drawScreen(display);
}
}
First, we have an array of 20 shuriken Images, and a corresponding array of 20 shuriken objects, both of which are initialized in the init() method.
When the mouse is clicked, a for-loop searches the shuriken array for an inactive shuriken by checking if the shuriken's fired Boolean is false or not. Once an inactive shuriken is found, it's angle is set by passing the mouse's current position into the atan2 method. Then the shuriken's fired Boolean is set to true.
In the main loop we use a for-loop again to see which shurikens are active and eligible for movement.
Finally, in the drawScreen method, each shuriken is draw to the screen using it's current x and y positions.
You might also be interested in





