Yes, That needs to be done to change the value in myColor.do I need the myColor = new Color(200, 0, 0);?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Yes, That needs to be done to change the value in myColor.do I need the myColor = new Color(200, 0, 0);?
If you don't understand my answer, don't ignore it, ask a question.
Could I do a setColors getter/setter and set them all that way? Or is it better individually like setRed??
Many ways to do it. One way would be to build the color object locally and pass it to a setColor method.
If you don't understand my answer, don't ignore it, ask a question.
Vic45 (April 5th, 2020)
I know it's not good practice but I am trying this code out:
import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JRadioButton; import javax.swing.JSeparator; import javax.swing.JTextField; import javax.swing.JLabel; public class MainFrame extends JFrame { //mainframe is jframe private PaintPanel paintPanel; // private JTextField txtGreen; // private JTextField txtRed; // private JTextField txtBlue; private JLabel lblChooseColors; public MainFrame() { setSize(800, 600); setDefaultCloseOperation(EXIT_ON_CLOSE); paintPanel = new PaintPanel(); getContentPane().add(paintPanel); getColors(); } public void getColors() { JRadioButton rdbtnTriangle = new JRadioButton("Triangle", false); paintPanel.add(rdbtnTriangle); JRadioButton rdbtnSquare = new JRadioButton("Square", true); paintPanel.add(rdbtnSquare); JRadioButton rdbtnCircle = new JRadioButton("Circle", false); paintPanel.add(rdbtnCircle); ButtonGroup Gshapes = new ButtonGroup(); Gshapes.add(rdbtnTriangle); Gshapes.add(rdbtnSquare); Gshapes.add(rdbtnCircle); rdbtnTriangle.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub paintPanel.setShapeSelected(PaintPanel.SHAPE_TRIANGLE); } }); rdbtnSquare.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { paintPanel.setShapeSelected(PaintPanel.SHAPE_RECTANGLE); } }); rdbtnCircle.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { paintPanel.setShapeSelected(PaintPanel.SHAPE_CIRCLE); } }); JSeparator separator = new JSeparator(); paintPanel.add(separator); lblChooseColors = new JLabel("Choose color#'s"); paintPanel.add(lblChooseColors); JFormattedTextField frmtdtxtfldRed = new JFormattedTextField(); frmtdtxtfldRed.setText("Red"); paintPanel.add(frmtdtxtfldRed); JFormattedTextField frmtdtxtfldGreen = new JFormattedTextField(); frmtdtxtfldGreen.setText("Green"); paintPanel.add(frmtdtxtfldGreen); JFormattedTextField frmtdtxtfldBlue = new JFormattedTextField(); frmtdtxtfldBlue.setText("Blue"); paintPanel.add(frmtdtxtfldBlue); int intRedValue = Integer.parseInt(frmtdtxtfldRed.getText()); int intGreenValue = Integer.parseInt(frmtdtxtfldGreen.getText()); int intBlueValue = Integer.parseInt(frmtdtxtfldBlue.getText()); //paintPanel.myColor = new Color(intRedValue, intGreenValue, intBlueValue); JButton btnGo = new JButton("GO!"); btnGo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { paintPanel.myColor = new Color(intRedValue, intGreenValue, intBlueValue); } }); paintPanel.add(btnGo); } }
I'm getting these errors:
Exception in thread "main" java.lang.NumberFormatException: For input string: "Red"
at java.base/java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at MainFrame.getColors(MainFrame.java:99)
at MainFrame.<init>(MainFrame.java:32)
at Main.main(Main.java:7)
Could you maybe give me some advice here? Any idea what I'm doing wrong?
--- Update ---
Ok, I got it. Thanks for your help!
The is not consistent:The second statement should call a method to set the color instead of directly changing the variablepaintPanel.setShapeSelected(PaintPanel.SHAPE_CIRCLE); // calls a set method ... paintPanel.myColor = new Color(intRedValue, intGreenValue, intBlueValue); // Does NOT call a set method???
If you don't understand my answer, don't ignore it, ask a question.