Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Help with a Java Program! Please!


  • Please log in to reply

#1
Eroved-Kcire

Eroved-Kcire

    Member

  • Member
  • PipPip
  • 14 posts
Hello All,

This is my first post...I hope to find some answers to my program I am writing! A couple things first - I am 17, and have designed a comp. science class for myself to take while a senior in high school, since there was not one offered. My end of the 2nd term project is to write a program that encompasses the information I have learned about for the past two terms. I know the very basics, if that. Please help!

My program involves the simple dice throwing program, 'Dice Thrower' by Kevin Boone. The full text/program write out can be found at:

http://www.kevinboon...rower-java.html

I am hoping to add a part to this program that basically tells the program if the dice show doubles, pop up a little GUI window that says "You are a Winner! You Got Doubles!"

I was thinking as I finished chapter 7 in my text book, that either an 'if/else' or 'switch' statement might be the right place to start.

Could someone please write me a start to the program text that I need to add to 'Dice Thrower', or explain what I need to do to get started! Thank you so much, I really appreciate it.

Erick DeVore - Eroved Kcire :tazz:
  • 0

Advertisements


#2
gust0208

gust0208

    Member

  • Member
  • PipPipPip
  • 311 posts
Hello,

So I understand your goal, you want to add a pop-up window that alerts the user in the event the get doubles? If so, you need to look through the program and think when the program should check for this state. Most logically, you would want to check after a dice throw, specifically when the function throwBothDice is called. Then, what user action is going to start this event? We will need to look at the mouse event handler, so I would look to add the pop-up window code to either the throwBothDice() function itself or right after it is called in the mouse event handler. I like it being called in the mouse event handler, so in the case that you get doubles from the init() call of throwBothDice(), you won't get a pop-up.

Give a shot at writing the code for the pop-up window and we can help you with syntax and general ideas.

Cheers,
Tom

Hello All,

This is my first post...I hope to find some answers to my program I am writing! A couple things first - I am 17, and have designed a comp. science class for myself to take while a senior in high school, since there was not one offered. My end of the 2nd term project is to write a program that encompasses the information I have learned about for the past two terms. I know the very basics, if that. Please help!

My program involves the simple dice throwing program, 'Dice Thrower' by Kevin Boone. The full text/program write out can be found at:

http://www.kevinboon...rower-java.html

I am hoping to add a part to this program that basically tells the program if the dice show doubles, pop up a little GUI window that says "You are a Winner! You Got Doubles!"

I was thinking as I finished chapter 7 in my text book, that either an 'if/else' or 'switch' statement might be the right place to start.

Could someone please write me a start to the program text that I need to add to 'Dice Thrower', or explain what I need to do to get started! Thank you so much, I really appreciate it.

Erick DeVore - Eroved Kcire :tazz:


  • 0

#3
Eroved-Kcire

Eroved-Kcire

    Member

  • Topic Starter
  • Member
  • PipPip
  • 14 posts
Firstly, thanks for the advice!

Secondly, today during school I began to review the information in my text book as well as researched information online concerning JPanel and JFrame information. I thought those would be helpful in trying to get my window / UI to pop up. I also began to write code after the command telling the program to throw dice. I was not able to bring the work home with me, however it is saved at school, and I will post what I have written so far tomorrow sometime around 10:50-11:30 a.m. Please check the forum topic around that time to see if you could give me any other pointers.

Thanks a million!
  • 0

#4
Eroved-Kcire

Eroved-Kcire

    Member

  • Topic Starter
  • Member
  • PipPip
  • 14 posts
Hey all, starting from a point in the original program text, my text starts at //Erick's PROGRAM Insert. It's not much, but I'd love for someone to tell me if this is going to get me a pop up window after the dice have been thrown to tell the person that they are a winner...does the text make sense? If it does (which I highly doubt) great, but if it doesn't, could someone please give me a few pointers or look at the syntax that would help it work?

Thanks! Erick~

// `throw' the dice so that they start off with random numbers showing

throwBothDice();

// Create a DiceThrowerMouseListener object to handle mouse
// clicks. The `setDiceThrower' operation informs this
// DiceThrowerMouseListener object of the object that it
// must activate when a mouse click occurs (i.e., this applet).
// The keyword `this' simply means `this object here'

// Erick's PROGRAM Insert -

if (die1 == die2)
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!"))
}

public static void main(String[] args)
{
WinnerGui window = new WinnerGui();
window.addWindowListener(new ExitButtonListener());
window.setSize(300, 100);
window.show();
}
else
DiceThrowerMouseListener diceThrowerMouseListener =
new DiceThrowerMouseListener();
diceThrowerMouseListener.setDiceThrower(this);
addMouseListener(diceThrowerMouseListener);
}
  • 0

#5
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
here's some code that might help. these are test files that will pop up the winner gui. the gui has been separated from main(). these 2 files have structure that you might consider using.

/////////////////	 Eroved.java
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Eroved {
  
  public static void diceProgram() { 
	// other code goes here
	// if( doubles) { displayGUI(); }
	displayGUI(); // this is test code
	// other code goes here
  }
  public static void displayGUI() {
	WinnerGui window = new WinnerGui();
	window.addWindowListener(new WindowAdapter() {
	  public void windowClosing(WindowEvent e) { 
	  // the following line will shut down the entire program
	  // when the GUI is closed. 
	  System.exit(0); 
	  }
	  });
	window.setSize(300, 100);
	window.show();
  }
  public static void main(String[] args) {
	diceProgram();
  }

}
/////////////////	end  Eroved.java

///////////////////  WinnerGui.java
import javax.swing.JLabel;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.*;

public 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!"));
  }
}
///////////////////  end WinnerGui.java

  • 0

#6
Eroved-Kcire

Eroved-Kcire

    Member

  • Topic Starter
  • Member
  • PipPip
  • 14 posts
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;

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();

	// Erick's Program Insert below

	public 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!"));
  }
  }

		//End Erick's Program insert


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

public void throwBothDice();
	{
	die1.throwDie();
	die2.throwDie();
	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 void throwDie()
	{
	numberShowing = (int)(Math.random() * 6 + 1);
	}

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 was what I have dwindled it down to, using my knowledge, as well as others' expertise...however I am listening to what you say, and get these messages when I try to compile in the CMD:

line 63 - illegal start of expression (carrot at p in public)
line 63 - ; expected (carrot at c in class)
line 63 - { expected (carrot at ; at end of line)
line 65 - illegal start of expression (carrot at p in public)
line 83 - <identifier> expected (carrot as: (this) ) -- I don't even know what that means
^
line 84 - invalid method declaration; return type required (carrot at a in add)
line 84 - <identifier> expected (carrot like this: [see below])

addMouseListener(diceThrowerMouseListener);
^

line 84 - ) expected (carrot is one place over from previous spot [see above]
line 93 - illegal start of expression (carrot at p in public)

I know this seems like a lot of errors, but believe it or not, I actually had it up in the high teens and was able to fix some of them myself from going by previous examples, as well as looking things up in my text book. If anyone has any suggestions concerning the syntax errors of the code, please please let me know and I will work my hardest to fix them...thank you all so much for helping.

Erick
  • 0

#7
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
1. the following are all the same syntax error.

public void throwBothDice();

public class WinnerGui extends JFrame;

public WinnerGui();

remove the semi colon from each.

2. try moving the GUI code to the bottom of the file and remove 'public'

3. you may need to remove a curly brace(}) from the end of the file

post any errors

once we get it to compile, we'll need to make changes in the code in post#5 since the gui is launched from an applet.

Edited by bdlt, 26 January 2006 - 11:19 PM.

  • 0

#8
Eroved-Kcire

Eroved-Kcire

    Member

  • Topic Starter
  • Member
  • PipPip
  • 14 posts
Thank you so much for helping me get my code to a good status without a lot of syntax errors!!! I am so very greatful...I actually got the errors (all by myself I might add) from high teens down to those 8 or 9, and with the help of you all, I actually got it to one error which I am really happy about. However, Doing everything that you have told me to do, the last error states:

class WinnerGui is public, should be decalred in a file named WinnerGui.java

I don't really undertstand what this error is talking about, and here's the weird thing. If I rename the .java file to WinnerGui.java instead of DiceThrower.java, the error states:

class DiceThrower is public, should be decalred in a file named DiceThrower.java

Can someone please help me out with this error?! Thanks so much! This is running along perfectly!

Erick
  • 0

#9
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
this probably wasn't clear from post #7
2. try moving the GUI code to the bottom of the file and remove 'public'

instead of
public class WinnerGui
try
class WinnerGui

it's sounds like you are getting the hang of reading compiler messages. that's great! the compiler does most of the work and it's up to us to figure out what it tells us.

here's an explanation of the error:
see restrictions here
http://www.cs.brown....ide/public.html
there can only be one public class per file

once this compiles there is a tweak we need to do to the gui code.

Edited by bdlt, 28 January 2006 - 03:22 PM.

  • 0

#10
Eroved-Kcire

Eroved-Kcire

    Member

  • Topic Starter
  • Member
  • PipPip
  • 14 posts
Thanks for that hint...I had no idea that there could only be one public class in a code. Thanks! When I did change the Public class WinnerGui to just class WinnerGui, the code did indeed compile (alas, a day I have been longing for - haha!)

However, as usual, there is another error from the command prompt, and this time it reads:

Exception in thread "main" java.lang.NoClassDefFoundError: DiceThrower

I also was just making sure, and checking with all of the brilliant minds out there, when this finally does run, the code will compile to pop up the Dice Throwing game. The user will play, and when the user gets doubles, a window will pop up stating that "They got doubles, and they win!" -- Am I correct?

Any ideas on what the computer wants me to do next?
  • 0

Advertisements


#11
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
there should not be a main() in an applet. try removing main() and its code.

if that doesn't fix it, then post/attach the code to your next post.

FYI - it does run once we get rid of main() and tweak the gui code.

Edited by bdlt, 29 January 2006 - 05:17 PM.

  • 0

#12
Eroved-Kcire

Eroved-Kcire

    Member

  • Topic Starter
  • Member
  • PipPip
  • 14 posts
I'm sorry, but I don't understand what you are talking about with main or main() which ever you mean...I can't find it anywhere in the code. I also hit F3 on the keyboard to look for main, and it couldn't find it anywhere...I checked the DiceThrower.java code, and there wasn't anything to be found. Here it is (and if you want to show me where something to do with main is, that'd be great!):

DiceThrower.java

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;

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()
	{
	die1.throwDie();
	die2.throwDie();
	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 void throwDie()
	{
	numberShowing = (int)(Math.random() * 6 + 1);
	}

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;
	}

}

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

Edited by Eroved-Kcire, 29 January 2006 - 07:44 PM.

  • 0

#13
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
you are correct - there is no main() in this code. the 'main()' error is what occurs when trying to run an applet from the command line using a non-applet command. below is the code to run the applet from an html file. save this file as 'dice.htm' and save it in the same folder as DiceThrower.java. then double click on it. the dice program should run.

post your results, then we will add code to call up the gui on doubles.

<html>
<head>
</head>
<body>
<p>Applet call from html</p>
<applet code = "DiceThrower.class" height = 500 width = 700>
Java not running
</body>
</html>

  • 0

#14
Eroved-Kcire

Eroved-Kcire

    Member

  • Topic Starter
  • Member
  • PipPip
  • 14 posts
This is wonderful...
The dice program is working perfectly!

The only step left now is to pop up the gui window that tells the user that when the dice program get doubles on a roll, the user is a winner!

Where do I need to start if I already have some of the gui code at the bottom of the DiceThrower.java code???

Thanks a Million! :tazz:

Erick
  • 0

#15
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
great!

let's add some code:
add WinnerGui window = new WinnerGui(); as below
Die die1;
Die die2;

WinnerGui window = new WinnerGui();

add these methods to class DiceThrower:


  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();
  }

add code, compile, run - should run the same as before
next - add code to compare each die

Edited by bdlt, 29 January 2006 - 09:46 PM.

  • 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