Android Studios is the medium through which this assignment is competed. The bas
ID: 3888583 • Letter: A
Question
Android Studios is the medium through which this assignment is competed.
The basis of the assignment is to :
Write an app that lets the user create a color using the RGB color model. It displays three text fields and one label. Expect the user to enter integers between 0 and 255
included in the three text fields; if a value is negative, it should be converted to 0;
if it is greater than 255, it should be converted to 255.
The value of the text fields represents the amount of red, green, and blue for the background color in the label.
When the user modifies the number in any text field, the background color of the label should be updated.
You should look at some documentation in the View class to figure out how to change the color of a View using programming.
The three text fields should have the same style with a minimum of four attributes. All four components should be centered vertically with good spacing and size.
Explanation / Answer
I am giving the steps to display color in label dynamically.
1.
Take three text field redText, greenText and blueText which can assign integer type and one label to display color.
2.
Apply validation on all the three text e.g.
if(redValue >0)
return redValue;
else if(redValue> 255)
return 255;
else
return 0;
same way we need to validate greenValue and blueValue.
3. class CreateColor extends Applet
Here we create class CreateColor which is extend by Applet class.
4.
Inside public void init()
create one Label i.e. Label label = new Label("display background color");
Add this label to awt container i.e. add(label);
5.
To set background color of a label use void setBackground(Color c) method
Here Color method from java.awt.Color;
label.setBackground(new Color(redValue, greenValue, blueValue));
6. Please find the sample code look like as below
import java.applet.Applet;
import java.awt.Color;
import java.awt.Label;
public class CreateColor extends Applet{
public void init(){
Label label = new Label("display background color");
add(label);
label.setBackground(new Color(244, 11, 23));
}
}
Here I take redValue = 244, greenValue = 11 and blueValue=23