0

I'm developing an memory card game in java. I need a delay after SelectedTile.hideFace();

Control.java

package control;

public class Control extends JFrame {

    private static final long serialVersionUID = 1L;
    public static Control CurrentWindow = null;
    private ImageIcon background1,background2,background3,background4,background5,background6,background7,background8; 
    private final String title ="Remembory";

    private Tile SelectedTile = null;
    private int points = 0;
    private int fails = 0;
    private int failsleft = 5;
    private JLabel score = new JLabel("Score:0");
    private JLabel pogingen = new JLabel("Pogingen:5");

    public Control() {

        setSize(334,464);
        setTitle(title);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBackground(Color.BLACK);
        setUpGame();
        add(score,BorderLayout.SOUTH);
        score.setForeground(new Color(1,21,118));
        score.setFont(score.getFont().deriveFont(18.0f));
        add(pogingen,BorderLayout.SOUTH);
        pogingen.setForeground(new Color(1,21,118));
        pogingen.setFont(pogingen.getFont().deriveFont(18.0f));
        setVisible(true);

    }

    public void setUpGame()
    {
        background1 = new ImageIcon("src//card.png");
        background2 =  new ImageIcon("src//card1.png");
        background3 =  new ImageIcon("src//card2.png");
        background4 =  new ImageIcon("src//card3.png");
        background5 =  new ImageIcon("src//card4.png");
        background6 =  new ImageIcon("src//card5.png");
        background7 =  new ImageIcon("src//card6.png");
        background8 =  new ImageIcon("src//card7.png");

        getContentPane().setLayout(new FlowLayout());

        getContentPane().add(new Tile(background1));
        getContentPane().add(new Tile(background1));
        getContentPane().add(new Tile(background2));
        getContentPane().add(new Tile(background2));
        getContentPane().add(new Tile(background3));
        getContentPane().add(new Tile(background3));
        getContentPane().add(new Tile(background4));
        getContentPane().add(new Tile(background4));
        getContentPane().add(new Tile(background5));
        getContentPane().add(new Tile(background5));
        getContentPane().add(new Tile(background6));
        getContentPane().add(new Tile(background6));
        getContentPane().add(new Tile(background7));
        getContentPane().add(new Tile(background7));
        getContentPane().add(new Tile(background8));
        getContentPane().add(new Tile(background8));

        }

    private void Addfails() {
        fails++;
        failsleft--;
        pogingen.setText("Pogingen:" + failsleft);
        repaint();
        System.out.println( "Poging " + fails + " u heeft nog " + failsleft + " poging over" );
        }



    private void AddPoint() {
        points++;
        score.setText("Score:" + points);
        repaint();
        System.out.println(" + " + points + "Punten");
    }


    public void TileClicked (Tile tile){
        if (SelectedTile == null) {
            tile.showFace();
            SelectedTile = tile;
            return;
        }
        if (SelectedTile == tile) {
            tile.hideFace();
            SelectedTile = null;
            return;
        }

        if (points < 8){
            tile.showFace();    
        }

        if (fails > 3){
            JOptionPane.showMessageDialog(null, "Helaas u hebt geen pogingen meer");
            System.exit(0);
        }

        if (points == 7){
            JOptionPane.showMessageDialog(null, "Gefeliciteerd! Jou score is : " + points);
            System.exit(0);
        }

        if (SelectedTile.getFaceColor().equals(tile.getFaceColor())) {
            AddPoint();
            SelectedTile = null;
            return;

        }


        SelectedTile.hideFace();
        //delay here
        tile.hideFace();
        Addfails();
        System.out.println("Probeer het nogmaals");
        SelectedTile = null;

    }







    public static void main(String[] args){
        CurrentWindow = new Control();
    }
}

Tile.java

package Tiles;

public class Tile extends JLabel implements MouseListener{

    private static final long serialVersionUID = 1L;

    private ImageIcon faceColor = new ImageIcon("src//background.png"); // standaard image (back)
    private final static Dimension size = new Dimension(71,96);

    public Tile(ImageIcon kleur)
    {
        setFaceColor(kleur);
        setMinimumSize(size);
        setMaximumSize(size);
        setPreferredSize(size);
        setOpaque(true);
        setIcon(new ImageIcon("src//background.png"));
        addMouseListener(this);
    }


    public void showFace()
    {
        //setBackground(faceColor);
        setIcon(faceColor);
    }
    public void hideFace()
    {
        setIcon(new ImageIcon("src//background.png"));
        //setBackground(new Color(213,86,31));
    }

    protected void setFaceColor(ImageIcon c)
    {
        this.faceColor = c;
    }

    public ImageIcon getFaceColor()
    {
        return this.faceColor;

    }


    public void mouseClicked(MouseEvent arg0) {
        control.Control.CurrentWindow.TileClicked(this);
    }


    public void mouseEntered(MouseEvent arg0) {

    }


    public void mouseExited(MouseEvent arg0) {

    }


    public void mousePressed(MouseEvent arg0) {

    }


    public void mouseReleased(MouseEvent arg0) {


    }
}
bobbel
  • 3,327
  • 2
  • 26
  • 43
Rene
  • 13,299
  • 5
  • 19
  • 28
  • 1
    When talking about Java, we usually say *method*. – Maroun Jan 29 '14 at 13:55
  • 1
    possible duplicate of [How to make a pause before continuing method](http://stackoverflow.com/questions/13078548/how-to-make-a-pause-before-continuing-method) – Taegost Jan 29 '14 at 13:57
  • @david99world That's a bad idea. This will block EDT. – Maroun Jan 29 '14 at 13:58
  • @ᴍarounᴍaroun: the OP didn't mention yet, what he really want's to do in his delay! So why not blocking the EDT? ;) – bobbel Jan 29 '14 at 14:07
  • Just as a note, it's usually considered bad form in Java to name variables or fields starting with capital letters. Capital letters usually indicate class names. This won't affect the functioning of your code - JVM don't care - but when you show your code to others, it affects the readability. – dcsohl Jan 29 '14 at 14:26

2 Answers2

0

What do mean with "delay"? Normally, a Thread.sleep(milliseconds) would do the job, but the program will completely stop at this moment!

Leigh
  • 28,765
  • 10
  • 55
  • 103
Bobo
  • 575
  • 3
  • 4
  • It actually is an answer @ᴍarounᴍaroun.. I would just note that it stops the active Thread, not the complete program. Can you specify your question please? – Dropout Jan 29 '14 at 14:00
  • Basically i want to have a slight pause when i select the second card the second card will show for some time. and when the pause expires both cards turn back. (hideface) – Rene Jan 29 '14 at 14:09
0

If you want to have an action with a delay (like hiding your cards after 500 milliseconds) you can schedule it like this:

import java.util.Timer;
import java.util.TimerTask;

// ...
SelectedTile.hideFace();

new Timer().schedule(new TimerTask() {
    public void run() {
        tile.hideFace();
    }
}, 500); // hide face after 500 ms
// ...
bobbel
  • 3,327
  • 2
  • 26
  • 43