Ok I’m really not understanding why this isn’t working.
class SketcherModel extends Observable implements Iterable<Element>{
public Iterator<Element> iterator(){
return elements.iterator();
}
public LinkedList<Element> elements = new LinkedList<Element>();
}
So I have a LinkedList in the class Element in the ‘model’ portion of my application. The idea is that when the ‘view’ portion of the app executes the paint() method it’ll access the linked list and draw each Element it contains, as shown in the code below:
public class SketcherView extends JComponent implements Observer{
public SketcherView(Sketcher theApp){
this.theApp = theApp;
//Testing to see if I can access the linked list, 'elements', outside of the paint() method;
Element test = theApp.getModel().elements.iterator().next()
}
public void update(Observable o, Object rectangle){
//Code to respond to changes in model...
}
public void paint(Graphics g){
Graphics2D g2D = (Graphics2D)g;
for(Element element : theApp.getModel()){
element = (Element)elements.iterator().next();
g2D.setPaint(element.getColor());
g2D.draw(element.getShape());
}
}
Now when accessing the linked list elements from the class constructor it seems to find it with no problems , however when I used the collection-based for loop to run through elements using the iterator it says ‘elements cannot be resolved’. Am I missing something?
You don’t have any
elementsvariable in the method. And the current element of the loop is already declared in the for loop. The code should beAnother, unrealated problem, is that you should override
paintComponent(), and notpaint().