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.

Page 2 of 2 FirstFirst 12
Results 26 to 30 of 30

Thread: Making a universal RGB Color in Swing

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Making a universal RGB Color in Swing

    do I need the myColor = new Color(200, 0, 0);?
    Yes, That needs to be done to change the value in myColor.
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    Junior Member
    Join Date
    Apr 2020
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Making a universal RGB Color in Swing

    Could I do a setColors getter/setter and set them all that way? Or is it better individually like setRed??

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Making a universal RGB Color in Swing

    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.

  4. The Following User Says Thank You to Norm For This Useful Post:

    Vic45 (April 5th, 2020)

  5. #29
    Junior Member
    Join Date
    Apr 2020
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Making a universal RGB Color in Swing

    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!

  6. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Making a universal RGB Color in Swing

    The is not consistent:
    paintPanel.setShapeSelected(PaintPanel.SHAPE_CIRCLE);   // calls a set method
    ...
    paintPanel.myColor = new Color(intRedValue, intGreenValue, intBlueValue);  // Does NOT call a set method???
    The second statement should call a method to set the color instead of directly changing the variable
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 0
    Last Post: August 26th, 2019, 08:32 AM
  2. Replies: 6
    Last Post: September 15th, 2013, 07:18 PM
  3. making the color dissapear.
    By arieltal in forum AWT / Java Swing
    Replies: 1
    Last Post: September 26th, 2012, 12:08 PM
  4. [SOLVED] universal outputstream
    By beruska in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 17th, 2011, 07:26 AM
  5. text color changer based on words/ word classification by color
    By knoxy5467 in forum Java Theory & Questions
    Replies: 25
    Last Post: June 15th, 2011, 07:52 AM

Tags for this Thread