Need help with project
Author |
Message |
Regals
|
Posted: Fri Jun 06, 2008 11:08 am Post subject: Need help with project |
|
|
Hello everyone. I'm creaing a dice game for a project in my ICS class. The premise of the game is that a player enters a bet amount and win's 2x the bet amount if the roll is a 6. However, I also want to have a GUI window open with the picture of the amount of the roll (i.e. a roll of 5 shows the image of a die rolling a 5). Here is my code so far:
Java: |
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.text.*;
import java.math.*;
public class JPanelImage extends JFrame
{
public JPanelImage ()
{
super ("Roll");
setSize (50, 100);
setDefaultCloseOperation (JFrame. EXIT_ON_CLOSE);
setVisible (true);
Container myContent = getContentPane ();
myContent. setBackground (Color. white);
FlowLayout myFlow = new FlowLayout ();
myPanel panel1 = new myPanel ();
myContent. add (panel1 );
setContentPane (myContent );
}
class myPanel extends JPanel
{
public void paintComponent (Graphics artist )
{
Toolkit myTools = Toolkit. getDefaultToolkit ();
Image pic1 = myTools. getImage ("die1.jpg");
if (pic1 != null)
{
artist. drawImage (pic1, 10, 10, this);
}
else
{
System. out. println ("Image not found!");
}
}
}
public void paintComponent2 (Graphics artist )
{
Toolkit myTools = Toolkit. getDefaultToolkit ();
Image pic2 = myTools. getImage ("die2.jpg");
if (pic2 != null)
{
artist. drawImage (pic2, 10, 10, this);
}
else
{
System. out. println ("Image not found!");
}
}
public void paintComponent3 (Graphics artist )
{
Toolkit myTools = Toolkit. getDefaultToolkit ();
Image pic3 = myTools. getImage ("die3.jpg");
if (pic3 != null)
{
artist. drawImage (pic3, 10, 10, this);
}
else
{
System. out. println ("Image not found!");
}
}
public void paintComponent4 (Graphics artist )
{
Toolkit myTools = Toolkit. getDefaultToolkit ();
Image pic4 = myTools. getImage ("die4.jpg");
if (pic4 != null)
{
artist. drawImage (pic4, 10, 10, this);
}
else
{
System. out. println ("Image not found!");
}
}
public void paintComponent5 (Graphics artist )
{
Toolkit myTools = Toolkit. getDefaultToolkit ();
Image pic5 = myTools. getImage ("die5.jpg");
if (pic5 != null)
{
artist. drawImage (pic5, 10, 10, this);
}
else
{
System. out. println ("Image not found!");
}
}
public void paintComponent6 (Graphics artist )
{
Toolkit myTools = Toolkit. getDefaultToolkit ();
Image pic6 = myTools. getImage ("die6.jpg");
if (pic6 != null)
{
artist. drawImage (pic6, 10, 10, this);
}
else
{
System. out. println ("Image not found!");
}
}
public static void main (String [] args ) throws IOException
{
BufferedReader stdin = new BufferedReader (new InputStreamReader (System. in));
String inData;
int cash = 100; // initail amount of cash
int lower = 1; // your lower integer value
int upper = 7; // the larger one of your two integers
int bet = 0;
while (cash >= 1)
{System. out. println ();
System. out. println ("You have $" + cash );
System. out. println ("Enter a bet amount.");
System. out. println ("Enter 0 to close.");
inData = stdin. readLine ();
bet = Integer. parseInt (inData );
if (bet == 0)
{
System. exit (0);
}
else if (bet > cash )
System. out. println ("Invalid amount.");
else
{
double rand = Math. random ();
int result = lower + (int) ((upper - lower ) * rand );
System. out. println ("You rolled a " + result + ".");
if (result == 1)
{
JPanelImage jpi = new JPanelImage ();
cash = cash - bet;
}
if (result == 2)
{
JPanelImage jpi = new JPanelImage ();
cash = cash - bet;
}
if (result == 3)
{
JPanelImage jpi = new JPanelImage ();
cash = cash - bet;
}
if (result == 4)
{
JPanelImage jpi = new JPanelImage ();
cash = cash - bet;
}
if (result == 5)
{
JPanelImage jpi = new JPanelImage ();
cash = cash - bet;
}
if (result == 6)
{
JPanelImage jpi = new JPanelImage ();
cash = cash + bet + bet;
}
}
}
}
}
|
Any ideas on how I can get the program to show the correct pictures? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
btiffin
![](http://compsci.ca/v3/uploads/user_avatars/189169540547b535e50e4a7.jpg)
|
|
|
|
![](images/spacer.gif) |
Regals
|
Posted: Fri Jun 06, 2008 12:40 pm Post subject: Re: Need help with project |
|
|
I'm not understanding exactly how that program works. Can someone explain it for me. Thanks. |
|
|
|
|
![](images/spacer.gif) |
btiffin
![](http://compsci.ca/v3/uploads/user_avatars/189169540547b535e50e4a7.jpg)
|
Posted: Fri Jun 06, 2008 1:19 pm Post subject: Re: Need help with project |
|
|
For your needs; ignore the applet JFrame (RollDice.java) and the JPanel extension (RollDicePanel.java) and hone in on the last source listing (Die.java). It should give you enough hints about one way of drawing a die in a window. In particular the paintComponents method and how it draws the pips on the white background. (drawSpot and its usage of fillOval).
Cheers |
|
|
|
|
![](images/spacer.gif) |
Regals
|
Posted: Mon Jun 09, 2008 1:00 pm Post subject: Re: Need help with project |
|
|
Alright, so I have the dice being drawn to the screen. However, I now have a problem with the betting algorithm. I cant figure out what it's doing. Here is the code:
Java: |
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.text.*;
import java.math.*;
public class DiceGame extends JFrame
{
private static final int SPOT_DIAM = 9; // Diameter of spots
private static int cash = 100; // initial amount of chips
private static int bet = 0; // value of bet
public DiceGame () // creates the roll window
{
super ("Roll");
setSize (60, 100);
setDefaultCloseOperation (JFrame. EXIT_ON_CLOSE);
setVisible (true);
Container myContent = getContentPane ();
myContent. setBackground (Color. white);
FlowLayout myFlow = new FlowLayout ();
myPanel panel1 = new myPanel ();
myContent. add (panel1 );
setContentPane (myContent );
}
class myPanel extends JPanel
{
public void paintComponent (Graphics artist )
{
Graphics2D g2 = (Graphics2D) artist;
g2. setRenderingHint (RenderingHints. KEY_ANTIALIASING,
RenderingHints. VALUE_ANTIALIAS_ON);
// Paint background
int w = 60; // sets the width of the die image
int h = 60; // sets the height of the die image
g2. setColor (Color. white); // sets the colour of the rectangle
g2. fillRect (0, 0, w, h ); // fills the rectangle
g2. setColor (Color. black); // sets the colour of the border of the dice
g2. drawRect (0, 0, w - 1, h - 1); // draws the border
Toolkit myTools = Toolkit. getDefaultToolkit ();
int result = (int) (6 * Math. random () + 1); // gets a random integer from 1-6
if (result == 4) // will increase the cash if a 4 is rolled
{
cash = cash + bet;
}
else // will decrease the bet if anything but 4 is rolled
{
cash = cash - bet;
}
switch (result )// draws the die
{
case 1:
drawSpot (g2, w / 2, h / 2);
break;
case 3:
drawSpot (g2, w / 2, h / 2);
// Fall thru to next case
case 2:
drawSpot (g2, w / 4, h / 4);
drawSpot (g2, 3 * w / 4, 3 * h / 4);
break;
case 5:
drawSpot (g2, w / 2, h / 2);
// Fall thru to next case
case 4:
drawSpot (g2, w / 4, h / 4);
drawSpot (g2, 3 * w / 4, 3 * h / 4);
drawSpot (g2, 3 * w / 4, h / 4);
drawSpot (g2, w / 4, 3 * h / 4);
break;
case 6:
drawSpot (g2, w / 4, h / 4);
drawSpot (g2, 3 * w / 4, 3 * h / 4);
drawSpot (g2, 3 * w / 4, h / 4);
drawSpot (g2, w / 4, 3 * h / 4);
drawSpot (g2, w / 4, h / 2);
drawSpot (g2, 3 * w / 4, h / 2);
break;
}
}
}
private void drawSpot (Graphics2D g2, int x, int y )// used to draw the spots on the die
{
g2. fillOval (x - SPOT_DIAM / 2, y - SPOT_DIAM / 2, SPOT_DIAM, SPOT_DIAM );
}
public static void main (String [] args ) throws IOException
{
BufferedReader stdin = new BufferedReader (new InputStreamReader (System. in));
String inData;
while (cash >= 1) // the game will continue to run until the user is out of cash
{
System. out. println ("You currently have $" + cash ); // prints the current cash of the user
System. out. println ("Enter bet amount"); // prompts the user to input a bet amount
inData = stdin. readLine (); // gets the user's input
bet = Integer. parseInt (inData ); // converts the input into integer form
DiceGame dg = new DiceGame ();
}
}
}
|
Any input on how to fix this is appreciated. Thank you for your time.
-Regals |
|
|
|
|
![](images/spacer.gif) |
Regals
|
Posted: Wed Jun 11, 2008 10:50 am Post subject: Re: Need help with project |
|
|
bump |
|
|
|
|
![](images/spacer.gif) |
|
|