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

2015/2/17

How to make a clock hand

1
2
efe efe

2015/2/17

#
The problem I am having when trying to program a clock application is the fact that the turn command turns the actor (clock hand) around its center. For the clock hand I would need it to turn around its base. Is there an easier way than creating a clock hand image with the stretch from center to base being invisible?
danpost danpost

2015/2/17

#
efe wrote...
The problem I am having when trying to program a clock application is the fact that the turn command turns the actor (clock hand) around its center. For the clock hand I would need it to turn around its base. Is there an easier way than creating a clock hand image with the stretch from center to base being invisible?
No. That is the easiest way.
GreenfootImage hand = new GreenfootImage("clockhand.png");
GreenfootImage handImg = new GreenfootImage(hand.getWidth()*2, hand.getHeight());
handImg.drawImg(hand, hand.getWidth(), 0);
setImage(handImg);
danpost danpost

2015/2/17

#
There is a way to accomplish that without doubling the size of the image to have it rotate at its center. Every act method you would have to move the hand to the centerpoint, set its new rotational value, and then move it half its width to its new location. Using the centerpoint of the world window:
public void move()
{
    rotationalValue += /** change in rotation */; // 'rotationalValue' is declared within the class to track the current rotation of the hand
    setLocation(getWorld().getWidth()/2, getWorld().getHeight()/2);
    setRotation(/** expression using 'rotationalValue */);
    move(getImage().getWidth()/2);
}
efe efe

2015/2/17

#
Thanks for the quick reply. I tried to implement your code but obviously I didn't understand it correctly. Thus these questions. clockhand.png is the original image of the clock hand, right? Your intention was to show me a way to draw a blank image on top of the old one to hide the lower half of the hand, right? When I implement your code (by the way in line 3 I think it has to say handImg.drawImage instead of drawImg) in a method upon calling the method I get a copy of the old hand drawn a little right to the original. It reacts the same way upon calling the turn method though, it turns around its center. Our messages seem to have crossed in cyberspace. I didn't have your second post, when I wrote my reply. Let me analyze first.
Super_Hippo Super_Hippo

2015/2/17

#
In the first post, danpost created a transparent image with width twice as long as the original. Then he draw the old one on it. So the middle of the actor isn't in the middle of the visible image anymore, because the image is transparent on the other side.
danpost danpost

2015/2/17

#
Yes, "clockhand.png" is the original image. You could possibly just use 'getImage()' to acquire it. Yes, it creates an image twice as long with half of it being transparent (invisible), so the visible part appears to rotate at its end (which is at the center of the image). Correct. Line 3 should be 'handImg.drawImage(...)' -- not 'handImg.drawImg(...)'.
efe efe

2015/2/17

#
Yeah, thats what I thought too, but the code doesn't give me that result.
danpost danpost

2015/2/17

#
Please show how you tried to implement it by posting what code you are using.
efe efe

2015/2/17

#
There is a class LongHand which has a method newImage which executes danposts code:
public void newImage(){
        GreenfootImage hand = new GreenfootImage("LongHand.png");
        GreenfootImage handImg = new GreenfootImage(hand.getWidth()*2, hand.getHeight());
        handImg.drawImage(hand, hand.getWidth(), 0);
        setImage(handImg);  
    }
I manually create a new LongHand Object and call the method newImage. The result is a new hand shifted in x direction which behaves exactly the way the original does, i.e. it turns around its center.
danpost danpost

2015/2/17

#
Please post the entire LongHand class code.
efe efe

2015/2/18

#
import greenfoot.*;

/**
 * Write a description of class LongHand here.
 * 
 * @author (EFE) 
 * @version (2014-2-17)
 */
public class LongHand extends Actor

{
    /**
     * Act - do whatever the LongHand wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public LongHand(){
    }

    public void act() 
    {
        // Add your action code here.
        move();
    } 

    public void newImage(){
        GreenfootImage hand = new GreenfootImage("LongHand.png");
        GreenfootImage handImg = new GreenfootImage(hand.getWidth()*2, hand.getHeight());
        handImg.drawImage(hand, hand.getWidth(), 0);
        setImage(handImg);  
    }

    public void move()
    {
        turn(5);

    }

}
danpost danpost

2015/2/18

#
It would be better to insert lines 27 through 30 at line 18 (removing the 'newImage' method).
efe efe

2015/2/18

#
I completely agree in the final version though. At this point I am testing and wanted to be able to compare situations before and after drawing a new image. If I put the code for creating a new image in the constructor the hand still rotates around its center.
danpost danpost

2015/2/18

#
I placed the LongHand class above into a scenario and tested it. I modified it slightly because I do not have a copy of your "LongHand.png" image file. I replaced line 27 with the following two lines:
GreenfootImage hand = new GreenfootImage(250, 3);
hand.fill();
Then, I manually created a LongHand object and ran the 'newImage' method on it; and, finally, clicked on the Run button. The hand spun around as if on its end. I do not know what other code you may have in other classes that may alter its behavior; but, because you say it still rotates around its center, I would say that it is probably the case that there is some.
efe efe

2015/2/18

#
With those modifications I can duplicate the behaviour you are describing. Thus the problem is with my LongHand drawing. I guess the problem is that the main extension in the graphic is in y direction (bottom to top) whereas the main extension in yours is in x direction (left to right).
There are more replies on the next page.
1
2