Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java2D and MVC  RSS feed

 
Nicole Gruber
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this asignment to create a Java SWING application, which uses the Java2D functionality. The user should be able to draw points on the panel and by pushing the Create Polygon button to unite the points into a polygon. I'm not able to create the Points and I am also confused by the MVC Pattern, wich we are obliged to use! Can someone help me???








 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We usually discuss Swing on a different forum. Moving.
 
Chris Nash
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! So much code! Let's narrow this down. What works? Then, where do things start to fall apart?

 
Nicole Gruber
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What works: the first time when the user clicks somewhere on the panel, that point is added in the Model-class, which notifices the Observer(View-class) [I omitted to write in the code
]
and finally the point is being drew through the paint() Method .
the Problem is that once you click twice on the Panel, the second point doesn't appear anymore...
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made changes to your code and it can now draw the points in your app.
I had to add the implementation for ChangeListener to get your code to compile.

One problem I encountered was with the Model entry method

Try this instead

In the MyDrawPanel class, pass in a reference to the Model at construction. No need to change the model each time there is a user change; the Controller class will make the change, update the Model and tell MydrawPanel to repaint itself. The MyDrawPanel class queries the Model for the current data.

About the Observer/Observable paradigm: the old/original way was to extend Observer and implement Observable. After the designers thought about it for a bit they came up with the idea of registering event listeners with components. So you can do your app with the antique Observer/Observable mechanism or use the current/newer event listener approach, which you already have in the Controller class.

You have your code encapsulated into classes according to the mvc design well enough as–is.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!