Results 1 to 2 of 2
  1. #1
    cmplin is offline Member
    Join Date
    Apr 2016
    Posts
    1
    Rep Power
    0

    Default continue changing color of Jbutton

    Hi:
    I am designing a game which will show different color every 2 second. I am using setBackground color on JButton, and try to use a delay loop to accomplish it, but it seems to always display the last color. Can anyone help? thanks

    class ButtonListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
    int rint;
    int k = 0;
    if (e.getSource() == jb1) {
    long start = System.currentTimeMillis();
    int delay = 1000;
    s1 = jcbb.getSelectedIndex(); // based on selected value to define how many different random color to display
    System.out.println(s1);
    for(int i=0; i < s1+1; i++){
    rint = (int)(Math.random()*6);
    System.out.println(rint);
    jl12.setBackground(clist[rint]); // clist is a list of pre-defined Colors
    created[i] = rint;
    while (System.currentTimeMillis()- start < delay){
    // empty loop

    }
    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: continue changing color of Jbutton

    First, use [code][/code] to show us code. It will give you indenting and formatting.

    Second, use the Random class instead of Math.random(). It's a lot simpler to use.

    Third, use a Timer to do the changing. A never ending while loop will eat CPU cycles and destroy your application:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
    
    public class ColorButton extends JButton {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                JFrame frame = new JFrame();
    
                frame.getContentPane().setLayout(new FlowLayout());
                frame.getContentPane().add(new ColorButton("This is my button"));
    
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frame.setSize(400, 400);
                frame.setVisible(true);
            });
        }
    
        private int previous = -1;
    
        public ColorButton(String text) {
            super(text);
    
            Color[] colors = {Color.red, Color.white, Color.green, Color.yellow};
    
            Timer timer = new Timer(2000, e -> {
                int i = -1;
                while (i == previous) {
                    i = new Random().nextInt(4);
                }
                setBackground(colors[i]);
    
            });
    
            timer.start();
        }
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Changing the color of windows
    By Zarah in forum New To Java
    Replies: 2
    Last Post: 05-02-2014, 08:33 AM
  2. changing int with jbutton
    By Frank_21 in forum New To Java
    Replies: 19
    Last Post: 05-23-2013, 05:13 AM
  3. ActionListener for JButton after changing Button to JButton
    By ravi.joshi53 in forum Java Applets
    Replies: 2
    Last Post: 10-07-2011, 07:35 AM
  4. Changing background color
    By nikkka in forum New To Java
    Replies: 4
    Last Post: 03-12-2011, 05:54 AM
  5. Color-changing model
    By higuchi in forum New To Java
    Replies: 1
    Last Post: 03-19-2009, 07:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •