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

2018/4/28

Image duplication

Lavenger Lavenger

2018/4/28

#
My code has no error but for some reason, when The main Image is being split into 16 equally sized pictures, the smaller images are duplicating. Example: MainImage Splits into equal 16 puzzle pieces> 1st Image is duplicated 3 times, 5th Image is duplicated 2 times 13th Image is duplicated 4 times, the other 7 Images are not duplicates. I have been trying to find out what's the cause but my efforts were in vain. Please help me inspect my code and tell me what's wrong with it... This is my world class code(Generates the 2 4x4 Grids(One of them is a grid that the player is supposed to place the puzzle piece in and the other is where the 16 smaller images, split from the main image, supposed to appear)):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import greenfoot.*;
import java.util.Random;
public class Puzzle extends World
{
    public BackFront[] backfront=new BackFront[16];
    public FrontImage[] frontimage=new FrontImage[16];
    public Puzzle()
    {
       super(600, 800, 1,false);
       int[] sorting=generateSort(16);
       for(int i=0;i<4;i++)
       {
           for(int j=0;j<4;j++)
           {
               backfront[i*4+j]=new BackFront(i,j);
               addObject(backfront[i*4+j],(int)(0.5*getWidth()+(i-1.5)*BackFront.Width),(int)(0.5*getHeight()+(j-4)*BackFront.Width));
               int id=sorting[i*4+j];
               int io=id/4;
               int jo=id%4;
               frontimage[id]=new FrontImage(io,jo);          
               addObject(frontimage[id],(int)(0.5*getWidth()+(i-1.5)*BackFront.Width),(int)(0.5*getHeight()+(j+1)*BackFront.Width));
               frontimage[id].positiono();
           }
       }
    }
    private int[] generateSort(int num)
    {
        int[] result=new int[num];
        Random ran=new Random();
        for(int i=0;i<num;i++)
        {
            int a=ran.nextInt(num);
            boolean Umaru=false;
            for(int j=0;j<i;j++)
            {
                if(a==result[j])
                {
                    Umaru=true;
                    break;
                }
                if(!Umaru)
                {
                    result[i]=a;
                }
                else i--;
            }
        }
        return result;
    }
    public void makeFront (int i, int j)
    {
        int posx=frontimage[i*4+j].getX();
        int posy=frontimage[i*4+j].getY();
        removeObject(frontimage[i*4+j]);
        addObject(frontimage[i*4+j],posx,posy);
    }
}
This is the class code that generates the image of the grid that the player is supposed to place the puzzle piece in:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import greenfoot.*;
public class BackFront extends Actor
{
    protected int idi=0,idj=0;
    public static int Width=75;
    public FrontImage frontimage;
    private Color backcolor=new Color(0,0,0,0);
    private int gridSpot=0;
    public BackFront()
    {
         
    }
    public BackFront(int i, int j)
    {
        idi=i;idj=j;
    }
    public void addedToWorld(World Later)
    {
        setGrid();
    }
    public void setCyan()
    {
        gridSpot=10;
        backcolor=Color.CYAN;
        setGrid();
    }
    public void setGrid()
    {
        GreenfootImage image=new GreenfootImage(Width,Width);
        image.setColor(backcolor);
        image.fill();
        image.setColor(Color.CYAN);
        if(idi==0 && idj==0)
        {
           image.drawRect(0,0,image.getWidth()-1,image.getHeight()-1);
        }
        else
        {
            if(idi==0 && idj>0)
            {
                image.drawRect(0,-1,image.getWidth()-1,image.getHeight()-1);
            }
            else if(idj==0 && idi>0)
            {
                image.drawRect(-1,0,image.getWidth()-1,image.getHeight()-1);
            }
            else
            {
                image.drawRect(-1,-1,image.getWidth()-1,image.getHeight()-1);
            }
        }
        setImage(image);
    }
    public boolean checkPiece()
    {
        return(frontimage==null);
    }
    public void act()
    {
       if(gridSpot>0)gridSpot--;
       if(gridSpot==1)
       {
          backcolor=new Color(0,0,0,0);
          setGrid();
       }
    }   
}
This is the Class code that generates the 16 smaller Images(This class is a subclass of the Class mentioned above):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import greenfoot.*;
public class FrontImage extends BackFront
{
    private static GreenfootImage mainimage=new GreenfootImage("Puzzle.png");
    private boolean drag=false;
    private int rx=0,ry=0;
    public BackFront backfront;
    private int posox,posoy;
    public FrontImage()
    {
         
    }
    public FrontImage(int i,int j)
    {
        super (i,j);
    }
    public void positiono()
    {       
        posox=getX();posoy=getY();
    }
    public void setGrid()
    {
        GreenfootImage image=new GreenfootImage(Width,Width);
        image.setColor(Color.WHITE);
        image.fill();
        image.drawImage(mainimage,-idi*Width,-idj*Width);
        setImage(image);
    }
    public void makeFront()
    {
        ((Puzzle)getWorld()).makeFront(idi,idj);
    }
    public void checkBackFront()
    {
        Actor actor=getOneObjectAtOffset(0,0,BackFront.class);
        if(actor!=null)
        {
            ((BackFront)actor).setCyan();
        }
    }
    public void Bound()
    {
        Actor actor=getOneObjectAtOffset(0,0,BackFront.class);
        if(actor!=null)
        {
            BackFront back=((BackFront)actor);
            if(back.frontimage==null)
            {   
                backfront=back;
                backfront.frontimage=this;
                setLocation(backfront.getX(),backfront.getY());
            }
        }
    }
    public void act()
    {
        if(Greenfoot.mouseDragged(this))
        {
            MouseInfo mouse=Greenfoot.getMouseInfo();
            if(!drag)
            {
                if(backfront !=null)
                {
                   backfront.frontimage=null;
                   backfront=null;
                }
                makeFront();
                drag=true;
                rx=getX()-mouse.getX();
                ry=getY()-mouse.getY();
            }
            else
            {
                setLocation(mouse.getX()+rx,mouse.getY()+ry);
                checkBackFront();
            }
        }
         if(Greenfoot.mouseDragEnded(this))
        {
            Bound();
            drag=false;
        }
        if(!drag && backfront==null)
        {
            int xo=getX(),yo=getY();     
            int d=5;
            if(posox!=getX())
            {
                if(posox>xo)xo=(Math.abs(posox-xo)>d)?xo+=d:posox;
                else xo=(Math.abs(posox-xo)>d)?xo-=d:posox;
            }
            if(posoy!=getY())
            {
                if(posoy>yo)yo=(Math.abs(posoy-yo)>d)?yo+=d:posoy;
                else yo=(Math.abs(posoy-yo)>d)?yo-=d:posoy;
            }
            setLocation(xo,yo);
        }
    }   
}
danpost danpost

2018/4/28

#
Please explain why you have the FrontImage class extending the BackFront class. Give a full description of what objects of each class are and how they are going to be used.
You need to login to post a reply.