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

2011/12/16

project..

joekon23 joekon23

2011/12/16

#
This is my project Interaction Defined 1. Start up by clearing the screen in a random basic color, then waiting for a keystroke. (5 points) 2. Wait for a keystroke (only from the list below), then continuously perform the action associated with that keystroke until another key is pressed: Keypress Action c Pressing “c”, clear the screen to a “white” color. (5 pts) r Pressing “r”, clears the screen to a random color. (5 pts) 1 Pressing “1” (the number one), will draw continuous randomly-placed points in random colors. (10 pts) 2 Pressing “2”, will draw continuous random-lengthed lines in random colors. (10 pts) 3 Pressing “3”, will draw continuous random-sized circles in random colors. (10 pts) 4 Pressing “4”, will draw continuous random-sized rectangles in random colors. (10 pts) e Pressing “e”, will end all drawing until the next “1”, “2”, “3”, “4”, or “5” is pressed. (5 pts) f Pressing “f”, will toggle the “fill-mode” of the circles and rectangles. So for example, if I press “3”, random empty-circles will be drawn in random colors. If I then press “f”, then any new circles drawn should be filled with the same random color. If I then press “f” again, then any new circles drawn will be empty. The key “f” acts as a toggle (an on-off switch) for filling new shapes being drawn. (20 pts) I am having trouble adding the color to my project.. here is my code public class Canvas extends World { /** * Constructor for objects of class Canvas. * */ public Canvas() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); GreenfootImage background = getBackground(); background.fill(); } private boolean oneCheck = false; public void act(){ java.lang.String k = Greenfoot.getKey(); GreenfootImage background = this.getBackground(); if (Greenfoot.isKeyDown("c")) { background.fill(white); } if(Greenfoot.isKeyDown("r")) { } if(Greenfoot.isKeyDown("1")) { } if (Greenfoot.isKeyDown("2")) { } if (Greenfoot.isKeyDown("3")) { } if (Greenfoot.isKeyDown("4")) { } if (Greenfoot.isKeyDown("5")) { } } } Im stuck..
dtcrane dtcrane

2011/12/16

#
Probably should have done Scott's project sooner, huh?
dtcrane dtcrane

2011/12/16

#
.
danpost danpost

2011/12/17

#
First off, what import statements are in your Canvas class? Secondly, you do not need to say 'java.lang.String' as 'String' is sufficient. Thirdly, why are you using 'isKeyDown', when you have already stored the keystroke in 'k'? Just say, 'if ("1".equals(k)) { ... }', etc. But before that, make sure that k is NOT 'null' !!!
String k = Greenfoot.getKey();
if (k == null) return;
if ("c".equals(k)) {
    //  code to clear screen to white
}
if ("r".equals(k)) {
etc. Something else, 'background.fill(white);' is not a valid statement. You need to use 'background.setColor(Color.white);' followed by 'background.fill();'
You need to login to post a reply.