ponsly
ponsly

Reputation: 87

Using Radio Buttons to change the colour of a GUI

I've written a program that has a graphical display of two arrays. One is full of random numbers, the other has the numbers arranged via a bubble sort. The program then takes these two arrays and displays them by shading a square based on the value of the number at the ith place of the array. I've then tried to modify my code to include some Radio Buttons to change the theme colour of the display, either red, green, or blue. When one button is selected, the display is fine, but when another button is pressed, the colour becomes a combination of the chosen ones (eg, yellow for red and green, white for all three) and you can't unselect a colour either.

My question is: How can I modify my code so that the Radio Buttons on my GUI can repeatedly swap between three colours, and not any combination of them?

Main:

import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;


public class BubbleMain {

public static void main(String args[])throws IOException{

    int n;
    Scanner keyboard = new Scanner(System.in);

    while(true){
        try{
            System.out.println("Enter the size of the array");
            n = keyboard.nextInt();

            if(n >= 2){
                break;
            }
            System.out.println("Size must be 2 or greater");
        }catch(InputMismatchException e){
            System.out.println("Value must be an integer");
            keyboard.nextLine();
        }
    }


    double[] template = new double[n];
    double[] mess = Bubble.randPop(template);

    double[] tidy = Arrays.copyOf(mess, mess.length);
    tidy = Bubble.bubbleSort(tidy);

    System.out.println("original: ");
    Bubble.printOut(mess);
    System.out.println("sorted: ");
    Bubble.printOut(tidy);

    String filename = "bubresult.txt";
    Bubble.printFile(mess, tidy, filename);


    double xMax = 0.0;
    double xMin = 0.0;

    while(true){
        try{
            System.out.println("Enter the minimum value of the range: ");
            xMin = keyboard.nextDouble();

            System.out.println("Enter the max value: ");
            xMax = keyboard.nextDouble();

            if(xMax > xMin){
                break;
            }
            System.out.println("The max value must be greater than the minimum");           
        }catch(InputMismatchException e){
            System.out.println("Value must be a double");
            keyboard.nextLine();
        }
    }

    double[] rangeTemplate = new double[n];
    double[] messRange = Bubble.randRangePop(rangeTemplate, xMin, xMax);

    double[] tidyRange = Arrays.copyOf(messRange, messRange.length);
    tidyRange = Bubble.bubbleSort(tidyRange);

    System.out.println("original: ");
    Bubble.printOut(messRange);
    System.out.println("sorted");
    Bubble.printOut(tidyRange);

    BubFrame bf = new BubFrame(mess, tidy);
    bf.setVisible(true);

}
}

Bubble:

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;




public class Bubble {

private double[] list;

public Bubble(double[] list){

    this.list = list;
}

public double[] getArray(){
    return list;
}

public static double[] randPop(double[] template){

    for(int i = 0; i < template.length; i++){
        template[i] = Math.random();
    }

    return template;
}


public static double[] randRangePop(double[] rangeTemplate, double xMin, double xMax){

    for(int i = 0; i < rangeTemplate.length; i++){
        Random r = new Random();
        double randomValue = xMin + (xMax - xMin) * r.nextDouble();
        rangeTemplate[i] = (xMin ) + (Math.random()* ( xMax - xMin + 1));;
    }

    //Arrays.
    return rangeTemplate;
}

public static double[] bubbleSort(double[] mess){

    double[] tidy = new double[mess.length];

    for(int i=0; i<mess.length; i++)
    {
        for(int j=i + 1; j<mess.length; j++)
        {
            if(mess[i] > mess[j])
            {
                double temp = mess[i];
                mess[i] = mess[j];
                mess[j] = temp;
            }
        }
        tidy[i] = mess[i];
    }
    return tidy;
}




public static void printOut(double[] list){

    for(int i = 0; i < list.length; i++){
        System.out.printf("%.3f \n", list[i]);
    }
}



public static void printFile(double[] mess, double[] tidy, String filename)throws IOException{

    FileWriter outputfile = new FileWriter(filename);
    BufferedWriter out = new BufferedWriter(outputfile);

    String origIntro = "Original Array: \n";
    out.write(origIntro, 0, origIntro.length());

    for(int i = 0; i < mess.length; i++){

        String orig = ""+mess[i]+"\n";
        out.write(orig, 0, orig.length());
    }


    String sortIntro = "Sorted Array: \n";
    out.write(sortIntro, 0, sortIntro.length());

    for(int i = 0; i < tidy.length; i++){

        String sort = ""+tidy[i]+"\n";
        out.write(sort, 0, sort.length());
    }

    out.close();
    outputfile.close();

}
}

Graphics:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

class BubPanel extends JPanel{

private int X, Y;
private int wM, hM, wT, hT;
private int R, G, B;

private double[] mess, tidy;

public BubPanel(double[] mess, double[] tidy){

    this.mess = mess;
    this.tidy = tidy;

    setBackground(Color.GRAY);


}

public void paintComponent(Graphics g){

    g.setColor(getBackground());
    super.paintComponent(g);


    wM = getWidth()/mess.length;
    hM = 40;
    wT = getWidth()/tidy.length;
    hT = 40;

    for(int i = 0; i < mess.length; i++){

        Color clr = new Color((int)(255*mess[i])*R, (int)(255*mess[i])*G, (int)(255*mess[i])*B);
        g.setColor(clr);
        g.fillRect(wM*i, 20, wM, hM);
    }


    for(int i = 0; i < tidy.length; i++){

        Color clr = new Color((int)(255*tidy[i])*R, (int)(255*tidy[i])*G, (int)(255*tidy[i])*B);
        g.setColor(clr);
        g.fillRect(wT*i, getHeight()-20-hT, wT, hT);
    }

}

public int getR(){
    return R;
}
public void setR(int R){
    this.R = R;
}
public int getG(){
    return G;
}
public void setG(int G){
    this.G = G;
}
public int getB(){
    return B;
}
public void setB(int B){
    this.B = B;
}
}






class BubFrame extends JFrame{

private int X = 800;
private int Y = 200;

double[] mess, tidy;

BubPanel bubPan;

JRadioButton R = new JRadioButton("R");
JRadioButton G = new JRadioButton("G");
JRadioButton B = new JRadioButton("B");

public BubFrame(double[] mess, double[] tidy){

    this.mess = mess;
    this.tidy = tidy;

    bubPan = new BubPanel(mess, tidy);

    setLayout(new BorderLayout());

    getContentPane().add(bubPan, BorderLayout.CENTER);
    pack();

    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e){
            System.exit(0);
        }
    });

    ButtonGroup group = new ButtonGroup();
    group.add(R);
    group.add(G);
    group.add(B);


    JPanel buttonPanel = new JPanel();
    buttonPanel.add(R);
    buttonPanel.add(G);
    buttonPanel.add(B);
    add(buttonPanel, BorderLayout.PAGE_END);




    setTitle("Bubble Sort");
    setBackground(Color.PINK);
    setSize(X, Y);
    setLocation(200, 200);
    redFlavour();
    greenFlavour();
    blueFlavour();

}


public void redFlavour(){
    R.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            if(R.isSelected()){
            bubPan.setR(1);
            }else if(!R.isSelected()){
                bubPan.setR(0);
            }
            repaint();
        }
    });
}

public void greenFlavour(){
    G.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            if(G.isSelected()){
            bubPan.setG(1);
            }else if(!G.isSelected()){
                bubPan.setG(0);
            }
            repaint();
        }
    });
}

public void blueFlavour(){
    B.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            if(B.isSelected()){
            bubPan.setB(1);
            }else if(!B.isSelected()){
                bubPan.setB(0);
            }
            repaint();
        }
    });
}

}

Upvotes: 0

Views: 997

Answers (2)

Kajal Sinha
Kajal Sinha

Reputation: 1575

You are setting either the R, G and B values but not unsetting the other 2. Please replace actionPerformed of the different flavours with the following

public void redFlavour(){
         R.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e){
                 if(R.isSelected()){
                 bubPan.setR(1);
                 bubPan.setG(0);
                 bubPan.setB(0);
                 }else if(!R.isSelected()){
                     bubPan.setR(0);
                 }
                 repaint();
             }
         });
     }

     public void greenFlavour(){
         G.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e){
                 if(G.isSelected()){
                     bubPan.setR(0);
                     bubPan.setG(1);
                     bubPan.setB(0);
                 }else if(!G.isSelected()){
                     bubPan.setG(0);
                 }
                 repaint();
             }
         });
     }

     public void blueFlavour(){
         B.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e){
                 if(B.isSelected()){
                     bubPan.setR(0);
                     bubPan.setG(0);
                     bubPan.setB(1);
                 }else if(!B.isSelected()){
                     bubPan.setB(0);
                 }
                 repaint();
             }
         });
     }

Upvotes: 2

Hugo Sousa
Hugo Sousa

Reputation: 1936

In your JRadioButton listeners, you need to clear the other colors too. Otherwise, you'll have a mix of them.

So, for instance:

B.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        if(B.isSelected()){
            bubPan.setR(0);  //added this
            bubPan.setG(0);  //added this
            bubPan.setB(1);         
        }else if(!B.isSelected()){
            bubPan.setB(0);
        }
        repaint();
    }
});

And do the same for the other buttons.

Upvotes: 0

Related Questions