The idea of program is to change the images between them.
Images here is stored in Icon Labels. If i click on image(label), i need to memorize this image, and change with the second image. Like a puzzle game.
I can to return the last two clicked image, but when i try to make changes between them, he dosn't work.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Fereastra extends Frame implements MouseListener{
	
	public Panel p = new Panel();	
	int countClick=0;//Count the click numbers
		
	 	public String value1 = new String(); // first clicked image
		public String value2 = new String(); //second clicked image
		public String AUX = new String();   // the variable who make changes between images
		public String pathValue = new String(); //variable who give the path of image	
			
		
        // Labels, here is stored images like icons.
	public JLabel l; 
	public JLabel l2;	
	public JLabel l3;	
        
       //Path to images
	public String path1=new String("hel1.jpg");
	public String path2=new String("hel2.jpg");
	public String path3=new String("eams.jpg");	
	
	public Fereastra(){
		super("puzzle");
		setSize(300,300);
		
		myRepaint(); //draw Images		
		p.add(l);
		p.add(l2);
		p.add(l3);
		add(p);		
		l.addMouseListener(this);
		l2.addMouseListener(this);
		l3.addMouseListener(this);		
		pack();			
	} 				
	public void myRepaint(){	//draw images			   		 
	 l = new JLabel(new ImageIcon(path1));
	 l2= new JLabel(new ImageIcon(path2));
	 l3= new JLabel(new ImageIcon(path3));	  	  
	}
	public void mouseClicked(MouseEvent e) {		
		countClick+=1;	//if cl
		
		if(e.getComponent()==l){
			pathValue=path1;	
		}
		if(e.getComponent()==l2){
			pathValue=path2;
		}
		if(e.getComponent()==l3){
			pathValue=path3;
		}	
            //the last two images between i need to do changes is stored in "value1" & "value2"
	    if(countClick==1)
	    {
	    	value1=pathValue;	    System.out.println(value1);
	    }
	    if(countClick==2){
	    	value2=pathValue;       System.out.println(value2); 
	    	
           //Here i try to make the changes
	    AUX=value1;
	    value1=value2;
	    value2=AUX;

	    repaint();
	    }
	    
	    countClick=0; //reset count   	    	 
    }
    public void mouseEntered(MouseEvent e) {
    }
	public void mousePressed(MouseEvent e){
	}
	public void mouseReleased(MouseEvent e){
	}
    public void mouseExited(MouseEvent e){
    }    
}

public class ImageIconss{
    public static void main(String args[]){
    	new Fereastra().show();
    } 
}

Please provide with any advice and help!

Recommended Answers

All 6 Replies

I know... my english is bad :(

first, you should use setVisble(true) instead of show()
second your objective is unclear, could you elaborate?

Sound like a game for small kids. Back home we call it "pexeso". You have set of cards with images in which each of the cards has it's pair. Mix the card, put them on the surface, face down (most commonly they will be put in some grid), and game can start. Player will choose 2 cards and turn them face up. If they have same image on then he will takes the cards, else he have to turn them face down again. Then is next player turn and so forward...
Is this the sort of the game you working on Motvel?

No, it's more ease than pexeso.
I try to develop a game like Puzzle. I have one big image slaced in little images and involved. You must to arrange the little images like the original.

For the moment, i decidet to make changes between 3 little images. The first clicked image must change with the second clicked image.
Please any reference if you know.

if you want to make a puzzle where the user has to drag pieces, you have a major flaw in your design, JLabels are not easy to move at runtime... they are meant to be placed during coding, and stay there. if you want the user to be able to drag an image you are going to need to use Graphics2D.

one of the problems you will almost assuredly encounter is that you will have trouble figuring out whether the image has been reproduced, and that the user won't be able to align the smaller images properly to get a perfectly interlaced puzzle, making the previous problem harder, i recommend some sort of grid to solve these problems

Thank you! I will introduce me more detailed with Graphics2D.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.