Toggle Theme Editor
Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Charcoal

Giúp mình với, bài tập quay số như máy xèng

Discussion in 'Xây dựng các ứng dụng Java khác, các vấn đề khác' started by duynguyendao87, 25/12/17.

  1. duynguyendao87

    duynguyendao87 New Member

    Hiện tại mình đang làm bài tập quay số như máy xèng.
    Có 3 cột số, hiện tại đang chạy cùng tốc độ. Giờ mình muốn mỗi cột chạy 1 tốc độ khác nahu thì phải làm sao vậy?
    Mình xin cảm ơn!
    Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    public class enshu81 {
        public static void main(String[] args) {
            enshu81Frame1 f = new enshu81Frame1("Enshu81 RSlot");
            f.setBackground(Color.white);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
        }
    }
    class enshu81Frame1 extends JFrame implements ActionListener,Runnable{
        int n[]={0,2,4},y[]={0,0,0},scount,s=0;
        Image img, back, offImage;
        Graphics offGraphics;
        static int snum = 7, ysize = 50;
        String base = "Slot";
        JPanel pnl;
        JLabel lbel;
        Thread thread;
        public enshu81Frame1(String title){
            super(title);
            for (int i=0; i<n.length; i++){
                y[i]=n[i]*ysize;
            }
            Container cnt = getContentPane();
            Toolkit tk = Toolkit.getDefaultToolkit();
            img = tk.getImage("slot.jpg");
            back = tk.getImage("bg.gif");
            cnt.setLayout(new BorderLayout());
            pnl = new JPanel();
            JButton button1 = new JButton("スタート");
            button1.addActionListener(this);
            pnl.add(button1);
            JButton button2 = new JButton("ストップ");
            button2.addActionListener(this);
            pnl.add(button2);
            setBackground(Color.white);
            setSize(300, 200);
            cnt.add("South",pnl);
            lbel = new JLabel("回るスロットです");
            cnt.add("North",lbel);
        }
        public void paint(Graphics g){
          int basex = (getSize().width - img.getWidth(this)*n.length) / 2;
          int basey = (getSize().height - ysize) / 2;
          for(int i=0; i<n.length;i++){
              if (y[i] < ysize*(snum-1) ) {
                  offGraphics.drawImage(img , i*img.getWidth(this), 5 - y[i], this);
              } else {
                  offGraphics.drawImage(img , i*img.getWidth(this), 5 - y[i], this);
                  offGraphics.drawImage(img , i*img.getWidth(this), 5 + ysize*snum - y[i], this);
              }
          }
          g.drawImage(offImage, basex, basey, this);
          pnl.repaint();
          lbel.repaint();
        }
        public void actionPerformed(ActionEvent e) {
            String cmd = e.getActionCommand();
            if ("スタート".equals(cmd)) {
                if (thread == null) {
                    thread = new Thread(this);
                    thread.start();
                    scount=0;
                    lbel.setText("1個目を止めてください");
                }
            } else {
                if (thread != null) {
                    if(scount<n.length-1){
                        while(y[scount]%ysize !=0){
                            y[scount]++;
                            repaint();
                        }
                        n[scount] = (y[scount]/ysize)%snum;
                        scount++;
                        lbel.setText((scount+1)+"個目を止めてください");
                    }else{
                        thread = null;
                        while(y[scount]%ysize !=0){
                            y[scount]++;
                            repaint();
                        }
                        n[scount] = (y[scount]/ysize)%snum;
                    }
    
                }
            }
        }
        public void run() {
            Thread thisThread = Thread.currentThread();
            while (thread == thisThread) {
                for(int i=scount; i<n.length;i++){
                    y[i]++;
                    if(y[i]>=snum*50){
                        y[i]=0;
                    }
                }
                try{
                    Thread.sleep(1);
                }catch(InterruptedException e){
                    break;
                }
                repaint();
            }
        }
        public void addNotify(){
            super.addNotify();
            offImage=createImage(50*n.length,60);
            offGraphics=offImage.getGraphics();
        }
    }
     
  2. Vũ Thiên

    Vũ Thiên Member

    Chọn vị trí phù hợp đặt cái này :
    Timer time;
    public void actionPerformed(ActionEvent e) {
    time = new Timer (50 ,this);
    }
    Trong đó 50 là thời gian chạy .Muốn lâu thì tăng lên
     
  3. D.A.N_3002

    D.A.N_3002 Member

    Mình không biết có giống ý bạn không nhưng mình cứ đưa bạn code máy xèng tham khảo nhé :)
    Code:
    package demo_small_project;
    
    import static java.lang.Thread.sleep;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JOptionPane;
    
    public class Xeng extends javax.swing.JFrame
    {
    // Var
        public static Integer Out_1 = 0;
        public static Integer Out_2 = 0;
        public static Integer Out_3 = 0;
        public static Boolean Check = true;
    //----------------------------------------------------------------------------// 
        public Xeng()
        {
            initComponents();
        }
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                       
        private void initComponents() {
    
            Output_1 = new javax.swing.JLabel();
            Output_2 = new javax.swing.JLabel();
            Output_3 = new javax.swing.JLabel();
            Text_1 = new javax.swing.JLabel();
            Button = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            Output_1.setFont(new java.awt.Font("Times New Roman", 1, 48)); // NOI18N
            Output_1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            Output_1.setText("0");
    
            Output_2.setFont(new java.awt.Font("Times New Roman", 1, 48)); // NOI18N
            Output_2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            Output_2.setText("0");
    
            Output_3.setFont(new java.awt.Font("Times New Roman", 1, 48)); // NOI18N
            Output_3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            Output_3.setText("0");
    
            Text_1.setFont(new java.awt.Font("Times New Roman", 2, 48)); // NOI18N
            Text_1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            Text_1.setText("Lucky Spin");
    
            Button.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
            Button.setText("Spin");
            Button.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    ButtonActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(106, 106, 106)
                            .addComponent(Output_1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(68, 68, 68)
                            .addComponent(Output_2, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(63, 63, 63)
                            .addComponent(Output_3, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(178, 178, 178)
                            .addComponent(Button, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(28, 28, 28)
                            .addComponent(Text_1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(59, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(27, 27, 27)
                    .addComponent(Text_1, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(33, 33, 33)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(Output_1, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(Output_3, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(Output_2, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(44, 44, 44)
                    .addComponent(Button, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(48, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                     
    
        private void ButtonActionPerformed(java.awt.event.ActionEvent evt) {                                     
            if(Button.getText().equals("Spin"))
            {
                Spin();
            }
            else
            {
                Stop();
            }
        }                                   
    
        public static void main(String args[])
        {
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(Xeng.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Xeng.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Xeng.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Xeng.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            java.awt.EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    new Xeng().setVisible(true);
                }
            });
        }
      
        public void Spin()
        {
            Check = true;
    //----------------------------------------------------------------------------//       
    // Thread for Output_1     
            new Thread()
            {
                @Override
                public void run()
                {
                    while(Check)
                    {
                        Out_1 = (int) Math.floor(Math.random() * 9);
                        Output_1.setText(Out_1 + "");                 
                        try {
                            sleep(20); // Chỉnh time ở đây !
                        } catch (InterruptedException ex) {
                            Logger.getLogger(Xeng.class.getName()).log(Level.SEVERE, null, ex);
                        }                 
                    }
                }
            }.start();
    //----------------------------------------------------------------------------//       
    // Thread for Output_2     
            new Thread()
            {
                @Override
                public void run()
                {
                    while(Check)
                    {
                        Out_2 = (int) Math.floor(Math.random() * 9);
                        Output_2.setText(Out_2 + "");                 
                        try {
                            sleep(1000); // Chỉnh time ở đây !
                        } catch (InterruptedException ex) {
                            Logger.getLogger(Xeng.class.getName()).log(Level.SEVERE, null, ex);
                        }                 
                    }
                }
            }.start();
    //----------------------------------------------------------------------------//       
    // Thread for Output_3     
            new Thread()
            {
                @Override
                public void run()
                {
                    while(Check)
                    {
                        Out_3 = (int) Math.floor(Math.random() * 9);
                        Output_3.setText(Out_3 + "");                 
                        try {
                            sleep(450); // Chỉnh time ở đây !
                        } catch (InterruptedException ex) {
                            Logger.getLogger(Xeng.class.getName()).log(Level.SEVERE, null, ex);
                        }                 
                    }
                }
            }.start();
    //----------------------------------------------------------------------------//       
            Button.setText("Stop");
        }
      
        public void Stop() 
        {
            Check = false;
            if(Out_1 == Out_2 && Out_2 == Out_3)
            {
                JOptionPane.showMessageDialog(null, "You are Lucky !!");
            }
            else
            {
                JOptionPane.showMessageDialog(null, "You are Unlucky !!");
            }
            Button.setText("Spin");
        }
        // Variables declaration - do not modify                   
        private javax.swing.JButton Button;
        private javax.swing.JLabel Output_1;
        private javax.swing.JLabel Output_2;
        private javax.swing.JLabel Output_3;
        private javax.swing.JLabel Text_1;
        // End of variables declaration                 
    }
    
     

Chia sẻ trang này

Loading...