Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Help with a Java Program! Please!


  • Please log in to reply

#16
Eroved-Kcire

Eroved-Kcire

    Member

  • Topic Starter
  • Member
  • PipPip
  • 14 posts
I've added the code you suggested to the orignal .java file...I don't know if it is in the right place, however:

public class DiceThrower extends Applet
{

Die die1;
Die die2;

WinnerGui window = new WinnerGui();

public void displayGUI() {
	window.addWindowListener(new WindowAdapter() {
	  public void windowClosing(WindowEvent e) {
		window.dispose();
	  }
	  });
	window.setSize(300, 100);
	window.show();
  }
  public void closeGUI() {
	window.dispose();
  }

If that is correct, then we can move on to comparing the two dice, I would guess, by an if/else statement or something. However if those code placements are wrong, let me know so that I may change them!

Erick
  • 0

Advertisements


#17
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 875 posts
that should work.

let's add the gui code.

1. change throwDie() to return an int

throwBothDice() mods:
2. declare 2 new ints
3. set each int = to the value returned from throwDie(), something like
n1 = die1.throwDie();
n2 = die2.throwDie();
4. add compare code ... if( n1 == n2 ) ...

this part is an exercise for you.
try adding the changes and post back if ok or if you have questions. we're almost there.
  • 0

#18
Eroved-Kcire

Eroved-Kcire

    Member

  • Topic Starter
  • Member
  • PipPip
  • 14 posts
I've tried to use some of your hints to help write this code...see if this is anywhere near right. I'm guessing not...:tazz: haha.

Here's the chunk:

throwBothDice();

	int die1 = n1;
	int die2 = n2;

	n1 = die1.throwBothDice();
	n2 = die2.throwBothDice();

	if (n1 = n2)
	display winnergui

	DiceThrowerMouseListener diceThrowerMouseListener =
		new DiceThrowerMouseListener();
	diceThrowerMouseListener.setDiceThrower(this);
	addMouseListener(diceThrowerMouseListener);
	}

Hope that's somewhat decent.
  • 0

#19
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 875 posts
let's look at the code:

throwBothDice(); // is a command calling method named throwBothDice

// the following is the declaration of a method named throwBothDice
public void throwBothDice() {
// code between curly braces
}

int die1 = n1; // n1 has not been declared
// and the die1 we want is declared as a Die object ... Die die1;
// we want 2 new ints
int n1 = 0; // initialized to 0
int n2 = 0; // initialized to 0

// and we want to assign a die value to each
n1 = die1.throwDie();
n2 = die2.throwDie();

// before we do this throwDie() must be modified to return the die value
// from 'public void throwDie()' to 'public int throwDie()'
// and add a return line after 'numberShowing = (int)(Math.random() * 6 + 1);'
return numberShowing;

// now we can compare the 2 value and call the gui if they match or remove the gui
// when they do not match
if( n1 == n2 ) {
displayGUI(); // already defined in class DiceThrower
}
else {
closeGUI(); // already defined in class DiceThrower
}


here's how the methods might look when done:

public int throwDie()
	{
	numberShowing = (int)(Math.random() * 6 + 1);
	return numberShowing;
	}


public void throwBothDice()
	{
	int n1 = 0;
	int n2 = 0;
	n1 = die1.throwDie();
	n2 = die2.throwDie();

	if( n1 == n2 ) {
	  displayGUI();
	}
	else {
	  closeGUI();
	}
	repaint();
	}

  • 0

#20
Eroved-Kcire

Eroved-Kcire

    Member

  • Topic Starter
  • Member
  • PipPip
  • 14 posts
Well, I added/changed the code that needed to be added/changed. Here's the most up to date version of the code...

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JLabel;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import java.awt.Color;

public class DiceThrower extends Applet
{

Die die1;
Die die2;

WinnerGui window = new WinnerGui();

public void displayGUI() {
	window.addWindowListener(new WindowAdapter() {
	  public void windowClosing(WindowEvent e) {
		window.dispose();
	  }
	  });
	window.setSize(300, 100);
	window.show();
  }
  public void closeGUI() {
	window.dispose();
  }

public void paint (Graphics g)
	{
	die1.draw(g);
	die2.draw(g);
	g.drawString ("Click mouse anywhere", 55, 140);
	g.drawString ("to throw dice again", 65, 160);
	}

public void init()
	{
	die1 = new Die();
	die2 = new Die();

	die1.setTopLeftPosition(20, 20);
	die2.setTopLeftPosition(150, 20);

	throwBothDice();

	DiceThrowerMouseListener diceThrowerMouseListener =
		new DiceThrowerMouseListener();
	diceThrowerMouseListener.setDiceThrower(this);
	addMouseListener(diceThrowerMouseListener);
	}

public void throwBothDice()
	{

	int n1 = 0;
	int n2 = 0;
	n1 = die1.throwDie();
	n2 = die2.throwDie();
	
	if (n1 == n2) {
	 displayGUI();
				  }
	else
	 closeGUI();  {
				  }
	
	repaint();
	}
}

class Die
{

int topLeftX;
int topLeftY;

int numberShowing = 6;

final int spotPositionsX[] = {0, 60,  0, 30, 60,  0,  60};
final int spotPositionsY[] = {0,  0, 30, 30, 30,  60, 60};

public void setTopLeftPosition(final int _topLeftX, final int _topLeftY)
	{
	topLeftX = _topLeftX;
	topLeftY = _topLeftY;
	}

public int throwDie()
	{
	numberShowing = (int)(Math.random() * 6 + 1);
	return numberShowing;
	}

public void draw(Graphics g)
	{

	switch(numberShowing)
		{
		case 1:
			drawSpot(g, 3);
			break;
		case 2:
			drawSpot(g, 0);
			drawSpot(g, 6);
			break;
		case 3:
			drawSpot(g, 0);
			drawSpot(g, 3);
			drawSpot(g, 6);
			break;
		case 4:
			drawSpot(g, 0);
			drawSpot(g, 1);
			drawSpot(g, 5);
			drawSpot(g, 6);
			break;
		case 5:
			drawSpot(g, 0);
			drawSpot(g, 1);
			drawSpot(g, 3);
			drawSpot(g, 5);
			drawSpot(g, 6);
			break;
		case 6:
			drawSpot(g, 0);
			drawSpot(g, 1);
			drawSpot(g, 2);
			drawSpot(g, 4);
			drawSpot(g, 5);
			drawSpot(g, 6);
			break;
		}
	}

void drawSpot(Graphics g, final int spotNumber)
	{
	g.fillOval(topLeftX + spotPositionsX[spotNumber],
		topLeftY + spotPositionsY[spotNumber], 20, 20);
	}

}

class DiceThrowerMouseListener extends MouseAdapter
{
DiceThrower diceThrower;

public void mouseClicked (MouseEvent e)
	{
	diceThrower.throwBothDice();
	}

public void setDiceThrower(DiceThrower _diceThrower)
	{
	diceThrower = _diceThrower;
	}

}

Please let me know of any changes that need to be made next! :tazz:
  • 0

#21
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 875 posts
you are at the last step - add the gui code to class DiceThrower
class WinnerGui extends JFrame {
  public WinnerGui()
  {
	super ("You Are The Winner with GUI");
	Container c = getContentPane();
	c.setBackground(Color.lightGray);
	c.setLayout(new FlowLayout());
	c.add(new JLabel("You're a Winner! You Got Doubles!"));
  }
}

  • 0

#22
Eroved-Kcire

Eroved-Kcire

    Member

  • Topic Starter
  • Member
  • PipPip
  • 14 posts
I added the latest code to the proper place...

ultimately i just checked the personal message i received to see if i put it in the right place, and I did, so I just copied and pasted the code you had in the personal message. However, something is still not right...it isn't working! I checked to see if the code needed to be recompiled, so I compiled it in the command prompt, and tried to open the applet with the .htm file you had me make a little bit ago. The window is still not popping up however...

Here's the code that I now have in the DiceThrower.java text file:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JLabel;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import java.awt.Color;

public class DiceThrower extends Applet
{

Die die1;
Die die2;

WinnerGui window = new WinnerGui();

public void displayGUI() {
	window.addWindowListener(new WindowAdapter() {
	  public void windowClosing(WindowEvent e) {
		window.dispose();
	  }
	  });
	window.setSize(300, 100);
	window.show();
  }
  public void closeGUI() {
	window.dispose();
  }

public void paint (Graphics g)
	{
	die1.draw(g);
	die2.draw(g);
	g.drawString ("Click mouse anywhere", 55, 140);
	g.drawString ("to throw dice again", 65, 160);
	}

public void init()
	{
	die1 = new Die();
	die2 = new Die();

	die1.setTopLeftPosition(20, 20);
	die2.setTopLeftPosition(150, 20);

	throwBothDice();

	DiceThrowerMouseListener diceThrowerMouseListener =
		new DiceThrowerMouseListener();
	diceThrowerMouseListener.setDiceThrower(this);
	addMouseListener(diceThrowerMouseListener);
	}

public void throwBothDice()
	{

	int n1 = 0;
	int n2 = 0;
	n1 = die1.throwDie();
	n2 = die2.throwDie();
	
	if (n1 == n2) {
	 displayGUI();
				  }
	else {
	 closeGUI();  
				  }
	
	repaint();
	}

class WinnerGui extends JFrame {
  public WinnerGui()
  {
	super ("You Are The Winner with GUI");
	Container c = getContentPane();
	c.setBackground(Color.lightGray);
	c.setLayout(new FlowLayout());
	c.add(new JLabel("You're a Winner! You Got Doubles!"));
  }
}

}

class Die
{

int topLeftX;
int topLeftY;

int numberShowing = 6;

final int spotPositionsX[] = {0, 60,  0, 30, 60,  0,  60};
final int spotPositionsY[] = {0,  0, 30, 30, 30,  60, 60};

public void setTopLeftPosition(final int _topLeftX, final int _topLeftY)
	{
	topLeftX = _topLeftX;
	topLeftY = _topLeftY;
	}

public int throwDie()
	{
	numberShowing = (int)(Math.random() * 6 + 1);
	return numberShowing;
	}

public void draw(Graphics g)
	{

	switch(numberShowing)
		{
		case 1:
			drawSpot(g, 3);
			break;
		case 2:
			drawSpot(g, 0);
			drawSpot(g, 6);
			break;
		case 3:
			drawSpot(g, 0);
			drawSpot(g, 3);
			drawSpot(g, 6);
			break;
		case 4:
			drawSpot(g, 0);
			drawSpot(g, 1);
			drawSpot(g, 5);
			drawSpot(g, 6);
			break;
		case 5:
			drawSpot(g, 0);
			drawSpot(g, 1);
			drawSpot(g, 3);
			drawSpot(g, 5);
			drawSpot(g, 6);
			break;
		case 6:
			drawSpot(g, 0);
			drawSpot(g, 1);
			drawSpot(g, 2);
			drawSpot(g, 4);
			drawSpot(g, 5);
			drawSpot(g, 6);
			break;
		}
	}

void drawSpot(Graphics g, final int spotNumber)
	{
	g.fillOval(topLeftX + spotPositionsX[spotNumber],
		topLeftY + spotPositionsY[spotNumber], 20, 20);
	}

}

class DiceThrowerMouseListener extends MouseAdapter
{
DiceThrower diceThrower;

public void mouseClicked (MouseEvent e)
	{
	diceThrower.throwBothDice();
	}

public void setDiceThrower(DiceThrower _diceThrower)
	{
	diceThrower = _diceThrower;
	}

}

That should be what you have been asking for. Is there anything else I need to do to get this to work?

And also, it's probably not going to be a problem, however I did finally receive my deadline for my presentation when I need to share to the committee what I have been doing this trimester...it's next wednesday, February the 8th. Do you think we can get the program to work by then?

Thanks John,

Erick

Edited by Eroved-Kcire, 03 February 2006 - 10:13 AM.

  • 0

#23
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 875 posts
hi Erick,

when you are running the html file, closed all internet windows.

if you recompile the java code, closed the 'dice' window, then restart the html file. the applet code does not refresh like html code does.

while you are retesting, I'll look at your code.

if it still does not run, then post the html code.

with any luck, we should finish this morning(if you are available)

bdlt(aka john)


edit/update - the code you posted this morning runs fine here.

Edited by bdlt, 03 February 2006 - 10:48 AM.

  • 0

#24
Eroved-Kcire

Eroved-Kcire

    Member

  • Topic Starter
  • Member
  • PipPip
  • 14 posts
John / bdlt

Thank you so much!!!

You have no idea how much your help, expertise, and kindness has helped me through this process. I have officially gotten the program to work, and will meet the deadline next wednesday, Feb the 8th. I closed all of the internet windows like you said, and the program worked beautifully! The window popped up when the doubles happened, and even went away when the user did not have doubles.

Once again, Thank you so much, it means alot :tazz:

Erick DeVore, Dayton, OH.
  • 0

#25
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 875 posts
you're welcome Erick. good luck with the presentation.
  • 0

Advertisements


#26
Eroved-Kcire

Eroved-Kcire

    Member

  • Topic Starter
  • Member
  • PipPip
  • 14 posts
I'll be sure to let you know how it goes, and what grade I get. I technically should share the grade with you since you (and some others - kindof) helped me with this project.

You can use the grade for moral support, and feel good that you helped some high school senior in a completley different state get through an Independant study!

Erick :tazz:
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP