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

2017/2/23

package

1
2
3
danpost danpost

2017/2/27

#
Nosson1459 wrote...
(1) Missing methods:
  • addedToWorld
  • getIntersectingObjects
  • getNeighbours
  • getObjectsAtOffset
  • getObjectsInRange
  • getOneIntersectingObject
  • getOneObjectAtOffset
  • intersects
  • isTouching
  • removeTouching
The reason these methods are missing is because they have 'protected' access. You can only call them from within the Actor subclass(es) that the actor belongs to (from the class the actor was created from up to the Actor class)..
Nosson1459 Nosson1459

2017/2/28

#
So if it isn't a greenfoot.Actor subclass then how does it have the other methods, and what needs to be done to make it an Actor subclass (it already extends greenfoot.Actor)?
danpost danpost

2017/2/28

#
Nosson1459 wrote...
So if it isn't a greenfoot.Actor subclass then how does it have the other methods, and what needs to be done to make it an Actor subclass (it already extends greenfoot.Actor)?
Maybe we are talking about two different things here. I am saying that line 12 here:
import greenfoot.World;
import javax.swing.JButton;

/**
 * Write a description of class Frame here.
 * 
 * @author Yehuda (1/2 of Nosson1459 - greenfoot.org user name)
 * @version 2/22/2017
 */
public class Frame extends World
{
    JButton button = new JButton();

    /**
     * Constructor for objects of class Frame.
     * 
     */
    public Frame()
    {
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        button.act();
    }
}
is creating a 'javax.swing.JButton' object -- not an actor. So, you cannot call 'act' on it (there is no 'act' method in the 'javax.swing.JButton' class or any class it extends from. If you are talking about some other code, show it and clearly explain what the issue is. Give detailed examples when possible.
danpost danpost

2017/2/28

#
Looking at the BIG code (the Component class), line 25 cannot be used because you cannot add on to a pre-existing package that easily (and you do not have access to alter that package, anyway -- all you can do is use it and create extensions. I would gather that some of the other classes used by the Component class, particular those imports starting with 'java.awt', require the objects they deal with to be of the 'java.awt' type and will not work on any class you create in your project unless they extend some 'java.awt.' class (meaning if the object extends greenfoot.Actor, it will complain).
Nosson1459 Nosson1459

2017/2/28

#
This is my picture: I have the code shown on the previous pages, which is the java.awt.Component class, it is part of the java.awt package and I took all that code and made a Component.java file. I took that file and I put it in a folder called "awt" the folder "awt" is in a folder called "java" which means when you do "import java.awt.Component;" it will be talking about MY class. If you want you can take your TextImage.java put it in a folder called "text" and have that folder in a folder called "greenfoot" then when you do "import greenfoot.text.TextImage;" it will work fine. In MY Component class (shown previously) I added in "extends greenfoot.Actor" and it doesn't complain. I also made a folder javax with swing in it and the three swing classes mentioned in this discussion, so now JButton extends AbstractButton which extends JComponent which extends Container which extends Component which extends greenfoot.Actor which extends java.lang.Object (instead of Component extending java.lang.Objet directly), so JButton is an extension of greenfoot.Actor. so... Why can't I access the protected methods and everything else you waid about yes or not extending greenfoot.Actor?
Nosson1459 Nosson1459

2017/2/28

#
So you can look at it as if I created my own JButton class. I can change the name of the class and change around whatever I want in the class (but when I change the name there are two methods that get errors isDefaultButton and removeNotify) so now the pre-existing package is my own new one.
danpost danpost

2017/2/28

#
Nosson1459 wrote...
now the pre-existing package is my own new one.
Again, the class may have the same code and same implementations, but it is an entirely different class from the 'java.awt.Component' class when you have it extend greenfoot.Actor. You can do whatever you want to make it more like it (even by making the package names equivalent); but it is still a different class in a different package. My TextImage class is a new class that extends GreenfootImage. It does not try to make an actor out of a GreenfootImage object. All I did was extend the GreenfootImage class to give it extra functionality. You are trying to make one type of object into a type of Actor; but all you are doing is creating a NEW type of Actor. Maybe you are fooling the compiler into executing some methods because the 'package/class' names are the same; but, you cannot fool the working of the code. If a internal method called in one of the 'java.awt' classes requires a 'java.awt.Component' object from its own package, then you are doomed. You will not be able to get it to work like you think you might.
Nosson1459 Nosson1459

2017/2/28

#
I know your TextImage class just extends GreenfootImage but I was using it as an example to show that I can use it's methods bu doing import greenfoot.fileName.TextImage. Also Component is part of some type of java.awt package that has to do with java because on one of the lines it uses Toolkit and when I change the package it says that it has to be in the same package to access that method which means that before I changed the package they were in the same package even though I didn't make a Toolkit.java class in my OWN package.
Nosson1459 Nosson1459

2017/2/28

#
Nosson1459 wrote...
I can change the name of the class and change around whatever I want in the class (but when I change the name there are two methods that get errors isDefaultButton and removeNotify) so now the pre-existing package is my own new one.
When I change the name I get a lot of errors in the other classes.
You need to login to post a reply.
1
2
3