diff --git a/lab/lab13/Question1/Controller.java b/lab/lab13/Question1/Controller.java new file mode 100644 index 0000000..bf48494 --- /dev/null +++ b/lab/lab13/Question1/Controller.java @@ -0,0 +1,13 @@ +public class Controller { + protected Model m; + + public Controller(Model m) { + this.m = m; + } + + // save data before shutdown + protected void shutdown() { + m.saveData(); + System.exit(0); + } +} diff --git a/lab/lab13/Question1/ControllerClicks.java b/lab/lab13/Question1/ControllerClicks.java new file mode 100644 index 0000000..e4d7d40 --- /dev/null +++ b/lab/lab13/Question1/ControllerClicks.java @@ -0,0 +1,19 @@ +import java.awt.Point; + +public class ControllerClicks extends Controller { + public ControllerClicks(Model m) { + super(m); + } + + public void mouseClicked(Point p) { + m.addPoint(p); + } + + public void resetClicked() { + m.clearAllPoints(); + } + + public void undoClicked() { + m.deleteLastPoint(); + } +} diff --git a/lab/lab13/Question1/Model.java b/lab/lab13/Question1/Model.java new file mode 100644 index 0000000..c24079d --- /dev/null +++ b/lab/lab13/Question1/Model.java @@ -0,0 +1,106 @@ +import java.awt.Point; +import java.util.ArrayList; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +public class Model { + private ArrayList points; + private ArrayList listeners; + + public Model() { + points = new ArrayList(); + listeners = new ArrayList(); + + if (new java.io.File("points.bin").exists()) { + try { + FileInputStream fi = new FileInputStream("points.bin"); + ObjectInputStream in = new ObjectInputStream(fi); + points = (ArrayList) in.readObject(); + in.close(); + fi.close(); + } catch (IOException e) { + System.err.println(e.getMessage()); + System.exit(1); + } catch (ClassNotFoundException e) { + System.err.println(e.getMessage()); + System.exit(1); + } + } + } + + public void addListener(ModelListener l) { + listeners.add(l); + } + + public ArrayList getPoints() { + return points; + } + + public void addPoint(Point p) { + points.add(p); + notifyListeners(); + } + + public void clearAllPoints() { + points.clear(); + notifyListeners(); + } + + public void deleteLastPoint() { + if (points.size() > 0) { + points.remove(points.size() - 1); + notifyListeners(); + } + } + + private void notifyListeners() { + for (ModelListener l : listeners) { + l.update(); + } + } + + public int numberOfPoints() { + return points.size(); + } + + public void saveData() { + + try { + FileOutputStream f = new FileOutputStream("points.bin"); + ObjectOutputStream out = new ObjectOutputStream(f); + out.writeObject(points); + out.close(); + f.close(); + } catch (IOException e) { + System.err.println(e.getMessage()); + System.exit(254); + } + } + + public static void testModel() { + Model m = new Model(); + m.addListener(new ModelListener() { + @Override + public void update() { + System.out.println(true + " (listener)"); + } + }); + System.out.println(m.getPoints() == m.points); + Point p1 = new Point(1, 2); + Point p2 = new Point(3, 4); + m.addPoint(p1); + m.addPoint(p2); + System.out.println(m.numberOfPoints() == 2); + System.out.println(m.points.get(0) == p1); + System.out.println(m.points.get(1) == p2); + m.deleteLastPoint(); + System.out.println(m.numberOfPoints() == 1); + System.out.println(m.points.get(0) == p1); + m.clearAllPoints(); + System.out.println(m.numberOfPoints() == 0); + m.notifyListeners(); + } +} diff --git a/lab/lab13/Question1/ModelListener.java b/lab/lab13/Question1/ModelListener.java new file mode 100644 index 0000000..664e6e5 --- /dev/null +++ b/lab/lab13/Question1/ModelListener.java @@ -0,0 +1,3 @@ +public interface ModelListener { + public void update(); +} diff --git a/lab/lab13/Question1/MyFrame.java b/lab/lab13/Question1/MyFrame.java new file mode 100644 index 0000000..14c8a73 --- /dev/null +++ b/lab/lab13/Question1/MyFrame.java @@ -0,0 +1,55 @@ +import java.awt.BorderLayout; +import java.awt.FlowLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.JButton; +import javax.swing.JPanel; + +public class MyFrame extends View { + + public MyFrame(Model m, ControllerClicks c) { + super(m, c); + this.setTitle("MyFrame Title"); + this.setSize(400, 300); + this.setLocationRelativeTo(null); + this.setLayout(new BorderLayout()); + + // layout + + MyPanel centerPanel = new MyPanel(m, c); + this.add(centerPanel, BorderLayout.CENTER); + + JPanel topPanel = new JPanel(); + this.add(topPanel, BorderLayout.PAGE_START); + + // top panel + topPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); + + JButton resetButton = new JButton("Reset"); + resetButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + c.resetClicked(); + } + }); + + JButton undoButton = new JButton("Undo"); + undoButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + c.undoClicked(); + } + }); + + topPanel.add(resetButton); + topPanel.add(undoButton); + + // final + this.setVisible(true); + } + + @Override + public void update() { + repaint(); + } +} diff --git a/lab/lab13/Question1/MyPanel.java b/lab/lab13/Question1/MyPanel.java new file mode 100644 index 0000000..0ce9ad3 --- /dev/null +++ b/lab/lab13/Question1/MyPanel.java @@ -0,0 +1,49 @@ +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Point; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.ArrayList; +import javax.swing.JPanel; + +public class MyPanel extends JPanel { + + private Model m; + private ControllerClicks c; + + public MyPanel(Model m, ControllerClicks c) { + this.m = m; + this.c = c; + + this.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + if (e.getButton() == MouseEvent.BUTTON1) { + c.mouseClicked(e.getPoint()); + } + } + }); + } + + @Override + protected void paintComponent(Graphics g) { + + // Draw the background + super.paintComponent(g); + // Draw the grid + ArrayList points = m.getPoints(); + g.setColor(Color.RED); + // Draw the points + if (points.size() == 1) { + Point p = points.get(0); + g.drawRect((int) p.getX(), (int) p.getY(), 1, 1); + } else { + // Draw the lines + for (int i = 1; i < points.size(); i++) { + Point start = points.get(i - 1); + Point end = points.get(i); + g.drawLine((int) start.getX(), (int) start.getY(), (int) end.getX(), (int) end.getY()); + } + } + } +} diff --git a/lab/lab13/Question1/Start.java b/lab/lab13/Question1/Start.java new file mode 100644 index 0000000..f9ba330 --- /dev/null +++ b/lab/lab13/Question1/Start.java @@ -0,0 +1,14 @@ +public class Start { + public static void main(String[] args) { + javax.swing.SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + Model model = new Model(); + ControllerClicks c1 = new ControllerClicks(model); + MyFrame frame = new MyFrame(model, c1); + Controller c2 = new Controller(model); + ViewNumber vn = new ViewNumber(model, c2); + } + }); + } +} diff --git a/lab/lab13/Question1/Test.java b/lab/lab13/Question1/Test.java new file mode 100644 index 0000000..dd06f02 --- /dev/null +++ b/lab/lab13/Question1/Test.java @@ -0,0 +1,5 @@ +public class Test { + public static void main(String[] args) { + Model.testModel(); + } +} diff --git a/lab/lab13/Question1/View.java b/lab/lab13/Question1/View.java new file mode 100644 index 0000000..48f7297 --- /dev/null +++ b/lab/lab13/Question1/View.java @@ -0,0 +1,22 @@ +import javax.swing.JFrame; + +public abstract class View extends JFrame implements ModelListener { + + protected Model m; + protected T c; + + public View(Model m, T c) { + this.m = m; + this.c = c; + m.addListener(this); + setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); // hide the frame when the user clicks on the close button + addWindowListener(new java.awt.event.WindowAdapter() { + public void windowClosing(java.awt.event.WindowEvent e) { + c.shutdown(); + } + }); + } + + @Override + public abstract void update(); +} diff --git a/lab/lab13/Question1/ViewNumber.java b/lab/lab13/Question1/ViewNumber.java new file mode 100644 index 0000000..4101df3 --- /dev/null +++ b/lab/lab13/Question1/ViewNumber.java @@ -0,0 +1,21 @@ +import javax.swing.JLabel; + +public class ViewNumber extends View { + + private JLabel label; + + public ViewNumber(Model m, Controller c) { + super(m, c); + this.setTitle("View Number"); + this.setSize(150, 150); + label = new JLabel(); + update(); + this.add(label); + this.setVisible(true); + } + + @Override + public void update() { + label.setText("Number of points is: " + m.numberOfPoints()); + } +} diff --git a/lab/lab13/Question2/Controller.java b/lab/lab13/Question2/Controller.java new file mode 100644 index 0000000..11232e4 --- /dev/null +++ b/lab/lab13/Question2/Controller.java @@ -0,0 +1,12 @@ +public class Controller { + protected Model m; + + public Controller(Model m) { + this.m = m; + } + + protected void shutdown() { + m.saveData(); + System.exit(0); + } +} diff --git a/lab/lab13/Question2/ControllerClicks.java b/lab/lab13/Question2/ControllerClicks.java new file mode 100644 index 0000000..e4d7d40 --- /dev/null +++ b/lab/lab13/Question2/ControllerClicks.java @@ -0,0 +1,19 @@ +import java.awt.Point; + +public class ControllerClicks extends Controller { + public ControllerClicks(Model m) { + super(m); + } + + public void mouseClicked(Point p) { + m.addPoint(p); + } + + public void resetClicked() { + m.clearAllPoints(); + } + + public void undoClicked() { + m.deleteLastPoint(); + } +} diff --git a/lab/lab13/Question2/Model.java b/lab/lab13/Question2/Model.java new file mode 100644 index 0000000..8654887 --- /dev/null +++ b/lab/lab13/Question2/Model.java @@ -0,0 +1,109 @@ +import java.awt.Point; +import java.util.ArrayList; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +public class Model { + private ArrayList points; + private ArrayList listeners; + + /** + * Constructor + */ + public Model() { + points = new ArrayList(); + listeners = new ArrayList(); + if (new java.io.File("points.bin").exists()) { + // load points from file + try { + FileInputStream fi = new FileInputStream("points.bin"); + ObjectInputStream in = new ObjectInputStream(fi); + points = (ArrayList) in.readObject(); + in.close(); + fi.close(); + } catch (IOException e) { + System.err.println(e.getMessage()); + System.exit(1); + } catch (ClassNotFoundException e) { + System.err.println(e.getMessage()); + System.exit(1); + } + } + } + + public void addListener(ModelListener l) { + listeners.add(l); + } + + public ArrayList getPoints() { + return points; + } + + public void addPoint(Point p) { + points.add(p); + notifyListeners(); + } + + public void clearAllPoints() { + points.clear(); + notifyListeners(); + } + + public void deleteLastPoint() { + if (points.size() > 0) { + points.remove(points.size() - 1); + notifyListeners(); + } + } + + private void notifyListeners() { + for (ModelListener l : listeners) { + l.update(); + } + } + + public int numberOfPoints() { + return points.size(); + } + + public void saveData() { + try { + // save points to file + FileOutputStream fo = new FileOutputStream("points.bin"); + ObjectOutputStream out = new ObjectOutputStream(fo); + out.writeObject(points); + out.close(); + fo.close(); + } catch (IOException e) { + System.err.println(e.getMessage()); + System.exit(1); + } + } + + public static void testModel() { + Model m = new Model(); + m.addListener(new ModelListener() { + @Override + public void update() { + System.out.println(true + " (listener)"); + } + }); + System.out.println(m.getPoints() == m.points); + Point p1 = new Point(1, 2); + Point p2 = new Point(3, 4); + m.addPoint(p1); + m.addPoint(p2); + System.out.println(m.numberOfPoints() == 2); + System.out.println(m.points.get(0) == p1); + System.out.println(m.points.get(1) == p2); + m.deleteLastPoint(); + System.out.println(m.numberOfPoints() == 1); + System.out.println(m.points.get(0) == p1); + m.clearAllPoints(); + System.out.println(m.numberOfPoints() == 0); + m.notifyListeners(); + } +} diff --git a/lab/lab13/Question2/ModelListener.java b/lab/lab13/Question2/ModelListener.java new file mode 100644 index 0000000..664e6e5 --- /dev/null +++ b/lab/lab13/Question2/ModelListener.java @@ -0,0 +1,3 @@ +public interface ModelListener { + public void update(); +} diff --git a/lab/lab13/Question2/MyFrame.java b/lab/lab13/Question2/MyFrame.java new file mode 100644 index 0000000..9709259 --- /dev/null +++ b/lab/lab13/Question2/MyFrame.java @@ -0,0 +1,53 @@ +import java.awt.BorderLayout; +import java.awt.FlowLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.JButton; +import javax.swing.JPanel; + +public class MyFrame extends View { + + public MyFrame(Model m, ControllerClicks c) { + super(m, c); + + this.setTitle("MyFrame Title"); + this.setSize(400, 300); + this.setLocationRelativeTo(null); + this.setLayout(new BorderLayout()); + + MyPanel centerPanel = new MyPanel(m, c); + this.add(centerPanel, BorderLayout.CENTER); + + JPanel topPanel = new JPanel(); + this.add(topPanel, BorderLayout.PAGE_START); + + topPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); + JButton resetButton = new JButton("Reset"); + resetButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + c.resetClicked(); + } + }); + + topPanel.add(resetButton); + JButton undoButton = new JButton("Undo"); + + undoButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + c.undoClicked(); + } + }); + + topPanel.add(undoButton); + + // final + this.setVisible(true); + } + + @Override + public void update() { + repaint(); + } +} diff --git a/lab/lab13/Question2/MyPanel.java b/lab/lab13/Question2/MyPanel.java new file mode 100644 index 0000000..c238e68 --- /dev/null +++ b/lab/lab13/Question2/MyPanel.java @@ -0,0 +1,44 @@ +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Point; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.ArrayList; +import javax.swing.JPanel; + +public class MyPanel extends JPanel { + private Model m; + private ControllerClicks c; + + public MyPanel(Model m, ControllerClicks c) { + this.m = m; + this.c = c; + + // add mouse listener + this.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + if (e.getButton() == MouseEvent.BUTTON1) { + c.mouseClicked(e.getPoint()); + } + } + }); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + ArrayList points = m.getPoints(); + g.setColor(Color.RED); + if (points.size() == 1) { + Point p = points.get(0); + g.drawRect((int) p.getX(), (int) p.getY(), 1, 1); + } else { + for (int i = 1; i < points.size(); i++) { + Point start = points.get(i - 1); + Point end = points.get(i); + g.drawLine((int) start.getX(), (int) start.getY(), (int) end.getX(), (int) end.getY()); + } + } + } +} diff --git a/lab/lab13/Question2/Start.java b/lab/lab13/Question2/Start.java new file mode 100644 index 0000000..f9ba330 --- /dev/null +++ b/lab/lab13/Question2/Start.java @@ -0,0 +1,14 @@ +public class Start { + public static void main(String[] args) { + javax.swing.SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + Model model = new Model(); + ControllerClicks c1 = new ControllerClicks(model); + MyFrame frame = new MyFrame(model, c1); + Controller c2 = new Controller(model); + ViewNumber vn = new ViewNumber(model, c2); + } + }); + } +} diff --git a/lab/lab13/Question2/Test.java b/lab/lab13/Question2/Test.java new file mode 100644 index 0000000..dd06f02 --- /dev/null +++ b/lab/lab13/Question2/Test.java @@ -0,0 +1,5 @@ +public class Test { + public static void main(String[] args) { + Model.testModel(); + } +} diff --git a/lab/lab13/Question2/View.java b/lab/lab13/Question2/View.java new file mode 100644 index 0000000..61769ce --- /dev/null +++ b/lab/lab13/Question2/View.java @@ -0,0 +1,23 @@ +import javax.swing.JFrame; + +public abstract class View extends JFrame implements ModelListener { + protected Model m; + protected T c; + + public View(Model m, T c) { + this.m = m; + this.c = c; + + m.addListener(this); + setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); + + addWindowListener(new java.awt.event.WindowAdapter() { + public void windowClosing(java.awt.event.WindowEvent e) { + c.shutdown(); + } + }); + } + + @Override + public abstract void update(); +} diff --git a/lab/lab13/Question2/ViewNumber.java b/lab/lab13/Question2/ViewNumber.java new file mode 100644 index 0000000..4101df3 --- /dev/null +++ b/lab/lab13/Question2/ViewNumber.java @@ -0,0 +1,21 @@ +import javax.swing.JLabel; + +public class ViewNumber extends View { + + private JLabel label; + + public ViewNumber(Model m, Controller c) { + super(m, c); + this.setTitle("View Number"); + this.setSize(150, 150); + label = new JLabel(); + update(); + this.add(label); + this.setVisible(true); + } + + @Override + public void update() { + label.setText("Number of points is: " + m.numberOfPoints()); + } +} diff --git a/lab/lab13/Question3/Controller.java b/lab/lab13/Question3/Controller.java new file mode 100644 index 0000000..4243e1f --- /dev/null +++ b/lab/lab13/Question3/Controller.java @@ -0,0 +1,15 @@ +public class Controller { + protected Model m; + + public Controller(Model m) { + this.m = m; + } + + /** + * Gracefully exits the program. + */ + protected void shutdown() { + m.saveData(); + System.exit(0); + } +} diff --git a/lab/lab13/Question3/ControllerClicks.java b/lab/lab13/Question3/ControllerClicks.java new file mode 100644 index 0000000..e4d7d40 --- /dev/null +++ b/lab/lab13/Question3/ControllerClicks.java @@ -0,0 +1,19 @@ +import java.awt.Point; + +public class ControllerClicks extends Controller { + public ControllerClicks(Model m) { + super(m); + } + + public void mouseClicked(Point p) { + m.addPoint(p); + } + + public void resetClicked() { + m.clearAllPoints(); + } + + public void undoClicked() { + m.deleteLastPoint(); + } +} diff --git a/lab/lab13/Question3/Model.java b/lab/lab13/Question3/Model.java new file mode 100644 index 0000000..562ea9e --- /dev/null +++ b/lab/lab13/Question3/Model.java @@ -0,0 +1,104 @@ +import java.awt.Point; +import java.util.ArrayList; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class Model { + private ArrayList points; + private ArrayList listeners; + + public Model() { + points = new ArrayList(); + listeners = new ArrayList(); + // msw-sh wg. DB-Verbindung + try (Connection conn = DriverManager.getConnection("jdbc:mysql://msw-sh.local:3306/java", "hmsy", + "woshimima"); + Statement stmt = conn.createStatement();) { + ResultSet rs = stmt.executeQuery("SELECT * FROM points"); + while (rs.next()) { + points.add(new Point(rs.getInt("x"), rs.getInt("y"))); + } + } catch (SQLException e) { + System.err.println(e.getMessage()); + System.exit(1); + } + } + + public void addListener(ModelListener l) { + listeners.add(l); + } + + public ArrayList getPoints() { + return points; + } + + public void addPoint(Point p) { + points.add(p); + notifyListeners(); + } + + public void clearAllPoints() { + points.clear(); + notifyListeners(); + } + + public void deleteLastPoint() { + if (points.size() > 0) { + points.remove(points.size() - 1); + notifyListeners(); + } + } + + private void notifyListeners() { + for (ModelListener l : listeners) { + l.update(); + } + } + + public int numberOfPoints() { + return points.size(); + } + + // save to DB + public void saveData() { + try (Connection conn = DriverManager.getConnection("jdbc:mysql://msw-sh.local:3306/java", "hmsy", + "woshimima"); + Statement stmt = conn.createStatement();) { + // delete all points + stmt.executeUpdate("DELETE FROM points"); + for (Point p : points) { + stmt.executeUpdate("INSERT INTO points (x, y) VALUES (" + p.x + ", " + p.y + ")"); + } + } catch (SQLException e) { + System.err.println(e.getMessage()); + System.exit(2); + } + } + + public static void testModel() { + Model m = new Model(); + m.addListener(new ModelListener() { + @Override + public void update() { + System.out.println(true + " (listener)"); + } + }); + System.out.println(m.getPoints() == m.points); + Point p1 = new Point(1, 2); + Point p2 = new Point(3, 4); + m.addPoint(p1); // Listener called. + m.addPoint(p2); // Listener called. + System.out.println(m.numberOfPoints() == 2); + System.out.println(m.points.get(0) == p1); + System.out.println(m.points.get(1) == p2); + m.deleteLastPoint(); // Listener called. + System.out.println(m.numberOfPoints() == 1); + System.out.println(m.points.get(0) == p1); + m.clearAllPoints(); // Listener called. + System.out.println(m.numberOfPoints() == 0); + m.notifyListeners(); // Listener called. + } +} diff --git a/lab/lab13/Question3/ModelListener.java b/lab/lab13/Question3/ModelListener.java new file mode 100644 index 0000000..664e6e5 --- /dev/null +++ b/lab/lab13/Question3/ModelListener.java @@ -0,0 +1,3 @@ +public interface ModelListener { + public void update(); +} diff --git a/lab/lab13/Question3/MyFrame.java b/lab/lab13/Question3/MyFrame.java new file mode 100644 index 0000000..f602435 --- /dev/null +++ b/lab/lab13/Question3/MyFrame.java @@ -0,0 +1,51 @@ +import java.awt.BorderLayout; +import java.awt.FlowLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.JButton; +import javax.swing.JPanel; + +public class MyFrame extends View { + public MyFrame(Model m, ControllerClicks c) { + super(m, c); + this.setTitle("MyFrame Title"); + this.setSize(400, 300); + this.setLocationRelativeTo(null); + this.setLayout(new BorderLayout()); + + MyPanel centerPanel = new MyPanel(m, c); + this.add(centerPanel, BorderLayout.CENTER); + + JPanel topPanel = new JPanel(); + this.add(topPanel, BorderLayout.PAGE_START); + + topPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); + + JButton resetButton = new JButton("Reset"); + resetButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + c.resetClicked(); + } + }); + + topPanel.add(resetButton); + JButton undoButton = new JButton("Undo"); + undoButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + c.undoClicked(); + } + }); + + topPanel.add(undoButton); + + // final + this.setVisible(true); + } + + @Override + public void update() { + repaint(); + } +} diff --git a/lab/lab13/Question3/MyPanel.java b/lab/lab13/Question3/MyPanel.java new file mode 100644 index 0000000..4f3bda8 --- /dev/null +++ b/lab/lab13/Question3/MyPanel.java @@ -0,0 +1,42 @@ +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Point; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.ArrayList; +import javax.swing.JPanel; + +public class MyPanel extends JPanel { + private Model m; + private ControllerClicks c; + + public MyPanel(Model m, ControllerClicks c) { + this.m = m; + this.c = c; + this.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + if (e.getButton() == MouseEvent.BUTTON1) { + c.mouseClicked(e.getPoint()); + } + } + }); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + ArrayList points = m.getPoints(); + g.setColor(Color.RED); + if (points.size() == 1) { + Point p = points.get(0); + g.drawRect((int) p.getX(), (int) p.getY(), 1, 1); + } else { + for (int i = 1; i < points.size(); i++) { + Point start = points.get(i - 1); + Point end = points.get(i); + g.drawLine((int) start.getX(), (int) start.getY(), (int) end.getX(), (int) end.getY()); + } + } + } +} diff --git a/lab/lab13/Question3/Start.java b/lab/lab13/Question3/Start.java new file mode 100644 index 0000000..f9ba330 --- /dev/null +++ b/lab/lab13/Question3/Start.java @@ -0,0 +1,14 @@ +public class Start { + public static void main(String[] args) { + javax.swing.SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + Model model = new Model(); + ControllerClicks c1 = new ControllerClicks(model); + MyFrame frame = new MyFrame(model, c1); + Controller c2 = new Controller(model); + ViewNumber vn = new ViewNumber(model, c2); + } + }); + } +} diff --git a/lab/lab13/Question3/Test.java b/lab/lab13/Question3/Test.java new file mode 100644 index 0000000..dd06f02 --- /dev/null +++ b/lab/lab13/Question3/Test.java @@ -0,0 +1,5 @@ +public class Test { + public static void main(String[] args) { + Model.testModel(); + } +} diff --git a/lab/lab13/Question3/View.java b/lab/lab13/Question3/View.java new file mode 100644 index 0000000..0b6a503 --- /dev/null +++ b/lab/lab13/Question3/View.java @@ -0,0 +1,21 @@ +import javax.swing.JFrame; + +public abstract class View extends JFrame implements ModelListener { + protected Model m; + protected T c; + + public View(Model m, T c) { + this.m = m; + this.c = c; + m.addListener(this); + setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); + addWindowListener(new java.awt.event.WindowAdapter() { + public void windowClosing(java.awt.event.WindowEvent e) { + c.shutdown(); + } + }); + } + + @Override + public abstract void update(); +} diff --git a/lab/lab13/Question3/ViewNumber.java b/lab/lab13/Question3/ViewNumber.java new file mode 100644 index 0000000..94278f2 --- /dev/null +++ b/lab/lab13/Question3/ViewNumber.java @@ -0,0 +1,20 @@ +import javax.swing.JLabel; + +public class ViewNumber extends View { + private JLabel label; + + public ViewNumber(Model m, Controller c) { + super(m, c); + this.setTitle("View Number"); + this.setSize(150, 150); + label = new JLabel(); + update(); + this.add(label); + this.setVisible(true); + } + + @Override + public void update() { + label.setText("Number of points is: " + m.numberOfPoints()); + } +}