Can anyone help me?(SMALL JAVA PROJECT) Assignment Create a class named App1 whi
ID: 3700706 • Letter: C
Question
Can anyone help me?(SMALL JAVA PROJECT)
Assignment
Create a class named App1 which provides a GUI window. Users will enter 24-bit RGB color values and the window will then display them. The application should meet the specifications below:
1) The window will contain an upper panel that holds three text boxes (JTextFields); each text box will have two associated buttons (JButtons).
a) The text boxes will be for red (R), green (G), and blue (B) and should be labeled
b) The values should be initialized to zero for all three text boxes
c) Users can enter values into the text boxes
d) For each text box, one button should allow incrementing and another should allow decrementing the present value, and should be labeled appropriately
e) Any color changes caused by clicking buttons should immediately be shown in the text boxes.
2) The window will contain a lower panel that displays, as its background, the RGB color specified by the values in the upper panel’s text boxes.
a) Initially the RGB value will be zero
b) Whenever any of the values in the top panel’s text boxes change, the lower panel’s background color will immediately update to match it.
3) No error checking is required (i.e., you can assume users will only enter integers), however:
a) Entering a number outside the range 0 to 255 will be treated as entering a zero
b) Clicking a decrement button when the value is zero will have no effect (no drop to -1)
c) Clicking an increment button when the value is 255 will have no effect (no rise to 256)
The graphical interface will look like the one as follows: ?Application 1 Red Green + 10Explanation / Answer
Hi... I have written java program for the above. Please check below code.
MyWindow.java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class MyWindow extends JFrame {
public MyWindow() {
this.setLayout(new BorderLayout());
JPanel leftPanel = new JPanel();
leftPanel.setBackground(Color.RED);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.GREEN);
JPanel rightPanel = new JPanel();
rightPanel.setBackground(Color.BLUE);
/*rightPanel.setSize(150,150);
centerPanel.setSize(250,250);
leftPanel.setSize(150,150);*/
JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, centerPanel);
JSplitPane sp2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sp, rightPanel);
JLabel lb1 = new JLabel("Red");
JButton jb1 = new JButton("+");
JTextField jt1 = new JTextField("Number");
jt1.setText("0 ");
jt1.setBounds(50,100, 200,30);
JButton jb2 = new JButton("-");
JLabel glb1 = new JLabel("Green");
JButton gjb1 = new JButton("+");
JTextField gjt1 = new JTextField("Number");
gjt1.setText("0 ");
gjt1.setBounds(50,50,150,20);
JButton gjb2 = new JButton("-");
JLabel blb1 = new JLabel("Blue");
JButton bjb1 = new JButton("+");
JTextField bjt1 = new JTextField("Number");
bjt1.setText("0 ");
bjt1.setBounds(50,100,200,30);
JButton bjb2 = new JButton("-");
jt1.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
//displayMessage("Focus gained", e);
}
public void focusLost(FocusEvent e) {
// displayMessage("Focus lost", e);
int red = Integer.parseInt(jt1.getText().trim());
int green = Integer.parseInt(gjt1.getText().trim());
int blue = Integer.parseInt(bjt1.getText().trim());
Color c = new Color(red, green, blue);
leftPanel.setBackground(c);
}
});
jb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// display/center the jdialog when the button is pressed
String val = jt1.getText().trim();
int v = Integer.parseInt(val);
v++;
if(v>256){
v=0;
}
jt1.setText(String.valueOf(v));
int red = v;
int green = Integer.parseInt(gjt1.getText().trim());
int blue = Integer.parseInt(bjt1.getText().trim());
Color c = new Color(red, green, blue);
leftPanel.setBackground(c);
}
});
jb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// display/center the jdialog when the button is pressed
String val = jt1.getText().trim();
int v = Integer.parseInt(val);
v--;
if(v>256){
v=0;
}else if(v<0){
v=0;
}
jt1.setText(String.valueOf(v));
int red = v;
int green = Integer.parseInt(gjt1.getText().trim());
int blue = Integer.parseInt(bjt1.getText().trim());
Color c = new Color(red, green, blue);
leftPanel.setBackground(c);
}
});
gjt1.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
//displayMessage("Focus gained", e);
}
public void focusLost(FocusEvent e) {
// displayMessage("Focus lost", e);
int red = Integer.parseInt(jt1.getText().trim());
int green = Integer.parseInt(gjt1.getText().trim());
int blue = Integer.parseInt(bjt1.getText().trim());
Color c = new Color(red, green, blue);
centerPanel.setBackground(c);
}
});
gjb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// display/center the jdialog when the button is pressed
String val = gjt1.getText().trim();
int v = Integer.parseInt(val);
v++;
if(v>256){
v=0;
}
gjt1.setText(String.valueOf(v));
int red = Integer.parseInt(jt1.getText().trim());
int green = Integer.parseInt(gjt1.getText().trim());
int blue = Integer.parseInt(bjt1.getText().trim());
Color c = new Color(red, green, blue);
centerPanel.setBackground(c);
}
});
gjb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// display/center the jdialog when the button is pressed
String val = gjt1.getText().trim();
int v = Integer.parseInt(val);
v--;
if(v>256){
v=0;
}else if(v<0){
v=0;
}
gjt1.setText(String.valueOf(v));
int red = Integer.parseInt(jt1.getText().trim());
int green = Integer.parseInt(gjt1.getText().trim());
int blue = Integer.parseInt(bjt1.getText().trim());
Color c = new Color(red, green, blue);
centerPanel.setBackground(c);
}
});
bjt1.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
//displayMessage("Focus gained", e);
}
public void focusLost(FocusEvent e) {
// displayMessage("Focus lost", e);
int red = Integer.parseInt(jt1.getText().trim());
int green = Integer.parseInt(gjt1.getText().trim());
int blue = Integer.parseInt(bjt1.getText().trim());
Color c = new Color(red, green, blue);
rightPanel.setBackground(c);
}
});
bjb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// display/center the jdialog when the button is pressed
String val = bjt1.getText().trim();
int v = Integer.parseInt(val);
v++;
if(v>256){
v=0;
}
bjt1.setText(String.valueOf(v));
int red = Integer.parseInt(jt1.getText().trim());
int green = Integer.parseInt(gjt1.getText().trim());
int blue = Integer.parseInt(bjt1.getText().trim());
Color c = new Color(red, green, blue);
rightPanel.setBackground(c);
}
});
bjb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// display/center the jdialog when the button is pressed
String val =bjt1.getText().trim();
int v = Integer.parseInt(val);
v--;
if(v>256){
v=0;
}else if(v<0){
v=0;
}
bjt1.setText(String.valueOf(v));
int red = Integer.parseInt(jt1.getText().trim());
int green = Integer.parseInt(gjt1.getText().trim());
int blue = Integer.parseInt(bjt1.getText().trim());
Color c = new Color(red, green, blue);
rightPanel.setBackground(c);
}
});
leftPanel.add(lb1); leftPanel.add(jb1); leftPanel.add(jt1); leftPanel.add(jb2);
centerPanel.add(glb1); centerPanel.add(gjb1); centerPanel.add(gjt1); centerPanel.add(gjb2);
rightPanel.add(blb1); rightPanel.add(bjb1); rightPanel.add(bjt1); rightPanel.add(bjb2);
this.add(sp2, BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600, 600);
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new MyWindow();
}
});
}
}
Please test the code and let me know any issues. Thank you. All the best.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.