PDA

View Full Version : help with the codes!


makarov
11-14-2009, 02:30 AM
This applet should generate 3 random colours, in the 3 coloured boxes to simulate the rolling of 3 dice. The player choose in the 3 drop down menu below the dice as guesses for what will be thrown.
When the button is is pressed, the 3 die are "rolled" and their scores are matched with the guesses in the boxes. The player is told if there are 0, 1, 2 or 3 matches.

cs_student
11-14-2009, 03:17 AM
I charge $60/hour. Will you be paying by paypal?

If you would like help with your homework, then maybe you could give it a try, post the code, and include a detailed description of what you are having trouble with and why you did what you did.

We don't do your homework for you (for free).


Regards,


cs_student

luboldja
11-14-2009, 07:31 PM
If you have something already started, post it inside a code tag, and someone will give you help.

makarov
11-15-2009, 10:19 PM
If you have something already started, post it inside a code tag, and someone will give you help.

import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DiceRoller extends Applet implements ActionListener{


private Die d1,d2;
private Button btnRoll;


public void init(){
setSize(220,200);
setBackground(Color.green);


d1 = new Die(20,40);
d2 = new Die(120,40);


btnRoll = new Button("ROLL DICE");
btnRoll.addActionListener(this);
add(btnRoll);
}

/**
* Display the current values of the 2 dice
*/
public void paint(Graphics g){
d1.display(g);
d2.display(g);
}

/**
* When the button is clicked roll the dice.
*
*/
public void actionPerformed(ActionEvent ae) {
d1.roll();
d2.roll();
repaint();
}
}

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

/**
* Simple class to demonstrate a Die
*
* @author Peter lager
*
*/
public class Die {

private int value;
private int posX, posY;
private Font font;

/**
* Create a new die at the given position and
* set the initial value to 1
*
* @param posX
* @param posY
*/
public Die(int posX, int posY) {
this.posX = posX;
this.posY = posY;
value = 1;
font = new Font("Courier", Font.BOLD, 60);
}

/**
* Set the top left position to display the die
* @param posX
* @param posY
*/
public void setPos(int posX, int posY){
this.posX = posX;
this.posY = posY;
}

/**
* @return the value
*/
public int getValue() {
return value;
}

/**
* @param value the value to set
*/
public void setValue(int value) {
this.value = value;
}

/**
* @param font the font to set
*/
public void setFont(Font font) {
this.font = font;
}

/**
* Roll the dice to get a random value between 1& 6
*/
public void roll(){
value = (int)(Math.random()*6 + 1);
}

public void display(Graphics g){
g.setColor(Color.yellow);
g.fillRect(posX, posY, 80, 80);
g.setColor(Color.black);
g.drawRect(posX, posY, 80, 80);
g.setFont(font);
g.drawString(""+value, posX + 20, posY+60);
}
}

makarov
11-15-2009, 10:21 PM
I charge $60/hour. Will you be paying by paypal?

If you would like help with your homework, then maybe you could give it a try, post the code, and include a detailed description of what you are having trouble with and why you did what you did.

We don't do your homework for you (for free).


Regards,


cs_student
Listen! Am not lazy & am not asking 4 anybody 2 do the entire work 4 me. Am simple asking 4 help with ideas & suggestions regarding what I've already done.

cs_student
11-15-2009, 11:42 PM
It would be nice to let us know what you needed to do without making us have to go find what is missing in your code. I'm assuming that your just missing the dropdown menus.

First off, instead of calling repaint(), I would use update() as it will make your life a lot easier when trying to debug since you won't have to worry about any concurrent drawing.

As for the menus. Use a JComboBox (http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JComboBox.html). Then when the button is pressed, roll the dice and get their colors. Then check if the color corresponds to what is in the ComboBox.