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 1 of 2 12 LastLast
Results 1 to 25 of 30

Thread: Making a universal RGB Color in Swing

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

    Default Making a universal RGB Color in Swing

    Hello. So I am making a small "paint" program in java as a GUI. I have most of it done but one thing is giving trouble. I need to be able to set an RGB value from one class to another. I have tried get/set RGB but that gave me errors. I ultimately need to have the user input the values to something like a jtextfield and then use that value with the particular shape being drawn. So I have it so I can draw circle and rectangles and change their color manually, but getting the RGB as input and applying it to a shape thats within an if statement is eluding me.


    In this bit of code, the x and y are fine, but the third argument is the color. How do I dynamically (without if statements or a switch) change the final color value within this if statement?

     if(shapeSelected.equals(SHAPE_RECTANGLE)) { drawingShape = new Rectangle(e.getX(), e.getY(), colorSelected);


    Here is the full code:

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;
    import java.util.ArrayList;
     
    import javax.swing.JPanel;
     
    import com.herbst.shapes.Circle;
    import com.herbst.shapes.Rectangle;
     
    public class PaintPanel extends JPanel {
    	private String shapeSelected = SHAPE_RECTANGLE;
    	private Color colorSelected = COLOR_RED;
    	private ArrayList<Shape> shapes;
     
    	public static final Color COLOR_RED = "RED";
    //	public static final int COLOR_GREEN = 0;
    //	public static final int COLOR_BLUE = 0;
     
    	public static final String SHAPE_RECTANGLE = "rectangle";
    	public static final String SHAPE_CIRCLE = "circle";
    	public static final String SHAPE_TRIANGLE = "triangle";
     
    	//Color red2 = new Color(155,0,0);
    	// private ArrayList<Shape> circles; only need 1 rectangle array
     
    	public PaintPanel() {
    		shapes = new ArrayList<Shape>();
     
    		addMouseMotionListener(new MouseMotionListener() {
    			private Shape drawingShape = null;
     
    			@Override
    			public void mouseMoved(MouseEvent e) {
    				drawingShape = null;
    			}
    //				System.out.println("mouse Released" + e.getX() + ", " + e.getY());
    //				drawingShape = new Rectangle(e.getX(), e.getY(), Color.Black);
    //				addShape(drawingShape);
     
    			@Override
    			public void mouseDragged(MouseEvent e) {
     
    				if (drawingShape == null) {
    					if(shapeSelected.equals(SHAPE_RECTANGLE)) {
    						drawingShape = new Rectangle(e.getX(), e.getY(), colorSelected);
    					} else if(shapeSelected.equals(SHAPE_CIRCLE)) {
    						drawingShape = new Circle(e.getX(), e.getY(), Color.BLUE);
    					} else {
    						drawingShape = new Rectangle(e.getX(), e.getY(), Color.BLUE);
    					}
    					if(colorSelected.equals(COLOR_RED)) {
     
    					}
     
    					addShape(drawingShape);
    				} else {
    					drawingShape.setWidth(e.getX() - drawingShape.getX());
    					drawingShape.setHeight(e.getY() - drawingShape.getY());
    					repaint();
    				}
    //					 System.out.println("mouse Released" + e.getX() + ", " + e.getY(), Color.BLACK);
    //					drawingShape = new Rectangle(e.getX(), e.getY(), Color.Black);
    //					addShape(drawingShape);
    			}
    		});
    	}
     
    //	
    //		shapes.add(new Rectangle(100, 100, Color.black));
    //		shapes.add(new Rectangle(400, 200, new Color(1,0,1)));
    //
    //		shapes.add(new Circle(200, 300));
    //		shapes.add(new Circle(400, 30));
     
    	private void addShape(Shape shape) {
    		shapes.add(shape);
    		repaint();
    	}
     
    	// Called automatically
    	@Override
    	protected void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		for (Shape shape : shapes) {
    			shape.paint(g);
    		}
     
    	}
     
    	public String getShapeSelected() {
    		return shapeSelected;
    	}
     
    	public void setShapeSelected(String shapeSelected) {
    		this.shapeSelected = shapeSelected;
    	}
     
    	public String getColorSelected() {
    		return colorSelected;
    	}
     
    	public void setColorSelected(String colorSelected) {
    		this.colorSelected = colorSelected;
    	}

    And here is the relevant bit of code from the other class:


    	rdbtnRed.addActionListener(new ActionListener() {
     
    		@Override
    		public void actionPerformed(ActionEvent arg0) {
    			// TODO Auto-generated method stub
    			paintPanel.setColorSelected(PaintPanel.COLOR_RED);
    		}
     
    	});

    Let me know if you need the rest of the code.

    Thank you.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making a universal RGB Color in Swing

    need to have the user input the values
    Where is the code that creates the Color object from the R, G and B values?

    Once there is a Color object its reference can be saved and passed to other methods as needed.

    How do I dynamically (without if statements or a switch) change the final color value
    You can change the value of a variable with an assignment statement.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    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

    It was actually commented out. So I know I need something like this: Color c = new Color(255, 0, 0); for red, correct? But putting that into my code:
    drawingShape = new Rectangle(e.getX(), e.getY(), Color.c);
    like that or this:
    drawingShape = new Rectangle(e.getX(), e.getY(), Color.getColor(c));
    doesn't work. - So how do I call that color object within that code?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making a universal RGB Color in Swing

    doesn't work.
    Please copy the full text of the error message and paste it here. It has important info about the error.

    how do I call that color object
       drawingShape = new Rectangle(e.getX(), e.getY(), referenceToColor);  // pass reference to color
     
     
        Color myColor = new Color(255, 0, 0);    // here myColor is a reference to a color object
        drawingShape = new Rectangle(e.getX(), e.getY(), myColor);  // pass reference to color
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    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

    Ok, so your code verbatim worked. I was trying to use stuff like Color.c and getColor(c) It works fine with just c as the final argument. Thank you for that.

    One more problem - setting the colors. so I can set them manually, like this:
    public int COLOR_RED;
    	public int COLOR_GREEN = 100;
    	public int COLOR_BLUE; 
     
    	Color c = new Color(COLOR_RED, COLOR_GREEN, COLOR_BLUE);

    Right now I am trying to use getters/setters, like this:

    	public int getRed() {
    		return COLOR_RED;
    	}
     
    	public void setRed(int red) {
    		this.COLOR_RED = red;
    	}
     
    	public int getGreen() {
    		return COLOR_GREEN;
    	}
     
    	public void setGreen(int green) {
    		this.COLOR_GREEN = green;
    	}
     
    	public int getBlue() {
    		return COLOR_BLUE;
    	}
     
    	public void setBlue(int blue) {
    		this.COLOR_BLUE = blue;
    	}

    And calling it here:

    rdbtnRed.addActionListener(new ActionListener() {
     
    		@Override
    		public void actionPerformed(ActionEvent arg0) {
    			// TODO Auto-generated method stub
    		paintPanel.setRed(0);
    		}
     
    	});
     
    	rdbtnGreen.addActionListener(new ActionListener() {
     
    		@Override
    		public void actionPerformed(ActionEvent arg0) {
    			// TODO Auto-generated method stub
    			paintPanel.setGreen(0);
    		}
     
    	});
    	rdbtnBlue.addActionListener(new ActionListener() {
     
    		@Override
    		public void actionPerformed(ActionEvent arg0) {
    			// TODO Auto-generated method stub
    			paintPanel.setBlue(100);
    		}
     
    	});

    What am I doing wrong? How should I go about this?

    Thank you.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making a universal RGB Color in Swing

    What am I doing wrong?
    Please explain.
    If you are getting errors, post them
    If the code is not doing what you want, please explain.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    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'm not getting errors now. I am trying to set the color from a different class than where this code is: (This situation confuses me often)

    I can set them manually, like below with green = 100 to show green.
    public int COLOR_RED;
    	public int COLOR_GREEN = 100;
    	public int COLOR_BLUE; 
     
    	Color c = new Color(COLOR_RED, COLOR_GREEN, COLOR_BLUE);

    So in my other class (not the one above) I am trying to set the color with RGB values. My main idea was to use getters/setters for the colors, like getRed, setRed, blue etc. (These are inside the method with the color c = code)

    I'm still just trying to be able to draw a shape and make it a certain color using RGB values.

    Thanks, I hope that makes more sense.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making a universal RGB Color in Swing

    set the color from a different class
    To call a method in a different class, you need a reference to that class:
       refToClass.TheMethod(theValue);  // call method in another class using refToClass
    The value will not be available in the different class until after the above method call is made.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    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

    Ok, that makes more sense, thank you. What about changing the color? Am I calling the getters/setters or something incorrectly?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making a universal RGB Color in Swing

    What about changing the color?
    The color could be created when it is needed from the existing R,G,B values or when a setter changes one of the R,G,B values.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    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

    My idea is that whatever value (between 0 and 255) is input with either of the 3 categories of R,G, or B. So I need to set the RED to user defined, green and blue as well.

    When I try to use my setters for RGB they are not working. What is wrong with my code?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making a universal RGB Color in Swing

    my setters for RGB they are not working
    Please explain what "not working" means?
    Are the values changed when the setters are called
    or do they stay the same as their initial values?

    Can you post all of the code? Just having bits and pieces makes it hard to see what is happening.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    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

    Ok:

     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;
    import java.util.ArrayList;
     
    import javax.swing.JPanel;
     
    import com.herbst.shapes.Circle;
    import com.herbst.shapes.Rectangle;
    import com.herbst.shapes.Triangle;
     
    public class PaintPanel extends JPanel {
    	private String shapeSelected = SHAPE_RECTANGLE;
     
    	private ArrayList<Shape> shapes;
     
    	public int COLOR_RED;
    	public int COLOR_GREEN = 100;
    	public int COLOR_BLUE;
    	//private int colorSelected = COLOR_RED;
    	public static final String SHAPE_RECTANGLE = "rectangle";
    	public static final String SHAPE_CIRCLE = "circle";
    	public static final String SHAPE_TRIANGLE = "triangle";
     
    	//Color red2 = new Color(155,0,0);
    	// private ArrayList<Shape> circles; only need 1 rectangle array
     
    	Color c = new Color(COLOR_RED, COLOR_GREEN, COLOR_BLUE);
    	public PaintPanel() {
    		shapes = new ArrayList<Shape>();
     
    		addMouseMotionListener(new MouseMotionListener() {
    			private Shape drawingShape = null;
     
    			@Override
    			public void mouseMoved(MouseEvent e) {
    				drawingShape = null;
    			}
     
    			@Override
    			public void mouseDragged(MouseEvent e) {
     
    				if (drawingShape == null) {
    					if(shapeSelected.equals(SHAPE_RECTANGLE)) {
    						drawingShape = new Rectangle(e.getX(), e.getY(), c);
    					} else if(shapeSelected.equals(SHAPE_CIRCLE)) {
    						drawingShape = new Circle(e.getX(), e.getY(), c);
    					} else {
    						drawingShape = new Triangle(e.getX(), e.getY(), c);
    					}
     
    					addShape(drawingShape);
    				} else {
    					drawingShape.setWidth(e.getX() - drawingShape.getX());
    					drawingShape.setHeight(e.getY() - drawingShape.getY());
    					repaint();
    				}
    //					 System.out.println("mouse Released" + e.getX() + ", " + e.getY(), Color.BLACK);
    //					drawingShape = new Rectangle(e.getX(), e.getY(), Color.Black);
    //					addShape(drawingShape);
    			}
    		});
    	}
     
    //	
     
     
    	private void addShape(Shape shape) {
    		shapes.add(shape);
    		repaint();
    	}
     
    	// Called automatically
    	@Override
    	protected void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		for (Shape shape : shapes) {
    			shape.paint(g);
    		}
     
    	}
     
    	public String getShapeSelected() {
    		return shapeSelected;
    	}
     
    	public void setShapeSelected(String shapeSelected) {
    		this.shapeSelected = shapeSelected;
    	}
    	public int getRed() {
    		return COLOR_RED;
    	}
     
    	public void setRed(int red) {
    		this.COLOR_RED = red;
    	}
     
    	public int getGreen() {
    		return COLOR_GREEN;
    	}
     
    	public void setGreen(int green) {
    		this.COLOR_GREEN = green;
    	}
     
    	public int getBlue() {
    		return COLOR_BLUE;
    	}
     
    	public void setBlue(int blue) {
    		this.COLOR_BLUE = blue;
    	}
    }

    The next code is where I'm trying to set color at, it has my UI stuff(problem code at bottom with radio buttons of RGB :

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.ButtonGroup;
    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);
    		radioButtons();
     
    	}
     
     
    	public void radioButtons() {
     
    //	lblChooseColors = new JLabel("Choose color#'s");
    //	paintPanel.add(lblChooseColors);
    //	
    //	txtRed = new JTextField();
    //	txtRed.setText("Red");
    //	paintPanel.add(txtRed);
    //	txtRed.setColumns(10);
    //	
    //	txtGreen = new JTextField();
    //	txtGreen.setText("Green");
    //	paintPanel.add(txtGreen);
    //	txtGreen.setColumns(10);
    //	
    //	txtBlue = new JTextField();
    //	txtBlue.setText("Blue");
    //	paintPanel.add(txtBlue);
    //	txtBlue.setColumns(10);
     
    	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);
     
    	JRadioButton rdbtnGreen = new JRadioButton("Green",true);
    	paintPanel.add(rdbtnGreen);
    	JRadioButton rdbtnRed = new JRadioButton("Red", false);
    	paintPanel.add(rdbtnRed);
    	JRadioButton rdbtnBlue = new JRadioButton("Blue", false);
    	paintPanel.add(rdbtnBlue);
     
    	ButtonGroup Gcolors = new ButtonGroup();
    	Gcolors.add(rdbtnGreen);
    	Gcolors.add(rdbtnRed);
    	Gcolors.add(rdbtnBlue); 
     
    //	txtGreen.addActionListener(new ActionListener() {
    //
    //		@Override
    //		public void actionPerformed(ActionEvent arg0) {
    //			// TODO Auto-generated method stub
    //		//	if(txtGreen.isSelected()) {
    //		//		PaintPanel.setColor(Color.GREEN);
    //			}
    //	
    //		
    //	});
    	rdbtnRed.addActionListener(new ActionListener() {
     
    		@Override
    		public void actionPerformed(ActionEvent arg0) {
    			// TODO Auto-generated method stub
    		paintPanel.setRed(0);
    		}
     
    	});
     
    	rdbtnGreen.addActionListener(new ActionListener() {
     
    		@Override
    		public void actionPerformed(ActionEvent arg0) {
    			// TODO Auto-generated method stub
    			paintPanel.setGreen(0);
    		}
     
    	});
    	rdbtnBlue.addActionListener(new ActionListener() {
     
    		@Override
    		public void actionPerformed(ActionEvent arg0) {
    			// TODO Auto-generated method stub
    			paintPanel.setBlue(100);
    		}
     
    	});
     
     
    	}
    }

    There are some more classes, but I do not think it's relevant.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making a universal RGB Color in Swing

    There are some missing classes that are needed to compile and test the code.

    The value of c is set when the class is created. I do not see where its value is ever changed from its initial value.

    Note: a single letter variable name (like c) makes it very hard to do a search for where the variable is used. Unique names like myColor is much easier to find.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    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

    Ok, here is the rest of it:

    (I will change the variable name c in my code, thanks)

     
    import java.awt.Color;
    import java.awt.Graphics;
     
    public abstract class Shape {
     
     
    	private int x;
    	private int y;
    	private int width;
    	private int height;
    	private Color color;
     
    //	private String size;
    	public abstract void paint(Graphics g);
     
    	public Color getColor() {
    		return color;
    	}
     
    	public void setColor(Color color) {
    		this.color = color;
    	}
     
    	public int getY() {
    		return y;
    	}
     
    	public void setY(int y) {
    		this.y = y;
    	}
     
    	public int getX() {
    		return x;
    	}
     
    	public void setX(int x) {
    		this.x = x;
    	}
     
    	public void Rectangle(int x, int y) {
    		this.x = x;
    		this.y = y;
    	}
     
    	public int getHeight() {
    		return height;
    	}
     
    	public void setHeight(int height) {
    		this.height = height;
    	}
     
    	public int getWidth() {
    		return width;
    	}
     
    	public void setWidth(int width) {
    		this.width = width;
    	}
     
     
    }

    import java.awt.Color;
    import java.awt.Graphics;
     
     
    public class Circle extends Shape {
     
    	public Circle(int x, int y, Color color) {
    		setX(x);
    		setY(y);
    		setColor(color);
    		setWidth(0);
    		setHeight(0);
    	}
    	@Override
    	public void paint(Graphics g) {
    		g.setColor(getColor());
    		g.fillOval(getX(), getY(), getWidth(), getHeight());
     
    	}
     
    }
     import java.awt.Color;
    import java.awt.Graphics;
     
     
    //maybe create new package for all shapes
    public class Rectangle extends Shape {
     
    	public Rectangle(int x, int y, Color color) {
    		setX(x);
    		setY(y);
    		setColor(color);
    		setWidth(0);
    		setHeight(0);
     
    	}
     
     
    public void paint(Graphics g)  {
    	g.setColor(getColor());
    	g.fillRect(getX(), getY(), getWidth(), getHeight());
    }
     
     
    }
    import java.awt.Color;
    import java.awt.Graphics;
     
     
    //maybe create new package for all shapes
    public class Triangle extends Shape {
     
    	public Triangle(int x, int y, Color color) {
    		setX(x);
    		setY(y);
    		setColor(color);
    		setWidth(0);
    		setHeight(0);
     
    	}
     
    public void paint(Graphics g)  {
     
    	int[] xPoints = {getX()	, getY(), 3};
    	int[] yPoints = {getX(), getY(), 3};
    	g.setColor(getColor());
    	g.fillPolygon(xPoints,yPoints, xPoints.length);
    }
     
     
    }
     
    public class Main {
     
    	public static void main(String[] args) {
     
    		MainFrame frame = new MainFrame();
    		frame.setVisible(true);
     
    	}
     
    }

    Thanks.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making a universal RGB Color in Swing

    Can you answer these questions I asked earlier?

    my setters for RGB they are not working
    Please explain what "not working" means?
    Are the values changed when the setters are called
    or do they stay the same as their initial values?

    See post#10.


    The code compiles now.
    What is a user supposed to do with it?
    What is supposed to happen?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    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

    Are you confused with what the program is trying to do?
    So I am trying to set COLOR_RED, COLOR_GREEN or COLOR_BLUE to any user-set value between 0 and 255. No they are not getting changed. Basically the program is just drawing shapes and then being able to change the shapes colors. I am trying to change the values of
     Color c = new Color(COLOR_RED, COLOR_GREEN, COLOR_BLUE);
    after a setter changes it with an appropriate value (between 0 and 255)

    Does that explanation help a bit?

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making a universal RGB Color in Swing

    Can you answer these questions I asked earlier?

    my setters for RGB they are not working
    Please explain what "not working" means?
    Are the values changed when the setters are called
    or do they stay the same as their initial values?


    c is given a value ONE time when the class instance is created. If you want its value to change, you must assign it a new value.

    --- Update ---

    Are you confused with what the program is trying to do?
    I am confused about what a user is supposed to do with the program.
    Can you list the steps a user should take to run the program?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    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

    OK, sure. I am trying to use setRed, setGreen and setBlue to set the corresponding values of COLOR_RED, COLOR_BLUE, COLOR_GREEN which controls the statement that sets the color for the rectangle, circle or triangle. I would LIKE the COLOR_RED value for instance to be changed when the setters are called, but they are not changing. They are staying the same.

    With the setters I am trying to change the value of c. - I am under the assumption that the declaration of the Color c is declared but the arguments (COLOR_RED,COLOR_GREEN,COLOR_BLUE) can be changed at any time with a setter like setRed.

    If a user runs the program, they can select whether to draw a square, circle, or triangle(I am still working on the triangle part) THen they also select a color they want to draw it in. Like 255 in the red area, 0 in the green area and 0 in the blue area makes the shape red. They can draw as many as they like in any appropriate color that they selected.

    Does that help any? Sorry I am being confusing.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making a universal RGB Color in Swing

    The code needs to assign a new value to the myColor (renamed c variable) every time the values of COLOR_RED,COLOR_GREEN,COLOR_BLUE are changed.
    Changing COLOR_RED,COLOR_GREEN,COLOR_BLUE will NOT change the value of myColor(c).

    Why don't the set methods change the value of myColor(c) directly?
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    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

    Ok, I see what you are saying. Within the "user interface" (The code with the radio buttons) I can put this for the red radio button:
     paintPanel.myColor = new Color(200, 0, 0);
    So I am really planning on using JTextField fields to get the numbers, and I could just use input from there to set the appropriate color? I think I understand you. I will try to program it up real quick and see how it goes. Hopefully it works. A sincere thanks for the help!

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making a universal RGB Color in Swing

    I can put this for the red radio button:
    No, use a setter method. Do not directly change the values of variables in another class.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    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'm having trouble with that then.

    public int getRed() {
    		return COLOR_RED;
    	}
     
    	public void setRed(int red) {
    		this.COLOR_RED = red;
    	}

    I'll need something like this somewhere in my getters/setters: myColor = new Color(200, 0, 0); right?

    How would I go about changing the color?

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making a universal RGB Color in Swing

    There may be a problem with your logic. An RGB color is built from the three values in R,G and B. Your code is only setting one of the variables. What about the other 2?
    Also where is the value of myColor(c) set? Changing the value of COLOR_RED does not change the value in myColor.

    When someone clicks one of color radio buttons, what do you want to happen?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Angry Re: Making a universal RGB Color in Swing

    I am trying to change them individually (not all three values at once) because I actually need them to get values from a JTextField. The radio buttons were just training wheels and I have failed.

    I have set up formatted text fields for the 3 colors to get the int values from.

    You lost me with the last setters/getters post. I must not be understanding how to use them correctly. Within my color getter/setters do I need the myColor = new Color(200, 0, 0);? You said just changing COLOR_RED doesnt do anything so I have to change it that way, right?

    Am I getting closer?

Page 1 of 2 12 LastLast

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