Hello. I am having trouble with my menu and buttons (which are just pictures that change when you scroll over them). One of my buttons works perfectly, but when I coded the rest in the same way (at least I think so) they dont work. Sorry for the long code. The main part (where it draws the graphic, etc) I hilighted in red. Another problem I am experiencing is that when I set titlemenu to 0 (titlemenu = 0) when declaring and then click start the game doesn't run, but when I set it to 2 initially (titlemenu = 2) then the game runs fine. one thing I should clarify pis pause (when p = 1 a pause menu pops up. en is enemy). Thanks for any help. Again sorry about the long code.
// The "ArrowKeys" class.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ArrowKeys2menuprob extends Applet implements KeyListener, Runnable, MouseListener, MouseMotionListener
{
int x = 50, dx, y = 75, dy, a = 0, score, total, locx, locy, p = 0, mx, my, enx = 500, eny = 500, endx, endy, life = 10, titlemenu = 2;
double rand, location;
char m;
private Image dbImage;
private Graphics dbg;
Thread thread;
Image img, title, background, target, menubg, cont, menu, en, hheart, heart, bheart, titletxt, play, instruct, exit, character, instructions;
Graphics backg;
Font pausef, scoref, scoreplay;
// Place instance variables here
public void init ()
{
rand = Math.random ();
location = Math.random () * 700 + 10;
locx = (int) Math.round (location);
location = Math.random () * 500 + 50;
locy = (int) Math.round (location);
img = getImage (getDocumentBase (), "bug1right.gif");
if (rand > .63)
{
target = getImage (getDocumentBase (), "flblue.gif");
score = 5;
}
else if (rand > .33)
{
target = getImage (getDocumentBase (), "flred.gif");
score = 2;
}
else
{
target = getImage (getDocumentBase (), "flyellow.gif");
score = 1;
}
//background = getImage (getDocumentBase (), "hedgelarge.gif");
addKeyListener (this);
addMouseListener (this);
addMouseMotionListener (this);
pausef = new Font ("Gigi", 30, 35);
scoref = new Font ("Arial", 30, 70);
scoreplay = new Font ("Arial", 15, 15);
exit = getImage (getDocumentBase (), "exitb.gif");
character = getImage (getDocumentBase (), "charselb.gif");
play = getImage (getDocumentBase (), "playb.gif");
instruct = getImage (getDocumentBase (), "instructb.gif");
instructions = getImage (getDocumentBase (), "instructions.jpg");
titletxt = getImage (getDocumentBase (), "titletxt.gif");
title = getImage (getDocumentBase (), "lbugtitle.jpg");
hheart = getImage (getDocumentBase (), "hheart.gif");
heart = getImage (getDocumentBase (), "fheart.gif");
bheart = getImage (getDocumentBase (), "bheart.gif");
menubg = getImage (getDocumentBase (), "menubg.gif");
cont = getImage (getDocumentBase (), "contb.gif");
//target = getImage (getDocumentBase (), "cont2.gif");
menu = getImage (getDocumentBase (), "menub.gif");
//target = getImage (getDocumentBase (), "menub2.gif");
en = getImage (getDocumentBase (), "en.jpg");
/*
tile = createImage (1280, 690);
backg = tile.getGraphics ();
backg.setColor (Color.white);
*/
// Place the body of the initialization method here
} // init method
public void start ()
{
thread = new Thread (this);
thread.start ();
}
public void stop ()
{
thread.stop ();
}
public void run ()
{
if (titlemenu == 2)
{
Thread.currentThread ().setPriority (Thread.MIN_PRIORITY);
while (true)
{
// Delay for 20 milliseconds
try
{
Thread.sleep (20);
}
catch (InterruptedException e)
{
}
if (titlemenu == 2)
{
if (dx == -2)
{
// If you hit an edge, reverse the direction
img = getImage (getDocumentBase (), "bug1left.gif");
for (int i = 0 ; i < 500000 ; i++)
;
repaint ();
if (y + 25 > locy && y + 25 < locy + 50 && x < locx + 50)
{
total += score;
rand = Math.random ();
location = Math.random () * 700 + 10;
locx = (int) Math.round (location);
location = Math.random () * 500 + 50;
locy = (int) Math.round (location);
if (rand > .63)
{
target = getImage (getDocumentBase (), "flblue.gif");
score = 5;
}
else if (rand > .33)
{
target = getImage (getDocumentBase (), "flred.gif");
score = 2;
}
else
{
target = getImage (getDocumentBase (), "flyellow.gif");
score = 1;
}
for (int i = 0 ; i < 1000000 ; i++)
{
}
}
}
else if (dx == 2)
{
img = getImage (getDocumentBase (), "bug1right.gif");
for (int i = 0 ; i < 500000 ; i++)
;
repaint ();
if (y + 25 > locy && y + 25 < locy + 50 && x + 50 > locx)
{
total += score;
rand = Math.random ();
location = Math.random () * 700 + 10;
locx = (int) Math.round (location);
location = Math.random () * 500 + 50;
locy = (int) Math.round (location);
if (rand > .63)
{
target = getImage (getDocumentBase (), "flblue.gif");
score = 5;
}
else if (rand > .33)
{
target = getImage (getDocumentBase (), "flred.gif");
score = 2;
}
else
{
target = getImage (getDocumentBase (), "flyellow.gif");
score = 1;
}
for (int i = 0 ; i < 1000000 ; i++)
{
}
}
}
else if (dy == -2)
{
img = getImage (getDocumentBase (), "bug1up.gif");
for (int i = 0 ; i < 500000 ; i++)
;
repaint ();
if (x + 25 > locx && x + 25 < locx + 50 && y < locy + 50)
{
total += score;
rand = Math.random ();
location = Math.random () * 700 + 10;
locx = (int) Math.round (location);
location = Math.random () * 500 + 50;
locy = (int) Math.round (location);
if (rand > .63)
{
target = getImage (getDocumentBase (), "flblue.gif");
score = 5;
}
else if (rand > .33)
{
target = getImage (getDocumentBase (), "flred.gif");
score = 2;
}
else
{
target = getImage (getDocumentBase (), "flyellow.gif");
score = 1;
}
for (int i = 0 ; i < 1000000 ; i++)
{
}
}
}
else if (dy == 2)
{
img = getImage (getDocumentBase (), "bug1down.gif");
for (int i = 0 ; i < 500000 ; i++)
;
repaint ();
if (x + 25 > locx && x + 25 < locx + 50 && y + 50 > locy)
{
total += score;
rand = Math.random ();
location = Math.random () * 700 + 10;
locx = (int) Math.round (location);
location = Math.random () * 500 + 50;
locy = (int) Math.round (location);
if (rand > .63)
{
target = getImage (getDocumentBase (), "flblue.gif");
score = 5;
}
else if (rand > .33)
{
target = getImage (getDocumentBase (), "flred.gif");
score = 2;
}
else
{
target = getImage (getDocumentBase (), "flyellow.gif");
score = 1;
}
for (int i = 0 ; i < 1000000 ; i++)
{
}
}
}
if (x + dx < 4)
{
dx = 0;
x = 4;
}
else if (x + dx > getSize ().width - 54)
{
dx = 0;
x = getSize ().width - 54;
}
if ((y + dy < 0 + 50))
{
dy = 0;
y = 50;
}
else if (y + dy > getSize ().height - 54)
{
dy = 0;
y = getSize ().height - 54;
}
//calculate the new position
x += dx;
y += dy;
if (enx > x)
{
if (p != 1 && dx != 0 || dy != 0)
{
endx = -1;
}
else
{
endx = 0;
endy = 0;
}
}
else if (enx < x)
{
if (p != 1 && dx != 0 || dy != 0)
{
endx = 1;
}
else
{
endx = 0;
endy = 0;
}
}
if (eny < y)
{
if (p != 1 && dx != 0 || dy != 0)
{
endy = 1;
}
else
{
endx = 0;
endy = 0;
}
}
else if (eny > y)
{
if (p != 1 && dx != 0 || dy != 0)
{
endy = -1;
}
else
{
endx = 0;
endy = 0;
}
}
enx += endx;
eny += endy;
if (endx == 1)
{
if (enx + 50 >= x && eny < y + 50 && eny + 50 > y)
{
life -= 1;
enx = 20;
eny = 75;
}
}
else if (endx == -1)
{
if (enx <= x + 50 && eny < y + 50 && eny + 50 > y)
{
life -= 1;
enx = 20;
eny = 75;
}
}
else if (endy == 1)
{
if (eny + 50 > y && enx < x + 50 && enx + 50 > x)
{
life -= 1;
enx = 20;
eny = 75;
}
}
else if (endy == -1)
{
if (eny > y + 50 && enx < x + 50 && enx + 50 > x)
{
life -= 1;
enx = 20;
eny = 75;
}
}
// reset thread priority
Thread.currentThread ().setPriority (Thread.MAX_PRIORITY);
}
}
}
} // while (true)
public void paint (Graphics g)
{
if (titlemenu == 0)
{
g.drawImage (title, 0, 0, 900, 700, this);
g.drawImage (titletxt, -10, -20, 400, 350, this);
g.drawImage (play, 50, 300, 100, 50, this);
g.drawImage (instruct, 50, 500, 100, 50, this);
g.drawImage (character, 50, 560, 100, 50, this);
// g.drawImage (exit, 50, 620, 100, 50, this);
if (mx > 50 && mx < 150 && my < 350 && my > 300)
{
play = getImage (getDocumentBase (), "playb2.gif");
repaint ();
}
else
{
play = getImage (getDocumentBase (), "playb.gif");
}
if (mx > 50 && mx < 150 && my > 500 && my < 550)
{
instruct = getImage (getDocumentBase (), "instructb2.gif");
}
else
{
instruct = getImage (getDocumentBase (), "instructb.gif");
}
if (mx > 50 && mx < 150 && my < 610 && my > 560)
{
character = getImage (getDocumentBase (), "charselb2.gif");
repaint ();
}
else
{
character = getImage (getDocumentBase (), "charselb.gif");
}
}
if (titlemenu == 1)
{
g.drawImage (instructions, 0, 0, 900, 700, this);
g.drawImage (menu, 700, 550, this);
if (mx > 700 && mx < 800 && my < 550 && my > 600)
{
menu = getImage (getDocumentBase (), "menub2.gif");
repaint ();
}
else {
menu = getImage (getDocumentBase (), "menub.gif");
}
}
if (titlemenu == 2)
{
//backg.drawImage(background, 0, 0, this);
g.setColor (Color.green);
g.fillRect (0, 0, 4, 700);
g.fillRect (896, 0, 4, 700);
g.fillRect (0, 696, 900, 4);
g.fillRect (0, 0, 900, 50);
g.setColor (Color.black);
g.setFont (scoreplay);
g.drawString ("Score: " + total, 700, 30);
g.drawImage (target, locx, locy, 50, 50, this);
g.drawImage (img, x, y, 50, 50, this);
g.drawImage (en, enx, eny, 50, 50, this);
if (p == 1)
{
g.drawImage (menubg, 100, 50, 500, 645, this);
g.drawImage (menu, 240, 530, this);
g.drawImage (cont, 360, 530, this);
g.setFont (pausef);
g.setColor (Color.black);
g.drawString (" Your Score: ", 260, 200);
g.setColor (Color.red);
g.setFont (scoref);
g.drawString ("" + total, 280, 400);
if (mx > 360 && mx < 460 && my > 530 && my < 600)
{
cont = getImage (getDocumentBase (), "contb2.gif");
g.drawImage (cont, 360, 530, this);
repaint ();
}
else
{
cont = getImage (getDocumentBase (), "contb.gif");
g.drawImage (cont, 360, 530, this);
}
if (mx > 240 && mx < 340 && my < 620 && mx > 550)
{
menu = getImage (getDocumentBase (), "menub2.gif");
g.drawImage (menu, 240, 530, this);
repaint ();
}
else
{
menu = getImage (getDocumentBase (), "menub.gif");
g.drawImage (menu, 240, 530, this);
}
if (life == 0)
{
}
}
if (total >= 100)
{
}
else if (total >= 75)
{
}
else if (total >= 50)
{
}
if (life == 10)
{
g.drawImage (heart, 70, 10, 20, 20, this);
g.drawImage (heart, 95, 10, 20, 20, this);
g.drawImage (heart, 120, 10, 20, 20, this);
g.drawImage (heart, 145, 10, 20, 20, this);
g.drawImage (heart, 170, 10, 20, 20, this);
}
else if (life == 9)
{
g.drawImage (heart, 70, 10, 20, 20, this);
g.drawImage (heart, 95, 10, 20, 20, this);
g.drawImage (heart, 120, 10, 20, 20, this);
g.drawImage (heart, 145, 10, 20, 20, this);
g.drawImage (hheart, 170, 10, 20, 20, this);
}
else if (life == 8)
{
g.drawImage (heart, 70, 10, 20, 20, this);
g.drawImage (heart, 95, 10, 20, 20, this);
g.drawImage (heart, 120, 10, 20, 20, this);
g.drawImage (heart, 145, 10, 20, 20, this);
g.drawImage (bheart, 170, 10, 20, 20, this);
}
else if (life == 7)
{
g.drawImage (heart, 70, 10, 20, 20, this);
g.drawImage (heart, 95, 10, 20, 20, this);
g.drawImage (heart, 120, 10, 20, 20, this);
g.drawImage (hheart, 145, 10, 20, 20, this);
g.drawImage (bheart, 170, 10, 20, 20, this);
}
else if (life == 6)
{
g.drawImage (heart, 70, 10, 20, 20, this);
g.drawImage (heart, 95, 10, 20, 20, this);
g.drawImage (heart, 120, 10, 20, 20, this);
g.drawImage (bheart, 145, 10, 20, 20, this);
g.drawImage (bheart, 170, 10, 20, 20, this);
}
else if (life == 5)
{
g.drawImage (heart, 70, 10, 20, 20, this);
g.drawImage (heart, 95, 10, 20, 20, this);
g.drawImage (hheart, 120, 10, 20, 20, this);
g.drawImage (bheart, 145, 10, 20, 20, this);
g.drawImage (bheart, 170, 10, 20, 20, this);
}
else if (life == 4)
{
g.drawImage (heart, 70, 10, 20, 20, this);
g.drawImage (heart, 95, 10, 20, 20, this);
g.drawImage (bheart, 120, 10, 20, 20, this);
g.drawImage (bheart, 145, 10, 20, 20, this);
g.drawImage (bheart, 170, 10, 20, 20, this);
}
else if (life == 3)
{
g.drawImage (heart, 70, 10, 20, 20, this);
g.drawImage (hheart, 95, 10, 20, 20, this);
g.drawImage (bheart, 120, 10, 20, 20, this);
g.drawImage (bheart, 145, 10, 20, 20, this);
g.drawImage (bheart, 170, 10, 20, 20, this);
}
else if (life == 2)
{
g.drawImage (heart, 70, 10, 20, 20, this);
g.drawImage (bheart, 95, 10, 20, 20, this);
g.drawImage (bheart, 120, 10, 20, 20, this);
g.drawImage (bheart, 145, 10, 20, 20, this);
g.drawImage (bheart, 170, 10, 20, 20, this);
}
else if (life == 1)
{
g.drawImage (hheart, 70, 10, 20, 20, this);
g.drawImage (bheart, 95, 10, 20, 20, this);
g.drawImage (bheart, 120, 10, 20, 20, this);
g.drawImage (bheart, 145, 10, 20, 20, this);
g.drawImage (bheart, 170, 10, 20, 20, this);
}
else if (life == 0)
{
g.drawImage (bheart, 70, 10, 20, 20, this);
g.drawImage (bheart, 95, 10, 20, 20, this);
g.drawImage (bheart, 120, 10, 20, 20, this);
g.drawImage (bheart, 145, 10, 20, 20, this);
g.drawImage (bheart, 170, 10, 20, 20, this);
}
// backg.drawImage( background, 0, 0, this );
// Place the body of the drawing method here
}
} // paint method
public void keyPressed (KeyEvent e)
{
if (titlemenu == 2)
{
int key = e.getKeyCode ();
if (key == KeyEvent.VK_RIGHT)
{
//backg.drawImage (background, 0, 0, 1280, 960, this);
if (p != 1)
{
dx = 2;
dy = 0;
x = x + dx;
}
}
else if (key == KeyEvent.VK_LEFT)
{
//backg.drawImage (img, 0, 0, 1280, 960, this);
if (p != 1)
{
dx = -2;
dy = 0;
x = x + dx;
}
}
else if (key == KeyEvent.VK_UP)
{
//backg.drawImage (img, 0, 0, 1280, 960, this);
if (p != 1)
{
dy = -2;
dx = 0;
y = y + dy;
}
}
else if (key == KeyEvent.VK_DOWN)
{
//backg.drawImage (img, 0, 0, 1280, 960, this);
if (p != 1)
{
dy = 2;
dx = 0;
y = y + dy;
}
}
else if (key == KeyEvent.VK_SPACE)
{
dx = 0;
dy = 0;
endx = 0;
endy = 0;
p = 1;
repaint ();
}
}
}
public void keyReleased (KeyEvent e)
{
}
public void keyTyped (KeyEvent e)
{
}
public void update (Graphics g)
{
// initialize doublebuffers
if (dbImage == null)
{
dbImage = createImage (this.getSize ().width, this.getSize ().height);
dbg = dbImage.getGraphics ();
}
// save background
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize ().width, this.getSize ().height);
// draw foreground on background
dbg.setColor (getForeground ());
paint (dbg);
// Now indicate ready drawn picture Offscreen on the right screen
g.drawImage (dbImage, 0, 0, this);
}
public void mouseEntered (MouseEvent e)
{
}
public void mouseExited (MouseEvent e)
{
}
public void mousePressed (MouseEvent e)
{
if (titlemenu == 2 && p == 1)
{
if (mx > 360 && mx < 460 && my > 530 && my < 600)
{
p = 0;
repaint ();
}
if (mx > 240 && mx < 340 && my > 530 && my < 600)
{
titlemenu = 0;
repaint ();
}
}
if (titlemenu == 1)
{
if (mx > 700 && mx < 800 && my > 550 && my < 600)
{
titlemenu = 0;
repaint ();
}
}
if (titlemenu == 0)
{
if (mx > 50 && mx < 150 && my < 350 && my > 300)
{
//play = getImage (getDocumentBase (), "playb2.gif");
titlemenu = 2;
p = 0;
repaint ();
}
else if (mx > 50 && mx < 150 && my > 500 && my < 550)
{
titlemenu = 1;
repaint ();
}
else if (mx > 50 && mx < 150 && my < 610 && my > 560)
{
//character = getImage (getDocumentBase (), "charselb2.gif");
}
else if (mx < 150 && mx > 50 && my < 670 && my > 620)
{
//cont = getImage (getDocumentBase (), "exitb2.gif");
}
else
{
//exit = getImage (getDocumentBase (), "exitb.gif");
//character = getImage (getDocumentBase (), "charselb.gif");
//play = getImage (getDocumentBase (), "playb.gif");
//instruct = getImage (getDocumentBase (), "instructb.gif");
}
}
}
public void mouseReleased (MouseEvent e)
{
}
public void mouseDragged (MouseEvent e)
{
}
public void mouseMoved (MouseEvent e)
{
mx = e.getX ();
my = e.getY ();
//repaint ();
}
public void mouseClicked (MouseEvent e)
{
}
} // ArrowKeys class
if needed I can upload a video on my game. outlining the problems. Thanks again