I made a small program using several MVC-patterns. So far I havnt got to much stuff to put in the model so I havnt got any model yet.
My idea is to make one MVC pattern for each panel. And looping through it in the mainloop in the MainController. Then I communicate (starting and stoping) with the MainLoop in the MainController passing the MainController as an parameter to the "subcontrollers" PanOneController and PanTwoController.
Like this:
Main | MainView | MainController (mainloop)
PanelOne Model | PanelOne View | PanelOne Controller
PanelTwo Model | PanelTwo View | PanelTwo Controller
Would this be considered as a good structure? Or some other suggestions of the structure?
This is just an exemple. Im going to add more panels and functions but just want to get the structure right.
Thanks for feedback!
MAIN
import javax.swing.SwingUtilities;
public class Main {
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MainView theView = new MainView();
MainModel theModel = new MainModel();
MainController theController = new MainController(theView, theModel);
theView.setVisible(true);
}
});
}
}
VIEW
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import panelTwo.PanTwoView;
import panelOne.PanOneView;
public class MainView extends JFrame {
PanOneView oneV = new PanOneView();
PanTwoView twoV = new PanTwoView();
JPanel panelOne = oneV;
JPanel panelTwo = twoV;
public MainView() {
setLayout(new FlowLayout(FlowLayout.LEFT, 100, 10));
setSize(new Dimension(300, 200));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(panelOne);
this.add(panelTwo);
}
public PanOneView getPanelOne() {
return oneV;
}
public PanTwoView getPanelTwo() {
return twoV;
}
}
CONTROLLER
import panelOne.*;
import panelTwo.*;
public class MainController implements Runnable {
private Thread thread;
private boolean running = false;
MainView theView;
MainModel theModel;
PanOneModel oneM = new PanOneModel();
PanTwoModel twoM = new PanTwoModel();
PanOneController oneC;
PanTwoController twoC;
private int inc = 0;
public MainController(MainView x, MainModel y) {
this.theView = x;
this.theModel = y;
thread = new Thread(this);
oneC = new PanOneController(oneM, x.getPanelOne(), this);
twoC = new PanTwoController(twoM, x.getPanelTwo(), this);
}
public synchronized void start() {
if (!running) {
thread = new Thread(this);
thread.start();
}
running = true;
}
public synchronized void stop() {
thread.interrupt();
running = false;
}
// mainloop
public void run() {
boolean shifter = false;
long timer = 0;
while (running) {
if (System.nanoTime() - timer >= 1000000) {
timer = System.nanoTime();
inc++;
if (inc % 1000 == 0) {
shifter = !shifter;
}
oneC.run(shifter);
twoC.run(shifter);
}
}
}
}
PANELONE VIEW
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class PanOneView extends JPanel {
private JLabel jl = new JLabel("PANEL 1");
private JButton start = new JButton("Start");
public PanOneView() {
this.setPreferredSize(new Dimension(100, 50));
this.setBackground(Color.red);
jl.setForeground(new java.awt.Color(255, 255, 255));
add(jl);
add(start);
}
public void addTheActionListeners(ActionListener theListener) {
start.addActionListener(theListener);
}
public JButton getStart() {
return start;
}
public void setBg(boolean shifter) {
if(shifter) {
this.setBackground(Color.red);
} else {
this.setBackground(Color.green);
}
}
}
PANELONE CONTROLLER
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import nwtrescontroller301.*;
public class PanOneController {
PanOneView oneV;
PanOneModel oneM;
MainController main;
public PanOneController(PanOneModel m, PanOneView v, MainController main_) {
this.oneM = m;
this.oneV = v;
this.main = main_;
oneV.addTheActionListeners(theListener);
}
public void run(boolean shift) {
oneV.setBg(shift);
}
ActionListener theListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == oneV.getStart()) {
main.start();
}
}
};
}
PANELTWO VIEW
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class PanTwoView extends JPanel {
JLabel jl = new JLabel("PANEL 2");
JButton stop = new JButton("Stop");
public PanTwoView() {
this.setPreferredSize(new Dimension(100, 50));
this.setBackground(Color.black);
jl.setForeground(new java.awt.Color(255, 255, 255));
add(jl);
add(stop);
}
public void addTheActionListeners(ActionListener theListener) {
stop.addActionListener(theListener);
}
public JPanel getPanelTwo() {
return this;
}
public JButton getStop() {
return stop;
}
public void setBg(boolean shift) {
if(shift) {
this.setBackground(Color.black);
} else {
this.setBackground(Color.blue);
}
}
}
PANELTWO CONTROLLER
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import nwtrescontroller301.*;
public class PanTwoController {
PanTwoView twoV;
PanTwoModel twoM;
MainController mcon;
public PanTwoController(PanTwoModel m, PanTwoView v, MainController mcon_) {
this.twoV = v;
this.twoM = m;
this.mcon = mcon_;
twoV.addTheActionListeners(theListener);
}
public void run(boolean shift) {
twoV.setBg(shift);
}
ActionListener theListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == twoV.getStop()) {
mcon.stop();
System.out.println("STOP");
}
}
};
}