🔒 Closed Dice and lottery game

Status
Not open for further replies.

capusijake

Fanatic
Pinagawa po kami ng Dice game and 6 output Lottery game, so far okay naman siya and working.
Magpapatulong po sana ako kung pano or ano yung code, para pagpindot ng Roll or Draw button is continuous yung randomizing ng number, then kapag niclick ulit is mag stop siya, parang autobot sa mga dice game sa internet.

And pano rin po yung habang nag Roll or Draw siya is, nag output sa isang Textfield yung previous outputs ng Rolls and Draw,


Salamat po sa makakatulong
 
Saan gawa 'to sir? Example sa java, may Random object ka as a field dun sa main class mo. Then timer. Kapag press ng roll start mo yung timer task na laman 'to: call random(), output sa label, then force repaint.
 
Ganito po ung pinaka problem,
kapag po niclick ung roll button is mag randomize siya ng number every 3 second then kapag ni click naman po ulit yung button is mag stop siya sa pag random. and gusto ko po dagdagan na mag record yung mga previous rolls sa isang text area.
 
Gumamit ka ng Swing Timer. You do not have permission to view the full content of this post. Log in or register now.

class: Randomizer implements ActionListener
fields: You do not have permission to view the full content of this post. Log in or register now., You do not have permission to view the full content of this post. Log in or register now., You do not have permission to view the full content of this post. Log in or register now.
constructor:
create timer with speed = 3000 (milliseconds) and action listener = this, set initial delay = 3000
methods:
Code:
public  void run(JTextArea textArea);        // start timer
public  void stop();                         // stop timer
public  void actionPerformed(ActionEvent e); // calls roll()

private void roll();    // rand.nextInt(), output to text area
yung logic mo ganito: pag press ng button
Code:
randomizing = !randomizing;
if (randomizing) randomizer.run(textArea);
else randomizer.stop();
 
lagay mo yung program mo dito. di ko alam kung anong level na ng programming mo since di mo ine-elaborate yung part na "hindi ma-gets". i simply assumed na marunong ka ng swing basics since nakagawa ka ng working gui applications.

kailangan mo pa ba ng explanation kung pano yung logic or yung code lang para matapos 'to?
 
Code lang po, since gui po yung ginawa ko and some code are working,
ang kailangan ko nalan po is kung pano mag random ng numbers every 3 second and mag stop siya kapag niclick ulit yung button, sorry po hindi ko po ma upload yung picture, nasa laptop po kase and nagloloko yung internet.

Thanks po
 
try mo to.
mag declare ka ng instance variable mo na..
Code:
private final Random rd = new Random();
private Timer t;
tapos import mo para walang error at mag compile.
Code:
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
gawa ka ng method na pang start sa timer para mag generate ng random numbers.
Code:
public void start() {
        ActionListener time = (ActionEvent ae) -> {
            timer_label.setText(String.valueOf(rd.nextInt(6) + 1));    // kaya may +1 para 1 - 6 lang yung value.
        };
        t = new Timer(1, time);
        t.start();
}

tapos gawa ka ng dalawang button, start at stop, tapos gawan mo ng actionListener parehas.
Code:
private void start_buttonActionPerformed(java.awt.event.ActionEvent evt) {                                          
        stop_button.setEnabled(true);
        start();
        start_button.setEnabled(false);
    }                                        

    private void stop_buttonActionPerformed(java.awt.event.ActionEvent evt) {                                        
        stop_button.setEnabled(false);
        t.stop();
        start_button.setEnabled(true);
    }

Eto yung example project.

I hope this helps.
 

Attachments

BossKevs, pano yon? nagana naman siya, pero kapag niclick ko na yung start, then stop, hindi ko na ulit ma click yung start ulit, nag greyout.
 

Attachments

  • IMG_9860.webp
    IMG_9860.webp
    335.3 KB · Views: 28
tinanggal ko na yung isang stop na button tapos pinalitan ko ng code yung actionPerformed ng button na start.
Code:
private void start_buttonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        if(start_button.getText().equals("Start")){
            start();
            start_button.setText("Stop");
        }else{
            t.stop();
            start_button.setText("Start");
        }
    }
 
Status
Not open for further replies.

About this Thread

  • 16
    Replies
  • 893
    Views
  • 3
    Participants
Last reply from:
5JraBwYnxCYaSO1Lc4leQ

Trending Topics

Online now

Members online
417
Guests online
1,193
Total visitors
1,610

Forum statistics

Threads
2,274,113
Posts
28,953,731
Members
1,235,080
Latest member
arkzx
Back
Top