Results 1 to 3 of 3
  1. #1
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Question To draw circles on JPanel using JComboBox to determine number of them

    I am making JApplet which has JPanel on it. I have JComboBox on JApplet (outside JPanel) and I should use it to select number of circles that will be drawn on JPanel. For example, if I choose 2, the JPanel should get 2 circles.

    How can I access the paintComponent method inside JPanel and to "connect" it with JCombobox?

    I have written the comboItemStateChanged method, but I don't know what to put in it. Here is how it looks:

    Java Code:
     private void comboItemStateChanged(java.awt.event.ItemEvent evt) {
        int selection;
        selection = combo.getSelectedIndex();
        if (selection == 0) {
        }
        else if (selection == 1) {
        }
        else if (selection == 2) {
        }
          else if (selection == 3) {
        }
          else if (selection == 4) {
        }
    }
    Any ideas...?

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    I think that you'll need a controller (the listener such as an item listener) that is outside of the two classes, the one holding the JComboBox and the one drawing the circles. Perhaps the class holding the JComboBox can have a public method called addItemListener(ItemListener il) and inside this method you add the parameter to the JComboBox as its ItemListener:

    Java Code:
    // warning: code neither tested nor compiled 
    class ComboDisplayer {
      private JComboBox myComboBox = ....
    
      public void addItemListener(ItemListener il) {
        myComboBox.addItemListener(il);
      }
    
      //.... other code
    
    }

    Then similarly for the JPanel that does the painting allows the controller to change the number of circles by giving it a private int field, say circleCount, and a public method setCircleCount(int count), and within this method set the circleCount variable and tell the JPanel to repaint. Have this JPanel use the circleCount field to determine how many circles to draw:

    Java Code:
    // warning: code neither tested nor compiled 
    class DrawCircles extends JPanel {
      private int circleCount = 0;
    
      public void setCircleCount(int circleCount) {
        this.circleCount = circleCount;
        repaint();
      }
    
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (int i = 0; i < circleCount; i++) {
           // code for drawing circles
        }
    
      }
    }
    Then in the class that holds the two classes above, you create an ItemListener that listens for changes in the combobox and uses the changes to set the number of circles. Note that there will be no need for if blocks here since you'll simply be plugging the number obtained from the combobox into the circle drawing class.

  3. #3
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

Similar Threads

  1. Draw circles, select circles
    By cselic in forum Java 2D
    Replies: 2
    Last Post: 05-17-2010, 03:02 PM
  2. Replies: 10
    Last Post: 02-20-2010, 09:25 PM
  3. Replies: 4
    Last Post: 08-28-2009, 07:50 PM
  4. [SOLVED] 2 D Array and JComboBox on a JPanel
    By crazydeo in forum New To Java
    Replies: 7
    Last Post: 05-23-2008, 10:17 AM
  5. Draw on JPanel, Help
    By carl in forum Java 2D
    Replies: 1
    Last Post: 07-31-2007, 07:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •