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

2013/9/29

Single Class Code?

Ed.Betteridge Ed.Betteridge

2013/9/29

#
How could I make a program without needing to make any classes other than a world? To be more specific: How can I make objects from the world without actor classes. And similarly, make GreenfootImage(s), in this case, textboxes. My example code (from: http://www.greenfoot.org/scenarios/9362): **Background (I want this to be my only SubClass)** Buttons Button=new Buttons; Labels Label=new Labels; public Background(){ for(int i=0;i<20;i++){ Button = new Buttons(); Label=new Labels(LabelText); Button.setImage(myImage); addObject(Button, 10+i, 10+1); addObject(Label,10+i, 10+i); } TextField TextBox = new TextField("0"); addObject(TextBox,235,25); **TextField** public TextField(String text){ GreenfootImage TextBox= new GreenfootImage(20*text.length(),20); TextBox.drawString(text,2,20); setImage(TextBox); } **Buttons is Empty** Thanks :)
danpost danpost

2013/9/29

#
The greenfoot.GreenfootImage class contains several methods (and constructors) that can be used to create or introduce images into you project; this includes methods that can alter current images. Without actor images, the only way to display anything is using the world background image; which can be altered in part or in whole. Here are some things to consider when drawing on images: (1) an area drawn on is not totally changed where transparency is involved; and (2) altering the whole background image each frame may cause lag. It is really the combination of these two issues that may determine how you go about creating and drawing images on the background, as well as what images you need to keep in instance world fields. Without actors, mouse action only have the world object to act on; so, mouse clicks will either be on or off the world, checked by 'Greenfoot.mouseClicked(this)', and the location of the click will have to be programmatically accessed by getting a MouseInfo object, getting the coordinates of where in the world the mouse was clicked, and comparing that to the possible 'hit-box' locations where you would do some action if clicked there. You could look at several of my scenarios to see examples of altering the world background; namely, Game of Life Type2: a fairly simple one-world-class scenario; Pause World Class: a demonstration of using the background of the world to show the visual state of another world; PIP Actor Class; uses the same technique a in the 'Pause World Class' to create the image of an inactive world. Although the 'Pause World Class' and the 'PIP Actor Class' use the images from the Actor objects of the inactive world to draw on its background, those images still must come from somewhere (either created programmatically, or saved in the 'images' folder', or a combination of both). Hope they help you in understanding what may be required for your project.
Ed.Betteridge Ed.Betteridge

2013/9/29

#
Thanks for the response danpost. I understood most of what you said (I am a really basic beginner). You may have already answered this question in what you said, but I'll ask more directly this time, in my Calculator project, why does Greenfoot NEED an actor for the Buttons, it's code is empty, so what does it do? Is there not a way of programmatically making an actor or another method of doing this without the separate subclass? I was having a go experimenting with code, and made this: Actor Button; public myWorld(){ super(250, 350, 1); Button.setImage("button-blue.png"); addObject(Button,10,10); } It only gave me a NullPointerException for the setImage line. Is there a way to make this work?
danpost danpost

2013/9/29

#
Actually, it can do several things: it displays the image of the button; it allows detection of clicks specifically on it; it could hold the text it was created for. Yes, these can be done on the world background and on the world object; but, then you would have to write the code to determine which button image on the background image a click occurs, among other things (thing about what you would need to do without 'if (Greenfoot.mouseClicked(buttons)...)'?).
davmac davmac

2013/9/29

#
Ed.Betteridge, if what you're saying is that you want an actor without having to create a class for it, then you can do: Actor button = new Actor() {}; You can then set its image etc as per your code above. You must have an actor object before you can set the image, that's why you got a NullPointerException (you declared a variable, but you didn't set it to anything). Note that this won't work: Actor actor = new Actor(); ... because Actor is an abstract class, you can't create it. The code above (new Actor() {}) actually creates an instance of an unnamed subclass of Actor. Please use lower-case letters to start variable names - it's a Java convention and it's very confusing for others if you break this. So it should be "button" and not "Button". Class names start with a capital.
danpost danpost

2013/9/29

#
Wow. Thanks, davmac. Creating unnamed actors is something I was not aware of and is something that could be quite useful!
SPower SPower

2013/9/29

#
@danpost @davmac The same goes for me, thanks davmac!
danpost danpost

2013/9/29

#
I found you cannot use 'null' as the class for 'instanceof'.
Ed.Betteridge Ed.Betteridge

2013/9/30

#
Thanks davmac, I think that is exactly what I was looking for, I'll give it a go :)
You need to login to post a reply.