Restore
This commit is contained in:
10
Labs/Lab13/Question1/.classpath
Normal file
10
Labs/Lab13/Question1/.classpath
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
17
Labs/Lab13/Question1/.project
Normal file
17
Labs/Lab13/Question1/.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Question1</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,2 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
||||
BIN
Labs/Lab13/Question1/bin/Controller.class
Normal file
BIN
Labs/Lab13/Question1/bin/Controller.class
Normal file
Binary file not shown.
BIN
Labs/Lab13/Question1/bin/ControllerClicks.class
Normal file
BIN
Labs/Lab13/Question1/bin/ControllerClicks.class
Normal file
Binary file not shown.
BIN
Labs/Lab13/Question1/bin/Model$1.class
Normal file
BIN
Labs/Lab13/Question1/bin/Model$1.class
Normal file
Binary file not shown.
BIN
Labs/Lab13/Question1/bin/Model.class
Normal file
BIN
Labs/Lab13/Question1/bin/Model.class
Normal file
Binary file not shown.
BIN
Labs/Lab13/Question1/bin/ModelListener.class
Normal file
BIN
Labs/Lab13/Question1/bin/ModelListener.class
Normal file
Binary file not shown.
BIN
Labs/Lab13/Question1/bin/MyFrame$1.class
Normal file
BIN
Labs/Lab13/Question1/bin/MyFrame$1.class
Normal file
Binary file not shown.
BIN
Labs/Lab13/Question1/bin/MyFrame$2.class
Normal file
BIN
Labs/Lab13/Question1/bin/MyFrame$2.class
Normal file
Binary file not shown.
BIN
Labs/Lab13/Question1/bin/MyFrame.class
Normal file
BIN
Labs/Lab13/Question1/bin/MyFrame.class
Normal file
Binary file not shown.
BIN
Labs/Lab13/Question1/bin/MyPanel$1.class
Normal file
BIN
Labs/Lab13/Question1/bin/MyPanel$1.class
Normal file
Binary file not shown.
BIN
Labs/Lab13/Question1/bin/MyPanel.class
Normal file
BIN
Labs/Lab13/Question1/bin/MyPanel.class
Normal file
Binary file not shown.
BIN
Labs/Lab13/Question1/bin/Start$1.class
Normal file
BIN
Labs/Lab13/Question1/bin/Start$1.class
Normal file
Binary file not shown.
BIN
Labs/Lab13/Question1/bin/Start.class
Normal file
BIN
Labs/Lab13/Question1/bin/Start.class
Normal file
Binary file not shown.
BIN
Labs/Lab13/Question1/bin/Test.class
Normal file
BIN
Labs/Lab13/Question1/bin/Test.class
Normal file
Binary file not shown.
BIN
Labs/Lab13/Question1/bin/View$1.class
Normal file
BIN
Labs/Lab13/Question1/bin/View$1.class
Normal file
Binary file not shown.
BIN
Labs/Lab13/Question1/bin/View.class
Normal file
BIN
Labs/Lab13/Question1/bin/View.class
Normal file
Binary file not shown.
BIN
Labs/Lab13/Question1/bin/ViewNumber.class
Normal file
BIN
Labs/Lab13/Question1/bin/ViewNumber.class
Normal file
Binary file not shown.
2
Labs/Lab13/Question1/points.txt
Normal file
2
Labs/Lab13/Question1/points.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
190 91
|
||||
239 123
|
||||
12
Labs/Lab13/Question1/src/Controller.java
Normal file
12
Labs/Lab13/Question1/src/Controller.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
16
Labs/Lab13/Question1/src/ControllerClicks.java
Normal file
16
Labs/Lab13/Question1/src/ControllerClicks.java
Normal file
@@ -0,0 +1,16 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
116
Labs/Lab13/Question1/src/Model.java
Normal file
116
Labs/Lab13/Question1/src/Model.java
Normal file
@@ -0,0 +1,116 @@
|
||||
import java.awt.Point;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Model {
|
||||
private ArrayList<Point> points;
|
||||
private ArrayList<ModelListener> listeners;
|
||||
|
||||
public Model() {
|
||||
points = new ArrayList<Point>();
|
||||
listeners = new ArrayList<ModelListener>();
|
||||
File file = new File("points.txt");
|
||||
if(!file.exists()) { // then create it:
|
||||
try {
|
||||
PrintWriter out = new PrintWriter(file);
|
||||
out.println(0); // Initialize counter in new file.
|
||||
out.close();
|
||||
}
|
||||
catch(FileNotFoundException e) {
|
||||
System.err.println(e.getMessage());
|
||||
System.exit(1); // End immediately.
|
||||
}
|
||||
}
|
||||
else {
|
||||
try {
|
||||
Scanner s = new Scanner(file);
|
||||
while(s.hasNext()) {
|
||||
points.add(new Point(s.nextInt(), s.nextInt()));
|
||||
}
|
||||
s.close();
|
||||
}
|
||||
catch(FileNotFoundException ex) {
|
||||
System.err.println(ex.getMessage());
|
||||
System.exit(1); // End immediately.
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void addListener(ModelListener l) {
|
||||
listeners.add(l);
|
||||
}
|
||||
|
||||
public ArrayList<Point> getPoints() {
|
||||
return points;
|
||||
}
|
||||
|
||||
public void addPoint(Point p) {
|
||||
points.add(p);
|
||||
notifyListeners(); // points changed so notify the listeners.
|
||||
}
|
||||
|
||||
public void clearAllPoints() {
|
||||
points.clear();
|
||||
notifyListeners(); // points changed so notify the listeners.
|
||||
}
|
||||
|
||||
public void deleteLastPoint() {
|
||||
if(points.size() > 0) {
|
||||
points.remove(points.size() - 1);
|
||||
notifyListeners(); // points changed so notify the listeners.
|
||||
}
|
||||
}
|
||||
|
||||
public void saveData() {
|
||||
try {
|
||||
PrintWriter out = new PrintWriter("points.txt");
|
||||
for(Point p: points) {
|
||||
out.println((int)p.getX() + " " + (int)p.getY()); // Update file.
|
||||
}
|
||||
out.close();
|
||||
}
|
||||
catch(FileNotFoundException e) {
|
||||
System.err.println(e.getMessage());
|
||||
System.exit(1); // End immediately.
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
public int numberOfPoints() {
|
||||
return points.size();
|
||||
}
|
||||
|
||||
private void notifyListeners() {
|
||||
for(ModelListener l: listeners) {
|
||||
l.update(); // Tell the listener that something changed.
|
||||
}
|
||||
}
|
||||
|
||||
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.
|
||||
}
|
||||
}
|
||||
4
Labs/Lab13/Question1/src/ModelListener.java
Normal file
4
Labs/Lab13/Question1/src/ModelListener.java
Normal file
@@ -0,0 +1,4 @@
|
||||
|
||||
public interface ModelListener {
|
||||
public void update();
|
||||
}
|
||||
43
Labs/Lab13/Question1/src/MyFrame.java
Normal file
43
Labs/Lab13/Question1/src/MyFrame.java
Normal file
@@ -0,0 +1,43 @@
|
||||
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<ControllerClicks> {
|
||||
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);
|
||||
this.setVisible(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
repaint(); // Makes Swing call MyPanel's paintComponent method.
|
||||
}
|
||||
}
|
||||
42
Labs/Lab13/Question1/src/MyPanel.java
Normal file
42
Labs/Lab13/Question1/src/MyPanel.java
Normal file
@@ -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<Point> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Labs/Lab13/Question1/src/Start.java
Normal file
14
Labs/Lab13/Question1/src/Start.java
Normal file
@@ -0,0 +1,14 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
javax.swing.SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Model m = new Model();
|
||||
ControllerClicks c1 = new ControllerClicks(m);
|
||||
MyFrame v1 = new MyFrame(m, c1);
|
||||
Controller c2 = new Controller(m);
|
||||
ViewNumber v2 = new ViewNumber(m, c2);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
5
Labs/Lab13/Question1/src/Test.java
Normal file
5
Labs/Lab13/Question1/src/Test.java
Normal file
@@ -0,0 +1,5 @@
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
Model.testModel();
|
||||
}
|
||||
}
|
||||
26
Labs/Lab13/Question1/src/View.java
Normal file
26
Labs/Lab13/Question1/src/View.java
Normal file
@@ -0,0 +1,26 @@
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
|
||||
public abstract class View<T extends Controller> 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);
|
||||
this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
|
||||
this.addWindowListener(new WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
c.shutdown(); // Controller decides meaning.
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract void update();
|
||||
}
|
||||
19
Labs/Lab13/Question1/src/ViewNumber.java
Normal file
19
Labs/Lab13/Question1/src/ViewNumber.java
Normal file
@@ -0,0 +1,19 @@
|
||||
import javax.swing.JLabel;
|
||||
|
||||
public class ViewNumber extends View<Controller> {
|
||||
private JLabel label;
|
||||
public ViewNumber(Model m, Controller c) {
|
||||
super(m, c);
|
||||
this.setTitle("View Number");
|
||||
this.setSize(150, 150);
|
||||
label = new JLabel();
|
||||
update(); // Initialize the label using the model.
|
||||
this.add(label);
|
||||
this.setVisible(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
label.setText("Number of points is: " + m.numberOfPoints());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user