Restore
This commit is contained in:
@@ -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>
|
||||
@@ -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
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,10 @@
|
||||
|
||||
public class Controller{
|
||||
//instance variable
|
||||
protected Model m;
|
||||
|
||||
//constructor
|
||||
public Controller(Model m) {
|
||||
this.m = m;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import java.awt.Point;
|
||||
|
||||
public class ControllerClicks extends Controller{
|
||||
//constructor
|
||||
public ControllerClicks(Model m) {
|
||||
super(m);
|
||||
}
|
||||
|
||||
//methods
|
||||
public void mouseClicked(Point p) {
|
||||
m.addPoint(p);
|
||||
}
|
||||
|
||||
public void resetClicked() {
|
||||
m.clearAllPoints();
|
||||
}
|
||||
|
||||
public void undoClicked() {
|
||||
m.deleteLastPoint();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import java.awt.Point;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Model {
|
||||
//instance variables
|
||||
private ArrayList<Point> points;
|
||||
private ArrayList<ModelListener> listeners;
|
||||
|
||||
//constructor
|
||||
public Model() {
|
||||
points = new ArrayList<Point>();
|
||||
listeners = new ArrayList<ModelListener>();
|
||||
}
|
||||
|
||||
//methods
|
||||
public void addListener(ModelListener l) {
|
||||
listeners.add(l);
|
||||
}
|
||||
|
||||
public ArrayList<Point> 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();
|
||||
}
|
||||
}
|
||||
|
||||
//test
|
||||
public static void testModel() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
public interface ModelListener {
|
||||
public void update();
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
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.JFrame;
|
||||
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);
|
||||
}
|
||||
|
||||
public void update() {
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
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 {
|
||||
//instance variables
|
||||
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);
|
||||
g.setColor(Color.RED);
|
||||
if(m.getPoints().size() == 1) {
|
||||
Point p = m.getPoints().get(0);
|
||||
g.drawRect((int)p.getX(), (int)p.getY(), 1, 1);
|
||||
}
|
||||
else {
|
||||
for(int i = 1; i < m.getPoints().size(); i++) {
|
||||
Point start = m.getPoints().get(i - 1);
|
||||
Point end = m.getPoints().get(i);
|
||||
g.drawLine((int)start.getX(), (int)start.getY(),
|
||||
(int)end.getX(), (int)end.getY());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
javax.swing.SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Model m = new Model(); // Single shared model.
|
||||
ControllerClicks c = new ControllerClicks(m);
|
||||
MyFrame v = new MyFrame(m, c);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
|
||||
public class Test {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Model.testModel();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import javax.swing.JFrame;
|
||||
|
||||
public abstract class View<T extends Controller> extends JFrame implements ModelListener{
|
||||
//instance variables
|
||||
protected Model m;
|
||||
protected T c;
|
||||
|
||||
//constructor
|
||||
public View(Model m, T c) {
|
||||
this.m = m;
|
||||
this.c = c;
|
||||
m.addListener(this);
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract void update();
|
||||
}
|
||||
Reference in New Issue
Block a user