linking frames

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • OCD
    New Member
    • Feb 2007
    • 9

    linking frames

    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
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by OCD
    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
    When a color is selected just do a g.setColor and the set the color that was selected.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by OCD
      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:

      [code=java]
      public interface SelectedColour {
      public void setSelectedColo ur(Color colour);
      public Color getSelectedColo ur();
      }
      [/code]

      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:

      [code=java]
      public YourFrame extends JFrame implements SelectedColour {
      private Color colour= Color.BLACK; // default value
      ...
      // interface implementation
      public void setSelectedColo ur(Color colour) { this.colour= colour; }
      public Color getSelectedColo ur() { return colour; }
      }
      [/code]

      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

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by JosAH
        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:

        [code=java]
        public interface SelectedColour {
        public void setSelectedColo ur(Color colour);
        public Color getSelectedColo ur();
        }
        [/code]

        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:

        [code=java]
        public YourFrame extends JFrame implements SelectedColour {
        private Color colour= Color.BLACK; // default value
        ...
        // interface implementation
        public void setSelectedColo ur(Color colour) { this.colour= colour; }
        public Color getSelectedColo ur() { return colour; }
        }
        [/code]

        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
        A neat implementation indeed.

        Comment

        Working...