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

2014/3/14

Mouse Buttons

1
2
3
4
Pointifix Pointifix

2014/3/14

#
without mouse != null i got an java exception mhhmm
danpost danpost

2014/3/14

#
Where is this error occurring (show error message in entirety and specify which line it occurs at).
danpost danpost

2014/3/14

#
I could not get any exceptions using the conditions I provided above. The code within the 'if' block was only executed when one of two conditions occurred: the first is if the mouse stayed still over the object and the right mouse button was pressed and released; the other (which can be fixed) was when the right mouse button was pressed while the mouse was NOT over the object and then moved over it before the button was released. To fix that, change the argument in 'mouseDragEnded' to 'null'.
Pointifix Pointifix

2014/3/14

#
i got a null pointer exception cause mouse is not defined from the beginning, just later on in the code, that was the mistake ^^ ok but still my item turn around twice when i press right button ...
danpost danpost

2014/3/14

#
Pointifix wrote...
i got a null pointer exception cause mouse is not defined from the beginning, just later on in the code, that was the mistake ^^ ok but still my item turn around twice when i press right button ...
Are you still getting the error, or was this corrected? There is probably some unseen (as far as I am concerned) code that is causing the behavior you are seeing. You will probably have to show the code for the entire actor class.
Pointifix Pointifix

2014/3/14

#
i tried to delete the check of mouse != null now and then i got a null pointer exception when i moved my mouse out of my world
Pointifix Pointifix

2014/3/14

#
the whole code of actor? this is my world class no actor, but i can post if you want^^
danpost danpost

2014/3/14

#
Pointifix wrote...
the whole code of actor? this is my world class no actor, but i can post if you want^^
Sorry, I presumed. Being you were dealing with 'rotation', I though maybe it was. Anyway, yes, please post the subclass of World, include in your post the entire error message you are getting. BTW, if you would rather not post it, you could e-mail it to me to look at.
Pointifix Pointifix

2014/3/14

#
import greenfoot.*;
import java.awt.Color;

public class MyWorld extends World
{
    //RASTER VAR #################################################################################

    public final int SIZE_RASTER = 70;
    
    public final int LENGTH_RASTER_X = 20;
    public final int LENGTH_RASTER_Y = 20;
    
    private int x_raster_pos = 0;
    private int y_raster_pos = 0;
    
    private int x_mouse_pos = 0;
    private int y_mouse_pos = 0;
    
    private int x_raster_off = SIZE_RASTER;
    private int y_raster_off = SIZE_RASTER;
    
    private int x_drag_off = 0;
    private int y_drag_off = 0;
    
    //Button 3 released?
    
    private boolean button3_release = false;
    
    //3 Dimensionales Array zur Speicherung aller Zellen (X,Y,Eigenschaften (Item?,Rotation?,Farbe?))
    
    public int[][][] itemfield = new int[LENGTH_RASTER_X][LENGTH_RASTER_Y][3];
    
    //Item Images
    
    private GreenfootImage[] image_item = new GreenfootImage[7];
    
    //Item Colors
    
    private Color[] item_colorarray = {Color.gray,Color.red,Color.blue,Color.green};
    
    //Item choosen
    
    private int item_actual = 1;
    private int item_rotation = 0;
    private int item_color = 0;
    
    //Mouse Info Variable ###########################################################################
    
    private MouseInfo mouse = Greenfoot.getMouseInfo();
    
    public MyWorld()
    {
        super(700,700,1);
        
        mouse = Greenfoot.getMouseInfo();
        
        ItemImageCreate();
        ResetItemField();
        Raster();
        
        getBackground().setColor(Color.black);
        DrawRasterPos();
    }
    
    public void act()
    {
        mouse = Greenfoot.getMouseInfo();
        
        getBackground().setColor(Color.white);
        DrawRasterPos();
        
        getBackground().setColor(Color.white);
        DrawItem();
        
        Raster();
        
        getBackground().setColor(Color.black);
        DrawRasterPos();
    }
    
    //Draw Actual Item ##################################################################################
    
    private void ItemAtMouse()
    {
        GreenfootImage image_atmouse = new GreenfootImage(SIZE_RASTER,SIZE_RASTER);
        
        String key = Greenfoot.getKey();
        
        if(key == "up")item_actual++;
        if(key == "down")item_actual--;
        
        if(key == "left")item_color++;
        if(key == "right")item_color--;
        
        if(mouse != null && mouse.getButton() == 3 && !Greenfoot.mouseDragEnded(this) && button3_release == false)
        {
            item_rotation++;
            if(item_rotation == 4)item_rotation = 0;
            button3_release = true;
        }
        if(mouse != null && mouse.getButton() != 3)
        {
            button3_release = false;
        }
        
        if(mouse != null)
        {
            image_atmouse.drawImage(image_item[item_actual],0,0);
            image_atmouse.rotate(item_rotation * 90);
            if(item_actual != 0)image_atmouse = ChangeImageColor(image_atmouse,new Color(item_colorarray[item_color].getRed(),item_colorarray[item_color].getGreen(),item_colorarray[item_color].getBlue(),155));
            getBackground().drawImage(image_atmouse,(x_mouse_pos - x_raster_pos - 1) * SIZE_RASTER + x_raster_off,(y_mouse_pos - y_raster_pos - 1) * SIZE_RASTER + y_raster_off);
            image_atmouse.rotate(360 - item_rotation * 90);
        }
        
        if(Greenfoot.mouseClicked(this) && !Greenfoot.mouseDragEnded(this) && mouse.getButton() == 1)
        {
            itemfield[x_mouse_pos][y_mouse_pos][0] = item_actual;
            itemfield[x_mouse_pos][y_mouse_pos][1] = item_rotation;
            itemfield[x_mouse_pos][y_mouse_pos][2] = item_color;
        }
    }
    
    //Item Images Creation ##############################################################################
    
    private void ItemImageCreate()
    {
        for(int i = 0;i < 7;i++)
        {
            image_item[i] = new GreenfootImage(SIZE_RASTER,SIZE_RASTER);
            image_item[i].setColor(Color.black);
        }
        
        //Item_Clear
        
        int[] x_clear1 = {0,10,SIZE_RASTER,SIZE_RASTER - 10};
        int[] y_clear1 = {10,0,SIZE_RASTER - 10,SIZE_RASTER};
        int[] x_clear2 = {SIZE_RASTER,SIZE_RASTER - 10,0,10};
        int[] y_clear2 = {10,0,SIZE_RASTER - 10,SIZE_RASTER};

        image_item[0].fillPolygon(x_clear1,y_clear1,4);
        image_item[0].fillPolygon(x_clear2,y_clear2,4);
        
        //Item_Full
        
        image_item[1].fill();
        
        //Item_Half

        image_item[2].fillRect(0,image_item[1].getHeight() / 2,SIZE_RASTER,SIZE_RASTER / 2);
        
        //Item_Triangle
        
        int[] x_triangle = {0,SIZE_RASTER,0};
        int[] y_triangle = {0,SIZE_RASTER,SIZE_RASTER};
        
        image_item[3].setColor(Color.black);
        image_item[3].fillPolygon(x_triangle,y_triangle,3);
        
        //Item_Slope
        
        int[] x_slope = {0,SIZE_RASTER,0};
        int[] y_slope = {SIZE_RASTER / 2,SIZE_RASTER,SIZE_RASTER};
        
        image_item[4].setColor(Color.black);
        image_item[4].fillPolygon(x_slope,y_slope,3);
        
        //Item_Cycle
        
        image_item[5].setColor(Color.black);
        image_item[5].fillOval(-SIZE_RASTER,0,SIZE_RASTER * 2,SIZE_RASTER * 2);
        
        //Item_Halfpipe

        for(int y=0;y < SIZE_RASTER;y++)image_item[6].drawLine((int)(Math.sqrt((SIZE_RASTER - 1) * (SIZE_RASTER - 1) - y * y) + 1),y,SIZE_RASTER,y);
    }
    
    //Item Field Reset ###################################################################################
    
    private void ResetItemField()
    {
        for(int i = 0;i < LENGTH_RASTER_X;i++)
        {
            for(int z = 0;z < LENGTH_RASTER_Y;z++)
            {
                itemfield[i][z][0] = 0;
                itemfield[i][z][1] = 0;
                itemfield[i][z][2] = 0;
            }
        }
    }
    
    //DrawItemField on Raster ############################################################################
    
    private void DrawItem()
    {
        GreenfootImage image_store = new GreenfootImage(SIZE_RASTER,SIZE_RASTER);
        
        for(int i = x_raster_pos;i <= x_raster_pos + getBackground().getWidth() / SIZE_RASTER && i < LENGTH_RASTER_X;i++)
        {
            for(int z = y_raster_pos;z <= y_raster_pos + getBackground().getHeight() / SIZE_RASTER && z < LENGTH_RASTER_Y;z++)
            {
                if(getBackground().getColor().equals(Color.white))
                {
                    getBackground().fillRect(SIZE_RASTER * (i - x_raster_pos) + x_raster_off - SIZE_RASTER,SIZE_RASTER * (z - y_raster_pos) + y_raster_off - SIZE_RASTER,SIZE_RASTER,SIZE_RASTER);
                }
                if(itemfield[i][z][0] != 0 && !getBackground().getColor().equals(Color.white))
                {
                    image_store.setColor(Color.white);
                    image_store.fill();
                    image_store.drawImage(image_item[itemfield[i][z][0]],0,0);
                    image_store = ChangeImageColor(image_store,item_colorarray[itemfield[i][z][2]]);
                    image_store.rotate(itemfield[i][z][1] * 90);
                    getBackground().drawImage(image_store,SIZE_RASTER * (i - x_raster_pos) + x_raster_off - SIZE_RASTER,SIZE_RASTER * (z - y_raster_pos) + y_raster_off - SIZE_RASTER);
                    image_store.rotate(360 - itemfield[i][z][1] * 90);
                }
            }
        }
    }
    
    //Raster Pos ######################################################################################
    
    private void DrawRasterPos()
    {
        getBackground().drawString("X: "+(x_mouse_pos+1),10,20);
        getBackground().drawString("Y: "+(y_mouse_pos+1),10,40);
        
        if(mouse != null && getBackground().getColor().equals(Color.white))
        {
            x_mouse_pos = x_raster_pos + (int)((mouse.getX() - x_raster_off - 2 + SIZE_RASTER) / SIZE_RASTER);
            y_mouse_pos = y_raster_pos + (int)((mouse.getY() - y_raster_off - 2 + SIZE_RASTER) / SIZE_RASTER);
        }
    }
    
    //RASTER ##########################################################################################
    
    private void Raster()
    {
        getBackground().setColor(Color.white);
        DrawRaster();
        
        RasterDrag();
        ResetRasterOffset();
        RasterBorder();
        
        getBackground().setColor(Color.lightGray);
        DrawItem();
        
        DrawRasterPos();
        
        ItemAtMouse();
        
        getBackground().setColor(Color.darkGray);
        DrawRaster();
    }
    
    private void DrawRaster()
    {
        for(int i = 0;i < getBackground().getWidth() / SIZE_RASTER;i++)
        {
            getBackground().drawLine(SIZE_RASTER * i + x_raster_off,0,SIZE_RASTER * i + x_raster_off,getBackground().getHeight());
            getBackground().drawLine(0,SIZE_RASTER * i + y_raster_off,getBackground().getWidth(),SIZE_RASTER * i + y_raster_off);
        }
    }
    
    private void RasterDrag()
    {
        if(Greenfoot.mouseDragged(this) && mouse != null)
        {
            x_raster_off += mouse.getX() - x_drag_off;
            y_raster_off += mouse.getY() - y_drag_off;
        }
        
        if(mouse != null)
        {
            x_drag_off = mouse.getX();
            y_drag_off = mouse.getY();
        }
    }
    
    private void RasterBorder()
    {
        if(x_raster_pos < 0)
        {
            x_raster_off = SIZE_RASTER;
            x_raster_pos = 0;
        }
        if(y_raster_pos < 0)
        {
            y_raster_off = SIZE_RASTER;
            y_raster_pos = 0;
        }
        if(x_raster_pos >= LENGTH_RASTER_X - getBackground().getWidth() / SIZE_RASTER)
        {
            x_raster_off = SIZE_RASTER;
            x_raster_pos = LENGTH_RASTER_X - getBackground().getWidth() / SIZE_RASTER;
        }
        if(y_raster_pos >= LENGTH_RASTER_Y - getBackground().getHeight() / SIZE_RASTER)
        {
            y_raster_off = SIZE_RASTER;
            y_raster_pos = LENGTH_RASTER_Y - getBackground().getHeight() / SIZE_RASTER;
        }
    }
    
    private void ResetRasterOffset()
    {
        while(x_raster_off > SIZE_RASTER || x_raster_off < 0 || y_raster_off > SIZE_RASTER || y_raster_off < 0)
        {
            if(x_raster_off > SIZE_RASTER)
            {
                x_raster_off -= SIZE_RASTER;
                x_raster_pos--;
            }
            if(x_raster_off < 0)
            {
                x_raster_off += SIZE_RASTER;
                x_raster_pos++;
            }
            
            if(y_raster_off > SIZE_RASTER)
            {
                y_raster_off -= SIZE_RASTER;
                y_raster_pos--;
            }
            if(y_raster_off < 0)
            {
                y_raster_off += SIZE_RASTER;
                y_raster_pos++;
            }
        }
    }
    
    //Change Color of an Image ################################################################################
    
    private GreenfootImage ChangeImageColor(GreenfootImage image_store,Color color)
    {
        for(int i = 0;i < image_store.getWidth();i++)
        {
            for(int z = 0;z < image_store.getHeight();z++)
            {
                if(image_store.getColorAt(i,z).getAlpha() > 0 && !image_store.getColorAt(i,z).equals(Color.white))
                {
                    image_store.setColorAt(i,z,color);
                }
            }
        }
        
        return image_store;
    }
}
Pointifix Pointifix

2014/3/14

#
The function ItemAtMouse should be interessting
Pointifix Pointifix

2014/3/14

#
i just thought posting is not possible cause maybe theres a maximum of symbols in greenfoot forums ;)
Pointifix Pointifix

2014/3/14

#
ah i think this is the solution: Greenfoot.mouseClicked() returns true if any mouse button was pressed AND released, so this with combination of mouse.getButton() == 3 it only joins the if once per rightclick :D
danpost danpost

2014/3/14

#
I am not sure if this is what you were wanting; but, comment out what you have before copying this in and trying:
private void ItemAtMouse()
{
    GreenfootImage image_atmouse = new GreenfootImage(SIZE_RASTER,SIZE_RASTER);
    String key = Greenfoot.getKey();
    if("up".equals(key)) item_actual++;
    if("down".equals(key)) item_actual--;
    if("left".equals(key)) item_color++;
    if("right".equals(key)) item_color--;
    if(Greenfoot.mouseClicked(this) && !Greenfoot.mouseDragEnded(this) && Greenfoot.getMouseInfo()..getButton() == 3)
        item_rotation = (item_rotation+1)%4;
        image_atmouse.drawImage(image_item[item_actual], 0, 0);
        image_atmouse.rotate(item_rotation*90);
        if (item_actual != 0) image_atmouse = ChangeImageColor(image_atmouse, new Color(item_colorarray[item_color].getRed(), item_colorarray[item_color].getGreen(), item_colorarray[item_color].getBlue(), 155));
        getBackground().drawImage(image_atmouse,(x_mouse_pos-x_raster_pos-1)*SIZE_RASTER+x_raster_off, (y_mouse_pos-y_raster_pos-1)*SIZE_RASTER+y_raster_off);
        image_atmouse.rotate(360 - item_rotation * 90);
    }
    if (Greenfoot.mouseClicked(this) && !Greenfoot.mouseDragEnded(this) && Greenfoot.getMouseInfo().getButton() == 1)
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        int x_mouse_pos = mouse.getX(), y_mouse_pos = mouse.getY();
        itemfield[x_mouse_pos][y_mouse_pos][0] = item_actual;
        itemfield[x_mouse_pos][y_mouse_pos][1] = item_rotation;
        itemfield[x_mouse_pos][y_mouse_pos][2] = item_color;
    }
}
danpost danpost

2014/3/14

#
Sorry, I had to edit the above due to your 'mouse' references.
Pointifix Pointifix

2014/3/14

#
dont get any nullpointerexception any more but what have you done? i only saw this: item_rotation = (item_rotation+1)%4;
There are more replies on the next page.
1
2
3
4