Simulate the operation of a processor using java swing

Asked

Viewed 279 times

-1

I need to simulate the workings of a processor using queue concepts. This processor will be color, where the user adds the color they want to be processed and its run time, this color needs to enter a queue, as the user adds more colors, this queue needs to be in order and resized on the screen , if the user needs to remove a color, this will be done in the first colors I added.

Note: this whole screen was made only by dragging the components of java swing. inserir a descrição da imagem aqui

I was able to add the buttons to the waiting list:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    String cores = (String) cor.getSelectedItem();
    String seg = (String) segundos.getSelectedItem();
    JButton b1 = new JButton(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’s calling the first color that was on the waiting list, now I want to take your time, run it and move on to the next color on the list. How to create a countdown with the time I spent together 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 on the 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] + " ");
        }
    }
    }
  • 5

    Too broad for a question. Try to develop and will post questions about very specific parts. The way someone is would have to not only develop, but think of the model, a minimum specification. My suggestion is the following: Instead of worrying about the graphical interface, worry about your model, about its classes (attributes, methods and relationships).

  • 1

    Let’s take parts. First you need a model. The model is a set of classes that models your business objects: Colors, queues, processing time, etc. Your model has to be totally independent, self-contained and separate from the swing. Once your model is ready, you make the swing talk to it. Sharing your project like this makes it easier for you.

  • There is also the problem that there is a data structure implemented in the hand there. There may be bugs in it as well.

1 answer

1


The model is not very complex: create a "Color" class to represent the colors and an Arraylist to represent the color queue. To add or remove elements from the list you will use the add() and remove() methods, according to the documentation:

https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

In the interface you need a Jpane to draw the queue. Create a new class for this.

Processing should be done on a separate Thread, as you need to have time control.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.