Results 1 to 3 of 3
- 05-23-2010, 12:50 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 57
- Rep Power
- 0
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:
Any ideas...?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) { } }
- 05-23-2010, 01:43 PM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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:
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.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 } } }
- 06-01-2010, 09:37 AM #3
Member
- Join Date
- Apr 2010
- Posts
- 57
- Rep Power
- 0
Similar Threads
-
Draw circles, select circles
By cselic in forum Java 2DReplies: 2Last Post: 05-17-2010, 02:02 PM -
How to Determine prime number and making shape using loop?
By cyzash in forum New To JavaReplies: 10Last Post: 02-20-2010, 08:25 PM -
How to draw in a JPanel using Netbeans 6.1 GUI maker
By Gatts79 in forum New To JavaReplies: 4Last Post: 08-28-2009, 06:50 PM -
[SOLVED] 2 D Array and JComboBox on a JPanel
By crazydeo in forum New To JavaReplies: 7Last Post: 05-23-2008, 09:17 AM -
Draw on JPanel, Help
By carl in forum Java 2DReplies: 1Last Post: 07-31-2007, 06:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks