Creating a new JComponent

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rpm27
    New Member
    • Jan 2008
    • 23

    Creating a new JComponent

    Hey, I'm trying to create a small program which displays several circles across the screen. However, I intend for these circles to be clickable and change color, size... on different mouse events on each object. So in order for the Class circle to be added to a JPanel and to register events, it should extend the JComponent class. Of course not JComponent since it is abstract. So which one? JPanel, JLabel... Thank you.
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by rpm27
    Hey, I'm trying to create a small program which displays several circles across the screen. However, I intend for these circles to be clickable and change color, size... on different mouse events on each object. So in order for the Class circle to be added to a JPanel and to register events, it should extend the JComponent class. Of course not JComponent since it is abstract. So which one? JPanel, JLabel... Thank you.
    Are those number of circles are fixed?
    When you click those circles, how would you choose to set it's color? by a ColorChooser?
    or a patterned color... ( Set )....
    Are those circles are changing it's position in every event?

    instead of several circles across the screen, have you tried 1 circle across the screen? (Program)

    Please be more specific....
    Our experts here are happy to help you if your problem have the supported complete details....

    sukatoa

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by rpm27
      Hey, I'm trying to create a small program which displays several circles across the screen. However, I intend for these circles to be clickable and change color, size... on different mouse events on each object. So in order for the Class circle to be added to a JPanel and to register events, it should extend the JComponent class. Of course not JComponent since it is abstract. So which one? JPanel, JLabel... Thank you.
      Why would you want a circular JComponent like thing? Why not create a, say,
      CircleHandler, than can contain and manage Circles? That handler can extend
      from a JPanel class and store Circles. The JPanel can handle mouse events and
      decide which Circle the mouse was on. Deal with it from there: dragging, resizing
      and whatever you have in mind with those Circles. At every paintComponent( )
      call for the extended JPanel all you have to do is redraw those Circles.

      kind regards,

      Jos

      Comment

      • rpm27
        New Member
        • Jan 2008
        • 23

        #4
        I didn't realize that my post wasn't clear enough. Here's what I had in mind.
        As a test before I stumbled upon this problem, I already created a class Circle with the following methods:
        Code:
         void setColor(Color c)
        void setSize(int x)
        void setPosition(int x, int y)
        void setActive(boolean b)
        void draw(Graphics g)
        My original idea was to create a class which extends JPanel, and have as instance variable an array of circles (which will be initialized with 5 circles).
        In addition, I wanted to include a Button, which when clicked, will activate the next circle (which means make it able to respond to events), while disactivating the current circle, by iterating through the array.

        I thought about implementing JosAH solution, but I had some problems. For example, I want the circle to be highlighted (ie change its color) when you put the mouse above it (using mouseEntered() method). I can't imagine how the program will know which circle was on focus if the circle isn't itself a component. In addition, event handling should take in consideration the active status of each circle, which made me realize that with this additional complexity, circle events should be handled by the Circle class and not the JPanel.

        I hope this time I was clear enough. I appreciate your input.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by rpm27
          I thought about implementing JosAH solution, but I had some problems. For example, I want the circle to be highlighted (ie change its color) when you put the mouse above it (using mouseEntered() method). I can't imagine how the program will know which circle was on focus if the circle isn't itself a component. In addition, event handling should take in consideration the active status of each circle, which made me realize that with this additional complexity, circle events should be handled by the Circle class and not the JPanel.
          Failing to implement an idea doesn't mean the idea is bad. The JPanel could
          receive all these events through its listeners, determine which circle was pointed
          at and delegate all the 'business logic' i.e. 'what needs to be done' to the appropriate
          circle. That moves all the 'circle functionality' to the Circle class just as you wanted.

          The JPanel just acts as a 'messenger', it delegates everything to the circles
          and offers its own functionality being a JComponent for the circles.

          kind regards,

          Jos

          Comment

          • rpm27
            New Member
            • Jan 2008
            • 23

            #6
            Originally posted by JosAH
            Failing to implement an idea doesn't mean the idea is bad. The JPanel could
            receive all these events through its listeners, determine which circle was pointed
            at and delegate all the 'business logic' i.e. 'what needs to be done' to the appropriate
            circle. That moves all the 'circle functionality' to the Circle class just as you wanted.

            The JPanel just acts as a 'messenger', it delegates everything to the circles
            and offers its own functionality being a JComponent for the circles.

            kind regards,

            Jos
            I understood that (I've already written something similar), but when I create an event handler class which implements MouseListener, and I write a method mouseEntered(), how the program will know that the mouse entered this particular circle so I can change its state to Active?

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by rpm27
              I understood that (I've already written something similar), but when I create an event handler class which implements MouseListener, and I write a method mouseEntered(), how the program will know that the mouse entered this particular circle so I can change its state to Active?
              Something like |m-c| <= r_c where m is the mouse coordinates, c is the center
              of the circle and r_c is the radius of the circle. You have to figure out what to
              do with two or more overlapping circles, but that's a geometry problem, not a
              classes/components problem. Maybe all the circles for which the test succeeds
              get active? It's up to you ...

              kind regards,

              Jos

              Comment

              • rpm27
                New Member
                • Jan 2008
                • 23

                #8
                Originally posted by JosAH
                Something like |m-c| <= r_c where m is the mouse coordinates, c is the center
                of the circle and r_c is the radius of the circle. You have to figure out what to
                do with two or more overlapping circles, but that's a geometry problem, not a
                classes/components problem. Maybe all the circles for which the test succeeds
                get active? It's up to you ...
                But, what if it's not a circle? Maybe some irregular shape, or a silhouette? I know I'm testing you patience :-) but I'd like to put my question in another way. I'd like to create a new button, with a look and appearance that I specify. Anyways, I'll try to extend class JComponent, and use paintComponent( Graphic g) to specify how it looks like. Thank you.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  You're changing the problem statement. Those non-circles didn't exist in your
                  original post.

                  kind regards,

                  Jos

                  Comment

                  • rpm27
                    New Member
                    • Jan 2008
                    • 23

                    #10
                    Originally posted by JosAH
                    You're changing the problem statement. Those non-circles didn't exist in your
                    original post.
                    This thread seems to get annoying :-) Anyways I don't actually have time to test these solutions. When I'll try them, I'll post more questions . Thanks again

                    Comment

                    Working...