Friday, March 4, 2016

Jigsaw Puzzles From Images


This program utilizes mouse events for repositioning clipped images. The class JLayeredPane is used here so that the image which is being moved will always be on top. Everytime the user relocates an image, the program will also determine whether all the clipped images have been placed correctly. The process is performed in the mouseReleased method.

  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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.util.*;

public class JigsawPuzzle extends JFrame{
    private JPanel pinfo=null;
    private JLabel linfo=null;
    private JLayeredPane layeredPane=null;
    private JPanel pbutton=null;
    private JButton bload=null;
    private JButton bstart=null;
    private BufferedImage bufimage=null;
    private BufferedImage[] bufimage2=null;
    private String imagefile=null;
    private boolean loaded=false;
    private boolean started=false;
    private JLabel[] lbox=null;
    private int bufwidth;
    private int bufheight;
    ArrayList<Integer> lstposition=null;
    private int boxselected=-1;
    private Point ptemp=null;
    
    public static void main(String[] args){
        JigsawPuzzle ap=new JigsawPuzzle();
    }
    public JigsawPuzzle(){
        super("Jigsaw Puzzle");
        setSize(700,700);
        
        pinfo=new JPanel();
        pinfo.setPreferredSize(new Dimension(700,50));
        linfo=new JLabel("");
        pinfo.add(linfo);
        layeredPane=new DrawPanel();
        layeredPane.setLayer(this, 0);
        layeredPane.setPreferredSize(new Dimension(700,600));
        layeredPane.addMouseListener(new MouseHandler());
        layeredPane.addMouseMotionListener(new MouseHandler());
        pbutton=new JPanel();
        pbutton.setPreferredSize(new Dimension(700,50));
        bload=new JButton("Load");
        bload.addActionListener(new ButtonHandler());
        bstart=new JButton("Start");
        bstart.addActionListener(new ButtonHandler());
        
        setLayout(new BorderLayout());
        add(BorderLayout.NORTH,pinfo);
        add(BorderLayout.CENTER,layeredPane);
        pbutton.add(bload);
        pbutton.add(bstart);
        add(BorderLayout.SOUTH,pbutton);
        
        lbox=new JLabel[16];
        for(int i=0;i<16;i++){
            lbox[i]=new JLabel();
        }
        
        bufimage2=new BufferedImage[16];
        
        lstposition=new ArrayList<Integer>(16);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        show();
    }
    
    class DrawPanel extends JLayeredPane{
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Graphics2D g2=(Graphics2D)g;
            
            if(loaded==true){
                try{
                    bufimage=ImageIO.read(new File(imagefile));
                }catch(Exception ex){
                }
                
                bufwidth=bufimage.getWidth();
                bufheight=bufimage.getHeight();
                layeredPane.setPreferredSize(new Dimension(bufwidth,bufheight));
                
                int counter=0;
                try{
                    for(int i=0;i<4;i++){
                        for(int j=0;j<4;j++){
                            bufimage2[counter]=bufimage.getSubimage((bufwidth/4)*j,(bufheight/4)*i,bufwidth/4,bufheight/4);
                            counter++;
                        }
                    }
                }catch(Exception e){
                }
                
                counter=0;
                for(int i=0;i<4;i++){
                    for(int j=0;j<4;j++){
                        layeredPane.add(lbox[counter],1);
                        lbox[counter].setBounds((bufwidth/4)*j,(bufheight/4)*i,bufwidth/4,bufheight/4);
                        counter++;
                    }
                }                
                
                for(int i=0;i<16;i++){
                    ImageIcon icon=new ImageIcon(bufimage2[i]);
                    lbox[i].setIcon(icon);
                }                
                
                loaded=false;
            }
            
            if(started==true){
                lstposition.clear();
                for(int i=0;i<16;i++){
                    lstposition.add(i,i);
                }
                    
                Random rand=new Random();
                for(int i=0;i<16;i++){
                    int temp=rand.nextInt(16);
                    int temp2=lstposition.get(i);
                    lstposition.set(i,lstposition.get(temp));
                    lstposition.set(temp,temp2);
                }
                    
                int counter=0;
                for(int i=0;i<4;i++){
                    for(int j=0;j<4;j++){
                        lbox[lstposition.get(counter)].setLocation((bufwidth/4)*j,(bufheight/4)*i);
                        counter++;
                    }
                }
                
                linfo.setText("");
                started=false;
            }
        }        
    }
    
    class MouseHandler implements MouseMotionListener, MouseListener{
        public void mouseMoved(MouseEvent e){
            
        }
        public void mouseDragged(MouseEvent e){
            if(boxselected!=-1){
                layeredPane.moveToFront(lbox[boxselected]);
                lbox[boxselected].setLocation(e.getX(),e.getY());
            }
        }
        public void mouseEntered(MouseEvent e){
            
        }
        public void mouseExited(MouseEvent e){
            
        }
        public void mouseReleased(MouseEvent e){
            for(int i=0;i<lbox.length;i++){
              if(i==boxselected){
                  lbox[boxselected].setLocation(ptemp);
                  continue;
              }
              if(e.getX()>lbox[i].getX()&&e.getX()<lbox[i].getX()+lbox[i].getWidth()
                    &&e.getY()>lbox[i].getY()&&e.getY()<lbox[i].getY()+lbox[i].getHeight()){
                     lbox[boxselected].setLocation(lbox[i].getX(),lbox[i].getY());
                     lbox[i].setLocation(ptemp);
                     break;
              }
            }
            boolean win=true;
            int counter=0;
            for(int i=0;i<4;i++){
                for(int j=0;j<4;j++){
                    Point p=lbox[counter].getLocation();
                    if(!(p.x==(bufwidth/4)*j&&p.y==(bufheight/4)*i)){
                        win=false;
                        break;
                    }
                    counter++;
                }
            }
            if(win){
                linfo.setText("You win!");
            }
            boxselected=-1;
        }
        public void mousePressed(MouseEvent e){
             for(int i=0;i<lbox.length;i++){
                 if(e.getX()>lbox[i].getX()&&e.getX()<lbox[i].getX()+lbox[i].getWidth()
                    &&e.getY()>lbox[i].getY()&&e.getY()<lbox[i].getY()+lbox[i].getHeight()){
                     boxselected=i;
                     ptemp=lbox[boxselected].getLocation();
                     break;
                 }
            }
        }
        public void mouseClicked(MouseEvent e){

        }
    }    
    
    class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getActionCommand()=="Load"){
                FileDialog fd=new FileDialog(JigsawPuzzle.this,"Open Image File",FileDialog.LOAD);
                fd.setDirectory("C:\\");
                fd.setVisible(true);
                imagefile=fd.getDirectory()+fd.getFile();
                loaded=true;
                layeredPane.repaint();
            }
            if(e.getActionCommand()=="Start"){
                started=true;
                layeredPane.repaint();
            }
        }
    }
}


Image Source: www.pixabay.com

No comments:

Post a Comment