I have posted my code below, but essentially I’m having a little trouble.
I am trying to create a program which will take 3 inputs of text, these being red, green and blue.
The idea is that the text starts as red, and when a change colour button is pressed. The RGB values entered will be taken and the colour will be changed based on the values.
However I am having trouble getting the values entered into the text field to be taken by the program and change the colour. Any help is appreciated.
I was also having problem with getting both the text and colour values to change together in the handler when they were edited manually in the code. It would either just change the colour, or just change the text.
Any help will be greatly appreciated. 😀
package RGBProgram;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class RGB extends JApplet {
Color col = new Color(255, 0, 0);
String str = "Hello";
JButton butReset, butChange;
JTextField textR, textG, textB;
public void init(){
butReset = new JButton("Reset");
butChange = new JButton("Change Colour");
textR = new JTextField("Red", 10);
textG = new JTextField("Green", 10);
textB = new JTextField("Blue", 10);
RGBPanel panel = new RGBPanel(this);
JPanel butPanel = new JPanel();
JPanel textPanel = new JPanel();
butPanel.add(butReset);
butPanel.add(butChange);
textPanel.add(textR);
textPanel.add(textG);
textPanel.add(textB);
add(panel, BorderLayout.CENTER);
add(butReset, BorderLayout.NORTH);
add(butChange, BorderLayout.SOUTH);
add(textPanel, BorderLayout.WEST);
Handler reset = new Handler(this);
Handler change = new Handler(this);
textR.addActionListener (new Handler(this));
textG.addActionListener (new Handler(this));
textB.addActionListener (new Handler(this));
butReset.addActionListener(reset);
butChange.addActionListener(change);
}
class RGBPanel extends JPanel{
RGB theApplet;
RGBPanel(RGB app){
theApplet = app;
}
public void paintComponent(Graphics g)
{super.paintComponent(g);
Color cols = col;
String str1 = str;
g.setColor(cols);
g.drawString(str1, 0, 150);
}
}
}
package RGBProgram;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Handler implements ActionListener {
RGB theApplet;
Handler(RGB app){
theApplet = app;
}
public void actionPerformed(ActionEvent e){
String red = theApplet.textR.getText();
String green = theApplet.textG.getText();
String blue = theApplet.textB.getText();
theApplet.textR.setText("");
theApplet.textG.setText("");
theApplet.textB.setText("");
try{
int r = Integer.parseInt(red.trim());
int g = Integer.parseInt(green.trim());
int b = Integer.parseInt(blue.trim());
}
catch (NumberFormatException ex){
}
if (e.getSource() == theApplet.butChange)
theApplet.str = "Goodbye";
theApplet.col = new Color(r, g, b);
if (e.getSource() == theApplet.butReset)
theApplet.str = "Hello";
theApplet.col = new Color(255, 0, 0);
theApplet.repaint();
}
}
I changed the
actionPerformedmethod in theHandlerclass to be as below and the color is now applied correctly: