Here's the sourcecode that was shown compiled in the youtube video i've uploaded.
No changes were made in the SpriteDemoJFrame.java file so you can copy the one in the first post and compile it with this one below.
GeSHi (java):
/*--------------------------------------------------------------------------
SpriteDemoJFrame class
*****************
By: HappyFace http://www.engineeringserver.com - http://www.javaforums.net
Contact: info [@] engineeringserver.com
Version: 15/August/2008
Last updated: 17/August/2008
*****************
Note: a demo that shows collision detection using rectangles.
//----------------------------------------------------------------------*/
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
int positionX = 0, positionY = 0;
int positionMovingX = 0, positionMovingY = 0;
int playFieldX = 0, playFieldY = 0;
int movement = 0;
boolean flip = false, gotHit = false, debug = false, fliphit = false;
int speed = 0;
int size = 10;
int hit = 0;
int points = 0;
public SpriteDemo(){
addMouseMotionListener(this);
}
super.paintComponent(g);
if(debug){
System.
out.
println(positionMovingX +
" " + positionMovingY +
" " + hit
);
}
rect2.setLocation(positionX, positionY);
g2.
setColor(Color.
GREEN);
g2.fillRect(rect.x,rect.y,rect.width,rect.height);
g2.
setColor(Color.
WHITE);
g2.fillRect(positionX, positionY,rect2.width,rect2.width);
g2.
setColor(Color.
BLACK);
g2.
setFont(new Font("Arial",
Font.
BOLD,
16));
g2.drawString("You got hit " + hit + " times, you got " + points + " points", 20, 20);
}
public static void main
(String[] args
) { }
positionX = e.getX()-20;
positionY = e.getY()-20;
repaint();
}
}
public void moveRectangle(){
speed =
(int) (5+
Math.
random()*
20);
if(flip == false){
positionMovingX = positionMovingX+speed;
positionMovingY = positionMovingY+speed;
rect.setLocation(positionMovingX, positionMovingY);
}
if(positionMovingX+rect.height >= playFieldX || positionMovingY+rect.width >= playFieldY ){
flip = true;
}
if(flip == true){
speed =
(int) (1+
Math.
random()*
50);
size =
(int) (10+
Math.
random()*
100);
positionMovingX = positionMovingX-speed;
positionMovingY = positionMovingY-speed;
rect.setLocation(positionMovingX, positionMovingY);
}
if(positionMovingX <=0 || positionMovingY<=0 && flip == true){
rect.setSize(size,size);
flip = false;
}
if(rect.intersects(rect2) && gotHit == false){
hit = hit+1;
gotHit = true;
}
if(!rect.intersects(rect2) && gotHit == true){
gotHit = false;
}
if(rect2.x <=0 && rect2.y <=0 && fliphit == false){
points = points+1;
fliphit = true;
}
if(rect2.x+size/2 >= playFieldX && rect2.y+size/2 >=playFieldY && fliphit == true){
points = points+1;
fliphit = false;
}
repaint();
}
public void setPlayingField(int x, int y){
this.playFieldX = x;
this.playFieldY = y;
}
}
Created by GeSHI 1.0.7.20