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.