Need help finding bug in this if statement
Disclaimer: this is a (frustrating) homework related problem.
I'm having odd results when I draw my objects on screen. I want this...I draw first object then draw second object when I select third object to draw the screen clears and I have to start the process again....what i get is...I draw first object, I draw second object, I go to draw third object screen clears...I select third object but SECOND object is what appears on screen. Please help point me in right direction.
package ui.panels;
import java.awt.Choice;
import java.awt.Panel;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import model.Model;
import interfaces.Resettable;
public class ChoicePanel extends Panel implements Resettable{
public int i = 0;
/**
*
*/
private static final long serialVersionUID = 1L;
Model model;
Choice selection;
public ChoicePanel(Model mdl) {
model = mdl;
selection = new Choice();
for (String msg : Model.selections) {
selection.add(msg);
}
selection.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(i==1) {//drop down clicked three times)
System.out.println("ChoicePanel says i == "+i);
model.setMessage(selection.getSelectedItem());
model.setCurrentShapeType(selection.getSelectedItem());
//model.repaint();
++i;
}else if(i==2){
System.out.println("ChoicePanel says i == "+i);
model.setMessage(selection.getSelectedItem());
开发者_高级运维 //model.setCurrentShapeType(selection.getSelectedItem());
model.resetComponents();
//--i;
}else{
model.setMessage(selection.getSelectedItem());
//this line is what sends a value to shape that is drawn on screen
model.setCurrentShapeType(selection.getSelectedItem());
//model.repaint();
++i;
}
}
});
this.add(selection);
}
public void resetComponents() {
System.out.println("resetComponents from ChoicePanel");
//this resets the drop down list selection array to the first choice on the list
selection.select(0);
//this sets selected item in the selection array set in the above line
//model.setMessage(selection.getSelectedItem());
i=0;
model.repaint();
}
}
Within else if(i==2){ ... }
you don't increment i
, it'll never get past 2
.
} else if(i==2){
System.out.println("ChoicePanel says i == "+i);
model.setMessage(selection.getSelectedItem());
//model.setCurrentShapeType(selection.getSelectedItem());
model.resetComponents();
++i; // Increment here
}
精彩评论