Help I need to read Java files

0

Hello!

After a long wait at the end, I have to develop a small exercise with Threads.

I have a set of txt files with 4 colors in RGB code (red, green, blue), each file contains the rgb colors separated by spaces and the color name: "84 84 84 gray" on each line.

The application I am developing in java with graphical interface (JFrame) contains a panel that should change the background color and a label with the name of the displayed color.

The panel changes color by means of a shared resource for the threads, a class in java that will contain 3 integer variables corresponding to each color rgb respectively and a variable string of text corresponding to the name, this class will be accessed by the threads in execution by means of methods that change the value of the RGB colors and the name of the color and immediately the panel information is updated (panel background color, color name).

The problem is that I need to read the file colores1.txt every line that is in the file represents a color in RGB and according to what I say in each line I should show the color in the panel with the corresponding name

This is my code

    public class Hilo implements Runnable{

        RGBColor color;
        int rojo;
        int verde;
        int azul;
        String nombre;
        int rojo2;
        int verde2;
        int azul2;
        String nombre2;

        public Hilo(RGBColor color, int rojo, int verde, int azul,String nombre, int rojo2, int verde2, int azul2,String nombre2) {
            this.color = color;
            this.rojo = rojo;
            this.verde = verde;
            this.azul = azul;
            this.rojo2 = rojo2;
            this.verde2 = verde2;
            this.azul2 = azul2;
            this.nombre=nombre;
            this.nombre2=nombre2;
        }

    FileReader entrada = null;

String linea = "";

String directorio  =  JOptionPane.showInputDialog("Cual es la ubicacion del 
archivo");
String sDirectorio = directorio;
File f = new File(sDirectorio);
BufferedReader br = new BufferedReader(entrada);

        @Override
        public void run() {
            try {
            Thread.sleep(6000);
            this.color.set(rojo,verde,azul,nombre);

                Thread.sleep(3000);
            } catch (InterruptedException ex) {
                System.err.println(ex);
            }
            this.color.set(rojo2,verde2,azul2,nombre2);
        }

    }

    public class RGBColor{

        private int rojo;
        private int verde;
        private int azul;
        private String nombre;
        private JPanel jpanel;
        private JLabel label;




        public synchronized void set(int rojo,
                int verde,
                int azul,
                String nombre) {


        this.rojo = rojo;
        this.verde = verde;
        this.azul = azul;
        this.nombre = nombre;
        jpanel.setBackground(new Color(rojo,verde,azul));
        label.setText(nombre);

}

        /**
         * @param jpanel the jpanel to set
         */
        public void setJpanel(JPanel jpanel) {
            this.jpanel = jpanel;
        }

        /**
         * @param label the label to set
         */
        public void setLabel(JLabel label) {
            this.label = label;
        }

    }
    
asked by Sebastian Salazar 07.03.2018 в 05:12
source

1 answer

2

I do not know if I understood correctly, but your critical region should be this, so adding synchronized there you should have the method synchronized so that two threads can not access the resource simultaneously.

public synchronized void set(int rojo,
                int verde,
                int azul,
                String nombre) {


        this.rojo = rojo;
        this.verde = verde;
        this.azul = azul;
        this.nombre = nombre;
        jpanel.setBackground(new Color(rojo,verde,azul));
        label.setText(nombre);

}

The normal thing, or at least for what I have worked with threads in java, is to put the synchronization method for it. So that two threads can not access simultaneously to the same resource, hence the synchronized in the set, because that is where the resource is accessed.

    
answered by 07.03.2018 / 15:29
source