Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* keyPressedClassUI.java
*
* Created on Sep 29, 2010, 8:05:53 AM
*/
package pacManPlan;
import java.awt.BasicStroke;
import java.aw*****lor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
/**
*
* @author Administrator
*/
public class pacManPlanUI extends javax.swing.JFrame
implements KeyListener, Runnable {
Graphics draw;
int x = 50, y = 110;
int start = 40, angle = 280;
boolean okMove = false;//thread control flag
long delay = 10; //fps
int xMove = 0;
int yMove = 0;
Thread move = null;
long time;// = System.currentTimeMillis();
Graphics2D draw2d;
Polygon borderLeft = new Polygon();
Polygon borderRight= new Polygon();
Polygon borderbot = new Polygon();
Polygon triangle = new Polygon();
// int xOffset, yOffset;
BasicStroke wallStroke = new BasicStroke(10,
BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND);
//fix the tunnelling problem
int pressed, clipDir;
//dots
Ellipse2D dots[] = new Ellipse2D[10];
Ellipse2D.Double ghost = new Ellipse2D.Double();
Rectangle ghostRect = new Rectangle();
Rectangle ghostRect2 = new Rectangle();//draw ghost
int gx = 100;
int gy = 40;
int gxm = -1; //ghost movement
int gym = -1;
public void setDots() {
for (int x = 0; x < dots.length - 1; x++) {
dots[x] = new Ellipse2D.Double(); //subclass
dots[x].setFrame(x * 30, 40, 10, 10);
}
}
public void setMaze() {
//left border
borderLeft.addPoint(0, 0);
borderLeft.addPoint(0, 600);
borderLeft.addPoint(10, 600);
borderLeft.addPoint(10, 0);
borderLeft.addPoint(0, 0);//back to starting point
borderRight.addPoint(600,600);
borderRight.addPoint(600,0);
borderRight.addPoint(600,10);
borderRight.addPoint(600,600);
borderbot.addPoint(0, 600);
borderbot.addPoint(10, 600);
//draw.fillRect(x, y, WIDTH, start);
triangle.addPoint(50, 50);
triangle.addPoint(25, 100);
triangle.addPoint(75, 100);
triangle.addPoint(50, 50);
}
public void clipMaze() {
Rectangle pacRec = new Rectangle(x - 5, y - 5, 25, 25);
//clipDir is a flag
clipDir = -1;
if (borderLeft.intersects(pacRec)) {
xMove = 0;
yMove = 0;
clipDir = pressed;
//set the direction of travel when clipping
}
if (triangle.intersects(pacRec)) {
xMove = 0;
yMove = 0;
clipDir = pressed;
}
//determine if ghost clips
//determine which direction to go
//random - bad idea
//move in an intelligent direction
//bounce back if clip
switch (clipDir) {
case 37:
case 65:
x -= 2;
okMove = false;
//break;
case 38:
y -= 2;
okMove = false;
// break;
case 39:
x += 2;
okMove = false;
// break;
case 40:
y += 2;
okMove = false;
}
for (int x = 0; x < dots.length - 1; x++) {
if (dots[x].intersects(pacRec)) {
draw2d.setColor(Color.black);
draw2d.fill(dots[x]);
dots[x].setFrame(-100, -100, 0, 0);
}
}
}
/** Creates new form keyPressedClassUI */
public pacManPlanUI() {
initComponents();
draw = this.jPanel1.getGraphics();
addKeyListener(this);
move = new Thread(this);
move.start();
draw2d = (Graphics2D) draw;
ghostRect.setSize(30, 30);
ghostRect2.setSize(20, 20);
setMaze();
setDots();
}
public void paint(Graphics g) {
super.paint(g);
//kill flicker
g.setColor(Color.black);
g.fillRect(0, 0, jPanel1.getWidth(), jPanel1.getHeight());
update(draw);
//update reduces flicker in animation
}
public void update(Graphics g) {
//erase pac
g.setColor(Color.black);
g.fillOval(x - 3, y - 3, 25, 25);
g.setColor(Color.yellow);
g.fillArc(x, y, 20, 20, start, angle);
g.setColor(Color.red);
draw2d.setStroke(wallStroke);
draw2d.drawPolygon(triangle);
draw2d.drawPolygon(borderLeft);
draw2d.drawPolygon(borderRight);
//draw ghost
draw2d.setColor(Color.black);
draw2d.fillOval(gx-2, gy -2, 35,35);
draw2d.setColor(Color.pink);
ghostRect.setLocation(gx, gy);
ghost.setFrame(ghostRect);
draw2d.fill(ghost);
//ghost2
draw2d.setColor(Color.black);
draw2d.fillOval(gx-2, gy -2, 35,35);
draw2d.setColor(Color.green);
ghostRect.setLocation(150, 25);
ghost.setFrame(ghostRect2);
draw2d.fill(ghost);
draw2d.setColor(Color.white);
//draw dots
for (int x = 0; x < dots.length - 1; x++) {
draw2d.fill(dots[x]);
}
// clipMaze();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setMaximumSize(new java.awt.Dimension(600, 600));
jPanel1.setMinimumSize(new java.awt.Dimension(600, 600));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 808, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 600, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
//java.awt.EventQueue.invokeLater(new Runnable() {
// public void run() {
new pacManPlanUI().setVisible(true);
// }
//});
}
public void keyTyped(KeyEvent e) {
int pressed = e.getKeyCode();
String keyP = String.valueOf(pressed);
}
public void keyPressed(KeyEvent e) {
int pressed = e.getKeyCode();
String keyP = String.valueOf(pressed);
//this.setTitle(okMove + " ");
}
public void keyReleased(KeyEvent e) {
pressed = e.getKeyCode();
String keyP = String.valueOf(pressed);
//okMove = true;
// draw.clearRect(0,0,100,100);
// draw.drawString(keyP + " pressed", 10, 30);
// draw.clearRect(0,0,100,100);
// draw.drawString(keyP + " released", 10,50);
//37 = left = 100 number pad 4 = 65 A
//38 = up
//39 = right
//40 = down
//arrow keys
//start the thread
// if(move == null)
//{
//stop();
//move.stop();
//requires a Runnable frame
//requires the run method
// }
// okMove = false;
this.setTitle(pressed + " ");
if (pressed == 37 || pressed == 100 || pressed == 65) {
//draw.drawString("went left", 10, 70);
//x-=2;
//redraw the screen
start = 220;
//repaint();
xMove = -2;
yMove = 0;
okMove = true;
}
if (pressed == 39) {
start = 40;
// x += 2;
// repaint();
xMove = 2;
yMove = 0;
okMove = true;
}
//up
if (pressed == 38) {
start = 130;
// y -= 2;
// repaint();
xMove = 0;
yMove = -2;
okMove = true;
}
//down
if (pressed == 40) {
start = 310;
// y += 2;
// repaint();
xMove = 0;
yMove = 2;
okMove = true;
}
if (pressed == 32) {
okMove = false;
}
if (okMove) {
time = System.currentTimeMillis();
//reset when the movement starts
}
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
// End of variables declaration
public void run() {
//throw new UnsupportedOperationException("Not supported yet.");
//long time = System.currentTimeMillis();
//get the time the thread is started
long sleepInterval = delay; //set to fps
//this.setTitle(okMove + "in run");
//int counter = 0;
while (true) {
if (okMove) {
clipMaze();
x += xMove;
y += yMove;
// this.setTitle("in run");
//calculate the sleep interval
time += delay;
sleepInterval = Math.max(delay, time - System.currentTimeMillis());
//this.setTitle(sleepInterval + " ");
try {
move.sleep(sleepInterval * 2);
} catch (InterruptedException ex) {
//Logger.getLogger(keyPressedClassUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
//chase pacman
if(x < gx)
{
gxm = -1;//move ghost to left
}
else if(x > gx)
{
gxm = 1;
}
else if(x == gx)
{
gxm = 0;
}
if(y < gy)
{
gym = -1;//move ghost to left
}
else if(y > gy)
{
gym = 1;
}
else if(y == gy)
{
gym = 0;
}
//move the ghost
//clipghost here
gx += gxm;
gy += gym;
try {
move.sleep(sleepInterval * 2);
} catch (InterruptedException ex) {
//Logger.getLogger(keyPressedClassUI.class.getName()).log(Level.SEVERE, null, ex);
}
update(draw);
}//end of while
}
}