Hi I need to create two frames in java. One that is a color palette and one that you can load an image into. Then when you click on one of the colors in the color palette frame, you have to be able to draw in that color in the other frame. How can I link these two frames so that the color can be used for drawing???
thanks
I normally build a few interfaces for that purpose. Your color palette wants to
set a selected colour; it needs a SelectedColour object for that; here it is:
-
public interface SelectedColour {
-
public void setSelectedColour(Color colour);
-
public Color getSelectedColour();
-
}
-
Your other frame needs a colour to be selected so it should implement the
interface above; as you wrote it's a JFrame, so here goes:
-
public YourFrame extends JFrame implements SelectedColour {
-
private Color colour= Color.BLACK; // default value
-
...
-
// interface implementation
-
public void setSelectedColour(Color colour) { this.colour= colour; }
-
public Color getSelectedColour() { return colour; }
-
}
-
Your palette object couldn't care less that the SelectedColour object is a JFrame,
as long as it can set a selected colour. The above scenario takes care of it.
Just pass YourFrame as a (constructor?) parameter to your palette object.
kind regards,
Jos