Java Hang Man game assistance

F

ford.aristotle

Hello,

I am working on the age old Hang Man game and need assistance turning a
GIF file into seperate elements and displaying those elements as the
incorrect letters are selected. Here is what I have so far. I will
create the animated GIF and plug it into the program using the Paint
method. How should I write the method so that as the incorrect element
is selected, the image appears?:



import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.applet.*;


public class Gamesetup extends Applet implements MouseListener,
MouseMotionListener {
private int xpos, ypos =-10;
private boolean clickedsomething=false;
private int lastitemclicked=-1;
// Globals needed;
int min_x=0; int max_x=500; int min_y=200; int max_y=299;
int rows=2; int columns=13;

public int get_row(int y) {
// -1 will be returned if clicked out of range
int return_row=0;
if(y<min_y) {return_row=-1;}
else if (y>max_y) {return_row=-1;}
else {return_row=(y-min_y)/((max_y-min_y)/rows);}
if(return_row==rows) {return_row--;}
return return_row; }

public int get_column(int x) {
// -1 will be returned if clicked out of range
int return_column=0;
if(x<min_x) {return_column=-1;}
else if (x>max_x) {return_column=-1;}
else {return_column=(x-min_x)/((max_x-min_x)/columns);}
if(return_column==columns) {return_column--;}
return return_column; }

public int get_row_y_start(int row){
return ((max_y-min_y)/rows)*row+min_y;}

public int get_column_x_start(int column){
return ((max_x-min_x)/columns)*column+min_x;}

public int convert_column_row_to_list(int x, int y) {
return y*columns+x; }

public int list_to_column(int l) {return l%columns;}
public int list_to_row(int l) {return l/columns;}
public int list_to_column_screen(int l) {return
min_x+(l%columns)*((max_x-min_x)/columns);}
public int list_to_row_screen(int l) {return
(l/columns)*((max_y-min_y)/rows)+min_y;}

public int convert_column_row_to_list_raw(int x, int y) {
return get_row(y)*columns+get_column(x); }


public void init() {
addMouseListener(this);
addMouseMotionListener(this);
}

public void paint(Graphics g) {
g.drawString("Last Item Clicked="+lastitemclicked, 50, 60 );
char firstletter='A';
for(int i=0;i<26;i++)
g.drawString(""+firstletter++, list_to_column_screen(i) +
((max_x-min_x)/columns)/2, list_to_row_screen(i)+((max_y-min_y)/rows)/2
);


}
public boolean clickedsomthingonpage(int x,int y){
boolean gotsomething=false;

if((min_x<=x)&&(max_x>=x)&&(min_y<=y)&&(max_y>=y)) {
lastitemclicked=convert_column_row_to_list_raw(x,y);
gotsomething=true;}

if((400<=x)&&(500>=x)&&(0<=y)&&(50>=y)) {
lastitemclicked=26;
gotsomething=true;}

return gotsomething;
}

public void mouseClicked( MouseEvent e)
{clickedsomething=clickedsomthingonpage(e.getX(),e.getY());
if(clickedsomething)repaint();}
public void mousePressed( MouseEvent e) {}
public void mouseReleased( MouseEvent e) {}
public void mouseEntered( MouseEvent e) {}
public void mouseExited( MouseEvent e) {}
public void mouseDragged( MouseEvent e) {}
public void mouseMoved( MouseEvent e) {}


}
 
R

Rhino

Hello,

I am working on the age old Hang Man game and need assistance turning a
GIF file into seperate elements and displaying those elements as the
incorrect letters are selected. Here is what I have so far. I will
create the animated GIF and plug it into the program using the Paint
method. How should I write the method so that as the incorrect element
is selected, the image appears?:
I'm not sure I understand what you are trying to do. Do you mean that your
animated GIF will have each of the elements of the hanging (scaffold alone,
then scaffold plus head, then scaffold and head plus neck, etc.) in a
separate frame of the same animated GIF and you just want to show the
different frames one at a time?

If that's what you mean, one approach would be use the Image I/O classes to
read in the various frames from animated GIF, put them in an array of
buffered images, then add the array to a MediaTracker. Then, you use the
MediaTracker to display each frame whenever you like. I've found that
approach quite satisfactory for my purposes.

Here are some relevant code fragments to help you get the job done.

/* Get the URL for the image file. */
URL imageFileUrl = null;
try {
imageFileUrl = new URL(imageFileName);
} catch (MalformedURLException excp) {
//error handling
}

/* Obtain an InputStream from the URL; use it to create an
ImageInputStream. */
ImageInputStream imageInputStream = null;
try {
imageInputStream =
ImageIO.createImageInputStream(imageFileUrl.openStream());
} catch (IOException io_excp) {
//error handling
}

/* Determine what format this file is. Use the first reader for this
format. */
Iterator readers = ImageIO.getImageReaders(imageInputStream);
ImageReader reader = (ImageReader)readers.next();
reader.setInput(imageInputStream, false);
String formatName = null;
try {
formatName = reader.getFormatName();
} catch (IOException io_excp) {
//error handling
}

/*
* Get the image count and minimum index.
*/
int numImages = 0;
int minIndex = 0;
try {
numImages = reader.getNumImages(true);
minIndex = reader.getMinIndex();
} catch (IOException io_excp) {
//error handling
}

/*
* Fetch and store each image of the file in an array of buffered
images. Load each
* image into the MediaTracker.
*/
this.imageFrames = new BufferedImage[numImages];
MediaTracker mediaTracker = new MediaTracker(this);
for (int ix=minIndex; ix<numImages+minIndex; ix++) {
try {
BufferedImage bufferedImage = reader.read(ix);
mediaTracker.addImage(this.imageFrames[ix], ix);
} catch (IOException io_excp) {
//error hanlding
}
}

/* Ensure that all images are loaded. */
try {
mediaTracker.waitForAll();
} catch (InterruptedException i_excp) {
//error handling
}

/* Check for errors and report any that are found. */
if (!mediaTracker.checkAll()) {
System.err.println("One or more images not yet loaded.");
//$NON-NLS-1$
}

if (mediaTracker.isErrorAny()) {
System.err.println("One or more images encountered an error.
URL: " + imageFileUrl); //$NON-NLS-1$
Object[] errors = mediaTracker.getErrorsAny();
for (int iy=0; iy<errors.length; iy++) {
System.err.println("Error: " + errors[iy]); //$NON-NLS-1$
Object[] media = mediaTracker.getErrorsID(iy);
for (int iz=0; iz<media.length; iz++) {
System.err.println("Media: " + media[iz]); //$NON-NLS-1$
} //end for media
} //end for errors
} //end isErrorAny

I'll leave you to figure out how to display the images once they are in the
MediaTracker. ;-) Hint: you should find plenty of code fragments on how to
do this in the archives of this newsgroup or the archive for
comp.lang.java.gui. You can use a Google Groups search to find them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top