Thursday, November 17, 2011

MEMBUAT APLIKASI MULTIMEDIA SEDERHANA MENGGUNAKAN JAVA

Source Code
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class Puzzle implements WindowListener,ActionListener
{
    JFrame jf;
    JPanel p1,p2;
    JButton jb[],rb;
    JTextField tf;
    JLabel l;
    int eb;
    int ccnt;
    void initbl()
    {
        for (int i=0;i<9;i++)
            jb[i].setText("0");
        jb[8].setText("");
    }
    Puzzle()

    {
        eb=8;
        jf=new JFrame("Puzzle");
        p1=new JPanel();
        p2=new JPanel();
        jb=new JButton[9];
        rb=new JButton("Acak");
        for (int i=0;i<9;i++)
            jb[i]=new JButton("0");
        jb[8].setText("");
        tf=new JTextField(5);
        l=new JLabel("Click Count: ");
        p1.setLayout(new GridLayout(3, 3));
        //menampilakan button angka
        for (int i=0;i<9;i++)
            p1.add(jb[i]);
           
        p2.add(l);
        p2.add(tf);
        p2.add(rb);
        (jf.getContentPane()).setLayout(new GridLayout(2, 1));
        (jf.getContentPane()).add(p1);
        (jf.getContentPane()).add(p2);
        jf.setSize(200, 200);
        jf.setVisible(true);
        jf.addWindowListener(this);
        jf.setResizable(false);
        acak();
        rb.addActionListener(this);
        //pindah button angka
        for (int i=0;i<9;i++)
            jb[i].addActionListener(this);
       
        ccnt=0;
        dccnt();
    }
    void dccnt()
    {
        tf.setText(Integer.toString(ccnt));
    }
    void acak()
    {
        Random r=new Random();
        for (int i=0;i<8;)
        {
            int j=r.nextInt(9);
            if (jb[j].getText().equals("0"))
            {
                jb[j].setText(Integer.toString(i+1));
                i++;
            }
        }
    }
    public static void main(String a[])
    {
        new Puzzle();
    }

    public void windowOpened(WindowEvent we)
    {
       
    }

    public void windowClosing(WindowEvent we)
    {
        System.exit(0);
    }

    public void windowClosed(WindowEvent we)
    {
       
    }

    public void windowIconified(WindowEvent we)
    {
       
    }

    public void windowDeiconified(WindowEvent we)
    {
       
    }

    public void windowActivated(WindowEvent we)
    {
       
    }

    public void windowDeactivated(WindowEvent we)
    {
       
    }
    void chek()
    {
        boolean go=false;
        for (int i=0;i<8;i++)
        {
            if (jb[i].getText().equals(Integer.toString(i+1)))
            {
                go=true;
            }
            else
            {
                go=false;
                break;       
            }
        }
        if (go)
        {
            JOptionPane jp=new JOptionPane();
            jp.showMessageDialog(jf,"Finish dengan "+ ccnt +" klik.");
            ccnt=0;
            initbl();
            acak();
            dccnt();
        }
    }
    public void actionPerformed(ActionEvent ae)
    {
        if (ae.getSource()==rb)
        {
            JOptionPane a=new JOptionPane();
            a.showMessageDialog(rb,"WARNING,Anda Akan Random");
            initbl();
            acak();
            return;
        }
        for (int i=0;i<9;i++)
        {
            if (ae.getSource()==jb[i])
            {
                ccnt++;
                dccnt();
                if (eb!=i)
                {
                    jb[eb].setText(jb[i].getText());
                    eb=i;
                    jb[eb].setText("");
                    chek();
                }
            }
        }
    }
}
Print Out

0 comments:

Post a Comment