Java - secuencia de cambio de colores

 
Vista:
sin imagen de perfil
Val: 15
Ha aumentado su posición en 4 puestos en Java (en relación al último mes)
Gráfica de Java

secuencia de cambio de colores

Publicado por maruch (3 intervenciones) el 17/04/2019 00:33:09
Desarrollar una animación que simule un semáforo. Debe mostrar tres círculos, uno rojo, amarillo y verde.

Al darle click en Iniciar la secuencia de cambio de colores debe comenzar.

El círculo rojo debe durar 3 segundos, el amarillo 1 segundo y el verde 4 segundos.

Entre el rojo y el verde siempre debe pasar por el amarillo.


Hice dos clases y la interfaz llevo esto pero no se como seguir ayuda. Gracias

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.awt.Color;
import static java.lang.Thread.sleep;
 
public class Bola {
    private int x;
    private int y;
    private Color color;
        int contador=0,temp=0,temp2=0,temp3=0,temp4=0;
 
 
    public Bola(int x, int y, Color c) {
        this.x = x;
        this.y = y;
        color = c;
    }
 
    public void cambiar(Color c)
    {
        color = c;
    }
 
    public  int getX() {
        return x;
    }
 
    public int getY() {
        return y;
    }
 
    public Color getColor() {
        return color;
    }
 
}
esta es la clase donde instancio todo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
 
public class Tablero extends Canvas {
    private Bola[] lista;
 
    public Tablero() {
        this.lista = new Bola[]{ new Bola(10, 10,  Color.RED),
                                 new Bola(10, 60,  Color.YELLOW),
                                 new Bola(10, 110,  Color.GREEN)};
    }
 
    @Override
    public void paint(Graphics g)
    {
        setBackground(Color.black);
        for (Bola b : lista) {
            if (b != null) {
                g.setColor(b.getColor());
                g.fillOval(b.getX(), b.getY(), 30, 30);
            }
        }
    }
 
    public void cambiar()
    {
        for (Bola b : lista) {
            if (b != null) {
                b.cambiar(Color.WHITE);
            }
        }
    }
 
 
}



aquí creo el tablero
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

secuencia de cambio de colores

Publicado por Tom (1831 intervenciones) el 17/04/2019 11:54:58
No caigas en la tentación de usar sleep().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;
 
/* */
public class Test_22 extends JFrame implements ActionListener {
	JButton btSem;
	Timer semTimer;
	boolean power = false;
	int state = -1;
	/* */
	Test_22() {
		semTimer = new Timer(0, this);
		semTimer.setActionCommand("state_change");
		semTimer.setRepeats(false);
 
		btSem = new JButton("RUN");
		btSem.setFont(new Font("Arial", Font.PLAIN, 32));
		btSem.setBackground(Color.LIGHT_GRAY);
		btSem.setActionCommand("power_switch");
		btSem.addActionListener(this);
 
		add(btSem);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
	}
	/* */
	public void actionPerformed(ActionEvent ev) {
		switch(ev.getActionCommand()) {
		case "power_switch":
			power = !power;
			if(power) {
				state = -1;
				stateChange();
			} else {
				semTimer.stop();
				btSem.setBackground(Color.LIGHT_GRAY);
			}
			break;
		case "state_change":
			stateChange();
		}
	}
	/* */
	void stateChange() {
		state = (state + 1) % 3;
 
		switch(state) {
		case 0:
			semTimer.setInitialDelay(3 * 1_000);
			btSem.setBackground(Color.RED);
			semTimer.start();
			break;
		case 1:
			semTimer.setInitialDelay(1 * 1_000);
			btSem.setBackground(Color.YELLOW);
			semTimer.start();
			break;
		case 2:
			semTimer.setInitialDelay(4 * 1_000);
			btSem.setBackground(Color.GREEN);
			semTimer.start();
			break;
		}
	}
	/* */
	public static void main(String[] args) {
		new Test_22().setVisible(true);
	}
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
sin imagen de perfil
Val: 15
Ha aumentado su posición en 4 puestos en Java (en relación al último mes)
Gráfica de Java

secuencia de cambio de colores

Publicado por maruch (3 intervenciones) el 17/04/2019 15:08:37
Gracias amigo por la ayuda. Una pregunta más esto que creaste es una nueva clase o la puedo implementar en el JFrame. mi interfaz voy así.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.awt.event.ActionEvent;
import javax.swing.Timer;
 
/**
 *
 * 
 */
public class Principal extends javax.swing.JFrame {
 
    Timer reloj;
 
    public Principal() {
        initComponents();
 
        reloj = new Timer(1000, (ActionEvent ae) -> {
            //tablero.cambiar();
            //tablero.repaint();
        });
    }
 
    private void btnIniciarActionPerformed(java.awt.event.ActionEvent evt) {
 
    }
 
    private void btnPararActionPerformed(java.awt.event.ActionEvent evt) {
 
    }
 
    public static void main(String args[]) {
 
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Principal().setVisible(true);
            }
        });
    }
 
    // Variables declaration - do not modify
    private javax.swing.JButton btnAutor;
    private javax.swing.JButton btnIniciar;
    private javax.swing.JButton btnParar;
    private examen.Tablero tablero;
    // End of variables declaration
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

secuencia de cambio de colores

Publicado por MATT (1 intervención) el 08/08/2019 16:54:14
si quiero mostrar unas imagenes un cierto tiempo, pero que al oprimir una tecla cambie ala otra imagen y medir cuanto tiempo tarde en oprimir ese boton y me salga en un print que clase debo usar por fa
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar