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

2014/5/22

drawLine(x1, y1, x2, y2) isn't drawing a line ;_;

blankaex blankaex

2014/5/22

#
So I'm trying to make a program that can solve the travelling salesman problem, and so far I've managed to represent my scenario in a matrix. In this matrix, I've made objects of a distance class, which would represent the edges of my graph. I want to use the drawLine method, and it compiles fine, but it's not doing anything. I've imported java.awt.Color, set the drawing colour, etc, etc, no progress. Anyone able to help please?
public class Distance extends Actor
{
    int _x1;
    int _y1;
    int _x2;
    int _y2;
    int _distance;
    GreenfootImage GFI = new GreenfootImage(1280,720);
    
    public Distance(int x1, int y1, int x2, int y2, int distance)
    {
        _x1 = x1;
        _y1 = y1;
        _x2 = x2;
        _y2 = y2;
        _distance = distance;
        GFI.setColor(Color.BLACK);
        GFI.drawLine(_x1, _y1, _x2, _y2);
    }
}
danpost danpost

2014/5/22

#
How are you creating an object of this class? Are you adding it to the world? and, for the big question: Should you not set the image of this object?
blankaex blankaex

2014/5/23

#
In the subclass of world, multiple lines of
new Distance(140, 500, 330, 210, 60)
and I'm not sure what you mean by setting the image. Isn't it better to have a transparent image to draw lines on top of?
danpost danpost

2014/5/23

#
What I am saying is that you are creating an actor object of the Distance class, whose constructor draws a line on an image the it has a reference to but is not set as the image of the actor. You will never see the drawn line unless you set that image to the actor:
setImage(GFI);
danpost danpost

2014/5/23

#
There must be a better way to do what you are trying to do.
You need to login to post a reply.