I have a project based on the Model-View-Controller paradigm, and I'm having trouble working correctly.
The program has 4 panels, which are supposed to allow me to change the oval pattern on the screen in various ways. It seems that they are working fine, and after serious troubles, I was able to show them in a JFrame that contains the whole shebang. I managed to display them, breaking away from the instructions provided, but when I do this, I can not get the oval to be updated. However, if I follow the directions to the letter, I see only an empty frame.
The project had rather specific directions, which I followed up to a certain point, but some of the documentation was unclear. I think that what I am missing should be something simple, since nothing pops up at me as if it makes no sense. I must admit that my Java experience is limited, and my experience with the GUI / paradigms is even greater.
In any case, I searched on the Internet, and this site widely tried to understand what was wrong, but this is a somewhat concrete example, and to be honest, I just donβt know enough about it to summarize any answers that I have find online and find out that is missing. I've been looking at this code for too long, so I really hope someone can help me.
public class Model { private Controller controller; private View view; private MvcFrame mvcFrame; private int radius = 44; private Color color = Color.BLUE; private boolean solid = true;
Here is the model class. It seems pretty simple. I think that my understanding of what is happening here is solid and nothing seems wrong. Included mainly for context.
public class Controller extends JPanel{ private Model model; public Controller(Model model) { this.model = model; setBorder(BorderFactory.createLineBorder(Color.GREEN)); setLayout(new GridLayout(4,1)); add(new RadiusPanel(model)); add(new ColorPanel(model)); add(new SolidPanel(model)); add(new TitlePanel(model)); } }
This is the controller class. As far as I can tell, setBorder, setLayout and a series of additives do nothing here. I commented on them, but this is how the instructions said that I was doing something, so either there is an error or something in my setting is incorrect. However, when I did it this way, I would get an empty window (JFrame), but none of the panels appeared in it. What I did to fix this, add the add functions to the mvcFrame class:
public class MvcFrame extends JFrame { private Model model; public MvcFrame(Model model){ this.model = model;
So, here, when everything starts to get weird. The first block of code with comments is the same as in the Controller class. The reason I commented on it was that it was just a good assumption - it should not be as indicated in the instructions. However, this worked in order to show the panels, but at that moment I was still tearing my hair, trying to display the oval.
Another commented line (add (new view (model));) was another attempt to make everything work. In this case, I put these add functions in the View class (see Code with comments below). This actually showed both the oval and the panels, but this method would not allow me to update the oval. In addition, although I only had an oval display, I cannot understand what exactly happened, and I cannot make it return.
public class View extends JPanel{ private Model model; public View(Model model) { this.model = model; //setLayout(new GridLayout(4,1)); //add(new RadiusPanel(model)); //add(new ColorPanel(model)); //add(new SolidPanel(model)); //add(new TitlePanel(model)); repaint(); } @Override protected void paintComponent(Graphics g){ super.paintComponent(g); //center of view panel, in pixels: int xCenter = getWidth()/2; int yCenter = getHeight()/2; int radius = model.getRadius(); int xStart = xCenter - radius; int yStart = yCenter - radius; int xWidth = 2 * radius; int yHeight = 2 * radius; g.setColor(model.getColor()); g.clearRect(0, 0, getWidth(), getHeight()); if (model.isSolid()){ g.fillOval(xStart, yStart, xWidth, yHeight); } else { g.drawOval(xStart, yStart, xWidth, yHeight); } } }
A similar idea, as before - commented code - is the material that I added to try to make everything work, but is not based on these directions. In case this material was excavated, I had an addition (new view (model)); line from mvcFrame line also without excavation.
Various panel classes (SolidPanel, ColorPanel, etc.) simply extend the ControlPanel class, which extends JPanel. All of them seem to work as expected, without having a lot of problems with them. There is also a driver that launches a graphical interface. This also works as expected.
The main problem I am facing is that I cannot get an oval to show it, and once I was able to display it, none of the options for changing it worked. I feel like I'm around, but I'm just at a loss for other things to try at this point.
Anyone who can help will receive my sincere thanks.