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

2013/7/30

Polygon

8bitcarrotjuice 8bitcarrotjuice

2013/7/30

#
Hello, I am having problems creating a polygon and an array. So far I have this:
import greenfoot.*; 

public class Mouse extends Conector
{
    public void act() 
    {
 
    }    
    public void addedToWorld(World world)
    {
        GreenfootImage image = new GreenfootImage(20, 20);  
        int[] xs = { 0, 5, 10};  
        int[] ys = { 10, 0, 10 };  
        image.drawPolygon(xs, ys, 1);  
}
}
The aim is to create a triangular cursor. Help? Please?
davmac davmac

2013/7/30

#
The third parameter to drawPolygon is the number of points. You are specifying 1, but your triangle has 3 points.
8bitcarrotjuice 8bitcarrotjuice

2013/7/30

#
... Stupid me. Thanks Dav.
8bitcarrotjuice 8bitcarrotjuice

2013/7/30

#
Dav, it still does not draw it, it just shows up as a greenfoot icon... Here is what I have so far:
import greenfoot.*; 
import java.awt.Color;  
public class Mouse extends Conector
{
    public void act() 
    {
        MouseInfo m = Greenfoot.getMouseInfo();
        if(m!=null)        
        setLocation(m.getX(),m.getY());

    }    
    public void addedToWorld(World world)
    {
        GreenfootImage image = new GreenfootImage(20, 20);  
        image.setColor(new Color(120,100,100,255));
        int[] xs = { 0, 5, 10};  
        int[] ys = { 10, 0, 10 };  
        image.drawPolygon(xs, ys, 3);  
}
}
MatheMagician MatheMagician

2013/7/30

#
After you draw the polygon, put this line of code:
setImage(image);
8bitcarrotjuice 8bitcarrotjuice

2013/7/30

#
I'm tripping today... Thanks Mathe
8bitcarrotjuice 8bitcarrotjuice

2013/7/30

#
How would I make it so the actual cursor disapears?
Gevater_Tod4711 Gevater_Tod4711

2013/7/30

#
You can use this methods to make the mouse cursor disappear:
//first import these classes:
import greenfoot.core.WorldHandler;
import java.awt.Point;
import java.awt.Toolkit;

//then you can execute this methods:
public void act() {
        cursorDissapear();
    }
    
    public void cursorDissapear() {
        WorldHandler.getInstance().getWorldCanvas().setCursor(Toolkit.getDefaultToolkit().createCustomCursor(new GreenfootImage(1, 1).getAwtImage(), new Point(0, 0), "invisibleCursor"));
    }
8bitcarrotjuice 8bitcarrotjuice

2013/7/30

#
Thanks!
davmac davmac

2013/7/31

#
Firstly: Gevater_Tod4711, if you tell people to use private API methods, please make sure they understand the consequences. Private API methods aren't documented and are not guaranteed to work (or even be available) in future versions. Next, if you're going to set the cursor, don't do it in the act method, do it in the world's 'started' method. There's no need to set the cursor over and over again. Finally, if you're going to mess with Swing, follow the threading rules for Swing, so:
    public void cursorDissapear() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                WorldHandler.getInstance().getWorldCanvas().setCursor(Toolkit.getDefaultToolkit().createCustomCursor(new GreenfootImage(1, 1).getAwtImage(), new Point(0, 0), "invisibleCursor"));  
            }
        });
    }  
Really, it's best to not resort to hacks like this though.
Gevater_Tod4711 Gevater_Tod4711

2013/7/31

#
@davmac Ok I'll think of this next time.
You need to login to post a reply.