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

2019/12/12

How to I draw?

pio22mibi pio22mibi

2019/12/12

#
In my game, I need to be able to drawing while using my mouse but I am not sure how. Can anyone help me??
danpost danpost

2019/12/12

#
pio22mibi wrote...
In my game, I need to be able to drawing while using my mouse but I am not sure how. Can anyone help me??
What have you tried? or, what all do you think it involves (as far as coding is concerned)?
pio22mibi pio22mibi

2019/12/14

#
I have not tried anything because I am not sure how to do it.
danpost danpost

2019/12/15

#
pio22mibi wrote...
I have not tried anything because I am not sure how to do it.
danpost wrote...
What have you tried? or, what all do you think it involves (as far as coding is concerned)?
I am not actually asking for code itself in the bold part.
pio22mibi pio22mibi

2019/12/17

#
Well I think it consists of making a circle follow the mouse and making a new circle very very quickly so it end up with a line made up of circle. I am not sure if this is how you would do it but this would makes sense to me.
danpost danpost

2019/12/17

#
pio22mibi wrote...
Well I think it consists of making a circle follow the mouse and making a new circle very very quickly so it end up with a line made up of circle. I am not sure if this is how you would do it but this would makes sense to me.
I can see how that might make sense. However, the mouse can move much faster than 1 pixel per act cycle; so, this could (will) result in "jumping" (AKA: line breaks). Also, unless you want to show the current mouse action, nothing needs to follow the mouse. Now, you mentioned the mouse and making circles. What classes will be extensively used, here? Are you familiar with them at all?
pio22mibi pio22mibi

2019/12/24

#
I am not familiar with these classes at all. Could you help me?
danpost danpost

2019/12/24

#
Okay. For mouse, you will need the getMouseInfo method from the Greenfoot class, which return a MouseInfo object that contains the latest state of the mouse, and you will need multiple methods from the MouseInfo class, which returns those states (location coordinates, button actions, etc.). The following is an example of getting the location of the mouse:
1
2
3
4
5
6
7
MouseInfo mouse = Greenfoot.getMouseInfo();
if (mouse != null))
{
    int mouseX = mouse.getX();
    int mouseY = mouse.getY();
    //  ...
}
Sometimes, when the mouse is inactive (no movement or change in button states), a null value is returned in lieu of a MouseInfo object. Therefore, to avoid a NullPointerException, line 2 is added. You will need, at minimum, the GreenfootImage class to draw anything. Review its methods to see what is available. You will probably also make use of the Color class. All new GreenfootImage object, by default, have their drawing color set to BLACK. You will find that the initial drawing color set to the world background is WHITE.
pio22mibi pio22mibi

2019/12/26

#
Thank you so much. I will try to figure it out and if I can't I will need your help :)
pio22mibi pio22mibi

2020/1/1

#
I have tried a code but nothing happens. I don't know if any of it is right or not. I want to draw whemn the mouse is held down put I am not sure how.
1
2
3
4
5
6
7
8
9
10
public void act() 
 {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if (mouse != (null)){
            int mouseX = mouse.getX();
            int mouseY = mouse.getY();
            GreenfootImage drawedImage = new GreenfootImage("hole.png");
            getWorld().getBackground().drawImage(drawedImage, 100, 100);
        
        }
danpost danpost

2020/1/1

#
pio22mibi wrote...
I have tried a code but nothing happens. I don't know if any of it is right or not. I want to draw whemn the mouse is held down put I am not sure how. << Code Omitted >>
If you want to draw at mouse location, the you need to change the 100, 100 in line 8. Also, you can replace lines 3 and 4 with:
1
2
if (Greenfoot.mousePressed(null) || Greenfoot.mouseDragged(null)){
    MouseInfo mouse = Greenfoot.getMouseInfo();
pio22mibi pio22mibi

2020/1/2

#
Thank you so much!! It works now
You need to login to post a reply.