I need to simulate the operation of a processor, using queue concepts. This processor will be color, where the user adds the color that wants to be processed and its execution time, this color needs to enter a queue, as the user adds more colors, this queue needs this in order and be resized on the screen, in case the user needs to remove a color, that removal will be done in the first colors I added.
OBs: This entire screen was made only by dragging the java swing components.
Iwasabletoaddthebuttonstothewaitinglist:
privatevoidjButton2ActionPerformed(java.awt.event.ActionEventevt){Stringcores=(String)cor.getSelectedItem();Stringseg=(String)segundos.getSelectedItem();JButtonb1=newJButton(cores+"/" + seg);
b1.setSize(10, 10);
b1.setVisible(true);
queue.enqueue(new ColorTime(cores, 5));
jPanel1.setLayout(new FlowLayout(FlowLayout.LEFT));
jPanel1.add(b1);
jPanel1.updateUI();
//setting flow layout of right alignment
}
and in the start option it is calling the first color that was on the waiting list, now I want to take your time, run it and move to the next color in the list. How to create a countdown timer with the time I spent with the color?
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//Timer timer;
//timer = new Timer();
//timer.s
while (item != null) {
item = queue.dequeue();
if (item != null) {
switch (item.color) {
case "Azul":
jPanel2.setBackground(Color.BLUE);
jTextField1.setBackground(Color.BLUE);
jTextField2.setText(item.color);
Timer t = new Timer(item.getTime(), action);
t.start();
break;
case "Verde":
jPanel2.setBackground(Color.GREEN);
jTextField1.setBackground(Color.GREEN);
jTextField2.setText(item.color);
break;
case "Vermelho":
jPanel2.setBackground(Color.RED);
jTextField1.setBackground(Color.RED);
jTextField2.setText(item.color);
break;
case "Cinza":
jPanel2.setBackground(Color.GRAY);
jTextField1.setBackground(Color.GRAY);
jTextField2.setText(item.color);
break;
case "Preto":
jPanel2.setBackground(Color.BLACK);
jTextField1.setBackground(Color.BLACK);
jTextField2.setText(item.color);
break;
case "Roxo":
jPanel2.setBackground(Color.CYAN);
jTextField1.setBackground(Color.CYAN);
jTextField2.setText(item.color);
break;
default:
System.out.println("Esta não é uma cor válida!");
}
}
}
}
My class list:
public class MyQueue <T> {
private T[] queue;
private int front = 0,
back = 0,
size;
/**
* Constructs an empty queue with an initial capacity of ten.
*/
public MyQueue () {
this.size = 10;
this.queue = (T[])new Object[size];
}
/**
* Constructs an empty queue with the specified initial capacity.
* @param size
*/
public MyQueue (int size) {
this.size = size;
this.queue = (T[])new Object[size];
}
/**
* Returns true if the queue contains no elements.
* @return
*/
public boolean isEmpty () {
return back == front;
}
/**
* Returns true if the queue is full.
* @return
*/
public boolean isFull () {
return front == size - 1;
}
/**
* Returns the size of the queue.
* @return
*/
public int size () {
return front - back;
}
/**
* Returns the element that is the end of the queue.
* If the referenced element does not exist null is returned.
* @return
*/
public T front () {
try {
return queue[front-1];
} catch (ArrayIndexOutOfBoundsException e) {
return null;
}
}
/**
* Returns the object that is the beginning of the queue
* If the referenced element does not exist null is returned.
* @return
*/
public T back () {
try {
return queue[back];
} catch (ArrayIndexOutOfBoundsException e) {
return null;
}
}
/**
* Adds an element in the queue.
* @param element
*/
public void enqueue (T element) {
if (isFull()) {
System.out.println("Error: queue overflow");
} else {
queue[front++] = element;
}
}
/**
* Removes an element in the queuein the queue and return the first element.
* In case of error return null.
* @return
*/
public T dequeue () {
if (isEmpty()) {
System.out.println("Error: queue underflow");
return null;
} else {
T item = queue[back++];
return item;
}
}
/**
* Removes all elements from the queue.
*/
public void clear () {
if (!isEmpty()) {
for (int i = 0; i < front; i++) {
queue[i] = null;
}
}
back = 0;
front = 0;
}
/**
* Prints all elements that are in the queue.
*/
public void print () {
if (!isEmpty()) {
for (int i = back; i < front; i++) {
System.out.print(queue[i] + " ");
}
}
}