Like a slot machine how can I see the movement of numbers and will stop 1 by 1..anyone know how to do it?

import java.awt.*;
import java.applet.*;
import java.lang.*;

public class Lotto1 extends Applet {
    public int n1,n2,n3,n4,n5,n6;
	void button1_Clicked(Event event) {
	    n1=(int)(53*Math.random()+1);
        n2=(int)(53*Math.random()+1);
        n3=(int)(53*Math.random()+1);
        n4=(int)(53*Math.random()+1);
        n5=(int)(53*Math.random()+1);
        n6=(int)(53*Math.random()+1);
		textField1.setText(Integer.toString(n1));
 		textField2.setText(Integer.toString(n2));
 		textField3.setText(Integer.toString(n3));
 		textField4.setText(Integer.toString(n4));
 		textField5.setText(Integer.toString(n5));
 		textField6.setText(Integer.toString(n6));
	}

	void button2_Clicked(Event event) {
		// to do: place event handler code here.
		destroy();
	}


	public void init() {
		super.init();

		//{{INIT_CONTROLS
		setLayout(null);
		addNotify();
		resize(528,266);
		setBackground(new Color(16756655));
		button1 = new java.awt.Button("Lotto");
		button1.reshape(48,132,84,40);
		add(button1);
		textField1 = new java.awt.TextField();
		textField1.reshape(24,24,61,49);
		add(textField1);
		textField2 = new java.awt.TextField();
		textField2.reshape(108,24,58,49);
		add(textField2);
		textField3 = new java.awt.TextField();
		textField3.reshape(192,24,58,49);
		add(textField3);
		textField4 = new java.awt.TextField();
		textField4.reshape(276,24,58,49);
		add(textField4);
		textField5 = new java.awt.TextField();
		textField5.reshape(360,24,58,49);
		add(textField5);
		textField6 = new java.awt.TextField();
		textField6.reshape(444,24,58,49);
		add(textField6);
		nervousText1 = new symantec.itools.multimedia.NervousText();
		nervousText1.reshape(192,132,264,60);
		nervousText1.setFont(new Font("TimesRoman", Font.BOLD, 20));
		add(nervousText1);
		nervousText1.setText("Lotto Picker");
		//}}
	}

	public boolean handleEvent(Event event) {
		if (event.target == button1 && event.id == Event.ACTION_EVENT) {
			button1_Clicked(event);
		}
		return super.handleEvent(event);
	}

	//{{DECLARE_CONTROLS
	java.awt.Button button1;
	java.awt.TextField textField1;
	java.awt.TextField textField2;
	java.awt.TextField textField3;
	java.awt.TextField textField4;
	java.awt.TextField textField5;
	java.awt.TextField textField6;
	symantec.itools.multimedia.NervousText nervousText1;
	//}}
}

Recommended Answers

All 9 Replies

Do you mean that you want to animate the display of the numbers so they slide down out of sight and the next number slides down from the top?
If so, you need to override paintComponent for the text fields and draw the text at an x,y position where the y coordinate is increased every few millisecs by a jaxax.swing.Timer

Member Avatar for hfx642

Just an observation...
It is customary to;
1) define your components
2) create your components
3) handle your components
You've done this in reverse order.
It just makes it a little more difficult to debug,
especially when reading it on screen.
I generally read from the top of a page to the bottom.
NOT from bottom to top.

Do you mean that you want to animate the display of the numbers so they slide down out of sight and the next number slides down from the top?
If so, you need to override paintComponent for the text fields and draw the text at an x,y position where the y coordinate is increased every few millisecs by a jaxax.swing.Timer

yes that what i mean..thanks for the comment i will try it.

OK! Come back here if you get stuck.
ps I see you are using AWT components - these have been superceded by their Swing equivalents (JApplet, JTextField etc) since Java 1.2 (1998).

I have a question about how to random numbers that the number result have no same value..

Easiest way is to keep a list of the numbers you have already used. Check each new random number against the list and get a new one if it was a duplicate.

can you give samples?

here's the new code but the number would not stop one by one...

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JApplet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MP extends JApplet implements MouseListener, Runnable
{
    private int i,a,b,c,d,e;
    private Rectangle r = new Rectangle(80, 75, 150, 50);
    private String buttonlabel[] = {"Start", "Stop"};
    private int stringbuttonindex = 0;
    private int numberindexes[] = {0,0,0,0,0,0};
    private Thread t;
    private Random random = new Random();
    private boolean isStart = false;
    private int x = 350;

     public void init() {
        // TODO start asynchronous download of heavy resources
         setSize(350,250);
         addMouseListener(this);

    }

     public void paint(Graphics g) {
        super.paint(g);
        g.drawString("Machine Problem: Number Generator", 0, 10);

        g.drawRect(10, 30, 50, 50);
        g.drawRect(60, 30, 50, 50);
        g.drawRect(110, 30, 50, 50);
        g.drawRect(160, 30, 50, 50);
        g.drawRect(210, 30, 50, 50);
        g.drawRect(260, 30, 50, 50);

         g.setFont(new Font(Font.DIALOG_INPUT, Font.PLAIN, 50));

        g.drawString(numberindexes[0]+"", 25, 65);
        g.drawString(numberindexes[1]+"", 75, 65);
        g.drawString(numberindexes[2]+"", 125, 65);
        g.drawString(numberindexes[3]+"", 175, 65);
        g.drawString(numberindexes[4]+"", 225, 65);
        g.drawString(numberindexes[5]+"", 275, 65);

        g.fillRect(10, 85, 160, 50);
        g.setColor(Color.WHITE);
        g.drawString(buttonlabel[stringbuttonindex], 20, 120);

        g.setFont(new Font(Font.DIALOG_INPUT, Font.PLAIN, 20));
        g.setColor(Color.BLACK);
        g.drawString("Congratulations to the winner...", x, 150);
    }

       public void mouseClicked(MouseEvent e) {

    }
        public void mousePressed(MouseEvent e) {

    }

        public void mouseReleased(MouseEvent e) {
        Rectangle mousepoint = new Rectangle(e.getX(), e.getY(), 5, 5);

        if(mousepoint.intersects(r)){
            if(stringbuttonindex==0){
                stringbuttonindex = 1;
                t = new Thread(this);
                t.start();
                isStart = true;
            }else if(stringbuttonindex==1){
                stringbuttonindex = 0;
                isStart = false;
            }
            repaint(80, 75, 150, 50);
        }
    }
        public void mouseEntered(MouseEvent e) {

    }

        public void mouseExited(MouseEvent e) {

    }

         
    public void run() {
        while(true){
            try {
                Thread.sleep(50);
            } catch (InterruptedException ex) {
                Logger.getLogger(MP.class.getName()).log(Level.SEVERE, null, ex);
            }
            
Random rnd = new Random(System.currentTimeMillis());
            for(int x=0;x<6;x++){
                int i = Math.abs(rnd.nextInt() % 10);
                while(i==0)
                    i = Math.abs(rnd.nextInt() % 10);
                numberindexes[0] = i;
                
                int a = Math.abs(rnd.nextInt() % 10);
                while((a==0)||(a== numberindexes[0]))
                a = Math.abs(rnd.nextInt() % 10);
                numberindexes[1] = a;
                
                int b = Math.abs(rnd.nextInt() % 10);
                while((b==0)||(b== numberindexes[0])||(b==numberindexes[1]))
                b = Math.abs(rnd.nextInt() % 10);
                numberindexes[2] = b;
                
                int c = Math.abs(rnd.nextInt() % 10);
                while((c==0)||(c== numberindexes[0])||(c==numberindexes[1])||(c==numberindexes[2]))
                c = Math.abs(rnd.nextInt() % 10);
                numberindexes[3] = c;
                
                int d = Math.abs(rnd.nextInt() % 10);
                while( (d==0)||(d== numberindexes[0])||(d==numberindexes[1])||(d==numberindexes[2])||(d==numberindexes[3]))
                d = Math.abs(rnd.nextInt() % 10);
                numberindexes[4] = d;
                
                int e = Math.abs(rnd.nextInt() % 10);
                while((e==0)||(e== numberindexes[0])||(e==numberindexes[1])||(e==numberindexes[2])||(e==numberindexes[3])||(e==numberindexes[4]))
                e = Math.abs(rnd.nextInt() % 10);
                numberindexes[5] = e;
            }
            repaint();

            if(isStart==false){
                break;
            }
        }
        while(true){
            x--;

            if(x==-350){
                x = 350;
            }

            try {
                Thread.sleep(5);
            } catch (InterruptedException ex) {
                Logger.getLogger(MP.class.getName()).log(Level.SEVERE, null, ex);
            }
            repaint(0, 125, getWidth(), 150);

            if(isStart==true){
                x = 350;
                repaint();
                break;
            }
        }
    }
}
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.