This site requires JavaScript, please enable it in your browser!
Greenfoot back
Hmac2000
Hmac2000 wrote ...

2018/1/28

JOptionPane

Hmac2000 Hmac2000

2018/1/28

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import javax.swing.*;
import java.util.*;
 
public class Question1 extends World
{
    /**
     * Constructor for objects of class Questions.
     *
     */
    public Question1()
    {   
        // Create a new world with 600x600 cells with a cell size of 1x1 pixels with a quit button.
        super(600, 600, 1);
        addObject(new Quit(), 75, 30);
 
        double[] cash = new double[] {52.50, 99.99, 36.00, 10.35, 72.11};
        double inWallet = cash[((int)(Math.random() * 4 + 0))];
 
        String q = "Hannah had $" + inWallet + " in her wallet. She spent a quarter of that\nwhen she went out with her friends. She owed one quarter\nof what was remaining to her mother so she paid her back.\nHow much did she have left?\n(Round final ANSWER only to 2 decimal places)";
 
        // Image of Text is 20 font size, black Text, white background
        GreenfootImage  ImageText = new GreenfootImage(q, 20, Color.BLACK,Color.WHITE);
        GreenfootImage bg = getBackground();
        bg.drawImage(ImageText, 100, 100); // upper left corner of image is at 100, 100.
 
        double spent = 0.25*inWallet;
        double left1 = inWallet-spent;
        double mom = 0.25*left1;
        double answer = left1-mom;
        double result = (double)Math.round(answer * 100000d) / 100000d;
        Greenfoot.delay(500);
 
        String inputstring = JOptionPane.showInputDialog("Your answer here:");
 
        if(inputstring.equals(Double.toString(result)))
        {
            Greenfoot.setWorld(new Question2());
            Greenfoot.playSound("ding.mp3");
        }
        else
        {
            Greenfoot.playSound("Wrong.mp3");
            addObject(new Wrong(), 300, 300);
            Greenfoot.delay(500);
            Greenfoot.setWorld(new Question2());
        }
    }
}
Not sure how to get the world to display before the JOptionPane.
Yehuda Yehuda

2018/1/28

#
Move it out of the constructor method. Now it pops up in middle of constructing the world.
danpost danpost

2018/1/28

#
There is a 'repaint' method in the World class that you could use.
Yehuda Yehuda

2018/1/28

#
You can get the final amount in the wallet by multiplying the original amount by 9/16ths (0.5625). result = inWallet * 0.5625
Hmac2000 Hmac2000

2018/1/28

#
Yehuda wrote...
Move it out of the constructor method. Now it pops up in middle of constructing the world.
Move it where?
Yehuda Yehuda

2018/1/29

#
You can put it in the act method (you can create one). It's not going to come more than once since(/if) you change the world right afterwards, with that other world having its own act.
danpost danpost

2018/1/29

#
Okay -- you cannot repaint a world that has not been completely constructed. Therefore, your initial world cannot be a question world if you are to use the constructor for the questioning. Secondary world constructors could be used; however, they again must be created after the initial world has been completely constructed (the secondary world being created from either the act method or the started method of the initial world). The secondary world would have to use the initial world background to pose the questions from its constructor because the initial world is the one that is active at the time the second world is being created (the secondary world is not yet set as the active world). Otherwise, the questions will need to be posed in the act method of the worlds.
You need to login to post a reply.