Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: ganaschka (posted 2012-08-05 11:18:36, viewed 164 times)

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  
package puzzle;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Vector;
/**
 *
 * @author Ganaa
 */
public class Puzzle extends JFrame implements Runnable{

    private String iconNames[] = {"D:/images/1.png", "D:/images/2.png", "D:/images/3.png", "D:/images/4.png"
                                    , "D:/images/5.png", "D:/images/6.png"};
    private Vector images = new Vector();
    private JPanel centerPanel;
    private JLabel topLbl;
    private Vector allImages = new Vector();
    private Vector tovchVec;
    Button lastTovch = null;
    private Thread th;
    private int min=0, sec=0;
    boolean game = false;
    boolean running = false;
    JMenuBar menuBar;//цэс гаргах хэсэг
    JMenu menu;
    private JTextField score;// оноо гаргах хэсэг
    private int onoo = 0;
   

    public Puzzle(){
        super("Fruit Puzzle");
        Container con = getContentPane();
        con.setLayout(new BorderLayout());

        tovchVec = new Vector();

        centerPanel = new JPanel();
        topLbl = new JLabel("0:0", SwingUtilities.CENTER);
        topLbl.setBackground(new Color(255, 225, 169));
        topLbl.setOpaque(true);

        for(int i=0; i<iconNames.length; i++){
            images.add(new ImageIcon(iconNames[i]));
        }

        for(int i=0; i<images.size(); i++){
            allImages.add(images.get(i));
        }
        for(int i=0; i<images.size(); i++){
            allImages.add(images.get(i));
        }

        MyHandler handler = new MyHandler();

        for(int i=0; i<iconNames.length*2; i++){
            ImageIcon randIcon = randomIcon();
            final Button t = new Button(randIcon);
//            t.addActionListener(handler);
            t.addMouseListener(handler);
            tovchVec.add(t);
            centerPanel.add(t);            
            // menu bar
            menuBar = new JMenuBar();
            menu = new JMenu("Restart");
            menu.setMnemonic(1);
            menuBar.add(menu);
            menu.addActionListener(new ActionListener(){// I was trying on this but doesnt work :( yeah I need init()
            public void actionPerformed(ActionEvent e){
                            if(e.getSource() == menu){
                             
                                allImages.clear();
                                 for(int i=0; i<images.size(); i++){
                                    allImages.add(images.get(i));
                                }
                                   for(int i=0; i<images.size(); i++){
                                     allImages.add(images.get(i));
                             }
                                 for(int i=0; i<iconNames.length*2; i++){
                                     ImageIcon randIcon = randomIcon();
                                        Button b = (Button)tovchVec.get(i);
                                        b.setZurag(randIcon);
                                        b.setEnabled(true);
                                }
                            }           
                        }});
        
        setJMenuBar(menuBar);//JFrame dr nemj bn
      
        //score garah heseg
        score = new JTextField();
        score.setEditable(false);
        score.setFont(new Font("Serif",Font.BOLD,12));
        score.setBackground(new Color(255, 225, 169));
        con.add(score,BorderLayout.SOUTH);


        }

        con.add(centerPanel, BorderLayout.CENTER);
        con.add(topLbl, BorderLayout.NORTH);

        setLocation(500,50);
        setSize(530,635);
        setResizable(false);
        setVisible(true);

        th = new Thread(this);
        th.start();
        running = true;
    }
    // reset()
    

    public ImageIcon randomIcon(){

        int r = (int)(Math.random() * allImages.size());
        ImageIcon icon = (ImageIcon)allImages.get(r);

        allImages.remove(r);

        return icon;
    }

    public static void main(String[] args) {
        Puzzle game = new Puzzle();
        game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    // Thread run
    public void run() {
        while(running){
            try {
                Thread.sleep(1000);
                sec++;
                if(sec>59)
                {
                    min++;
                    sec = 0;
                }
                topLbl.setText(min+":"+sec);

                for(int i=0; i<tovchVec.size(); i++)
                {
                    Button t = (Button)tovchVec.get(i);
                    if(t.isEnabled()){
                        game = true;
                        break;
                    }

                }
                if(game == false){
                    running = false;
                    JOptionPane.showMessageDialog(Puzzle.this, "Баяр хүргье. Та "+min+" минут "+sec+" секундэд амжилттай гүйцэтгэлээ.");
                    th.stop();
                }
                game = false;
            } catch (InterruptedException ex) {
            }
        }
    }

    class MyHandler implements MouseListener{

        public void mouseClicked(MouseEvent e) {

            if( e.getClickCount() == 1)
            {
                for (int i = 0; i < tovchVec.size(); i++) {
                Button firstTovch = (Button)tovchVec.get(i);
                if(e.getSource() == firstTovch){
                    firstTovch.setNuuh(false);
                    if(lastTovch != null){
                        if(firstTovch.getHashCode() == lastTovch.getHashCode() && firstTovch.hashCode() != lastTovch.hashCode()){
                            firstTovch.removeMouseListener(this);
                            lastTovch.removeMouseListener(this);
                            ++onoo;
                            score.setText("\t\t   "+onoo+" "+"зураг амжилттай оллоо");
                            if(onoo ==6){
                                score.setText("\t\tбүх ижил зурагнуудыг амжилттай оллоо !");
                            }
                            firstTovch.setNuuh(false);
                            lastTovch.setNuuh(false);
                            firstTovch.setBackground(new Color(253, 195, 115));
                            lastTovch.setBackground(new Color(253, 195, 115));
                            firstTovch.setEnabled(false);
                            lastTovch.setEnabled(false);
                        }
                    }
                    lastTovch = firstTovch;

                }
                }
            }
            else return;
        }

        public void mousePressed(MouseEvent e) {
//            throw new UnsupportedOperationException("Not supported yet.");
        }

        public void mouseReleased(MouseEvent e) {
//            throw new UnsupportedOperationException("Not supported yet.");
        }

        public void mouseEntered(MouseEvent e) {
//            throw new UnsupportedOperationException("Not supported yet.");
        }

        public void mouseExited(MouseEvent e) {
             for (int i = 0; i < tovchVec.size(); i++) {
                Button tovch = (Button)tovchVec.get(i);
                if(e.getSource() == tovch){
                    tovch.setNuuh(true);
                 }
             }
        }
    }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 
xxMrPHDxx (17 views)
2017-11-21 16:21:00

xxMrPHDxx (11 views)
2017-11-21 16:14:31

xxMrPHDxx (14 views)
2017-11-21 16:10:57

Ecumene (113 views)
2017-09-30 02:57:34

theagentd (148 views)
2017-09-26 18:23:31

cybrmynd (247 views)
2017-08-02 12:28:51

cybrmynd (244 views)
2017-08-02 12:19:43

cybrmynd (244 views)
2017-08-02 12:18:09

Sralse (258 views)
2017-07-25 17:13:48

Archive (877 views)
2017-04-27 17:45:51
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!