Compare commits

..

3 Commits

Author SHA1 Message Date
5dfbf0b8ae a6 2022-05-25 13:14:19 +08:00
a9f7967976 lab 13 2022-05-25 13:14:19 +08:00
d0416b02bd lab12 2022-05-25 13:14:16 +08:00
48 changed files with 1506 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
import java.awt.Graphics;
public interface IShape {
public int getX();
public int getY();
public void setX(int x);
public void setY(int y);
public boolean isVisible(int w, int h);
public boolean isIn(int x, int y);
public void draw(Graphics g);
}

View File

@@ -0,0 +1,51 @@
import java.awt.Color;
import java.awt.Graphics;
public abstract class Shape {
private int x;
private int y;
private Color color;
/**
* Constructor for Shape
*
* @param x
* the x coordinate of the shape
* @param y
* the y coordinate of the shape
*/
public Shape(int x, int y) {
this.x = x;
this.y = y;
color = new Color((float) Math.random(), (float) Math.random(), (float) Math.random());
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public abstract boolean isVisible(int w, int h);
public abstract boolean isIn(int x, int y);
public void draw(Graphics g) {
g.setColor(color);
}
public static void testShape() {
// TODO: implement this method
// Because this is the abstract so we don't test anything here.
}
}

View File

@@ -0,0 +1,5 @@
public class Start {
public static void main(String[] args) {
Shape.testShape();
}
}

View File

@@ -0,0 +1,89 @@
import java.awt.Graphics;
public class Bubble extends Shape {
private double radius;
/**
* Constructor for the Bubble class.
*
* @param x the x coordinate of the center of the bubble
* @param y the y coordinate of the center of the bubble
*/
public Bubble(int x, int y) {
super(x, y);
this.radius = 10;
}
// Find the point (wx, wy) inside the window which is closest to the
// center (x, y) of the circle. In other words, find the wx in the
// interval [0, w - 1] which is closest to x, and find the wy in the
// interval [0, h - 1] which is closest to y.
// If the distance between (wx, wy) and (x, y) is less than the radius
// of the circle (using Pythagoras's theorem) then at least part of
// the circle is visible in the window.
// Note: if the center of the circle is inside the window, then (wx, wy)
// is the same as (x, y), and the distance is 0.
@Override
public boolean isVisible(int w, int h) {
double x = getX();
double y = getY();
double wx = (x < 0 ? 0 : (x > w - 1 ? w - 1 : x));
double wy = (y < 0 ? 0 : (y > h - 1 ? h - 1 : y));
double dx = wx - x;
double dy = wy - y;
return dx * dx + dy * dy <= radius * radius;
}
/**
* Whether the point at xy is currently inside the bubble
*
* @param x the x coordinate of the point
* @param y the y coordinate of the point
* @return true if the point is inside the bubble, false otherwise
*/
@Override
public boolean isIn(int x, int y) {
double dx = x - getX();
double dy = y - getY();
return dx * dx + dy * dy < radius * radius;
}
/**
* Draw the bubble.
*
* @param g the graphics context
*/
@Override
public void draw(Graphics g) {
super.draw(g);
g.drawOval(
getX() - (int) radius,
getY() - (int) radius,
(int) radius * 2,
(int) radius * 2);
}
public static void testBubble() {
Bubble b = new Bubble(20, 30);
System.out.println(b.getX() == 20);
System.out.println(b.getY() == 30);
b.setX(40);
System.out.println(b.getX() == 40);
System.out.println(b.getY() == 30);
b.setY(60);
System.out.println(b.getX() == 40);
System.out.println(b.getY() == 60);
System.out.println(b.isVisible(100, 100) == true);
b.setX(50);
b.setY(0);
System.out.println(b.isVisible(100, 100) == true);
b.setX(99);
b.setY(50);
System.out.println(b.isVisible(100, 100) == true);
}
}

View File

@@ -0,0 +1,17 @@
import java.awt.Graphics;
public interface IShape {
public int getX();
public int getY();
public void setX(int x);
public void setY(int y);
public boolean isVisible(int w, int h);
public boolean isIn(int x, int y);
public void draw(Graphics g);
}

View File

@@ -0,0 +1,42 @@
import java.awt.Color;
import java.awt.Graphics;
public abstract class Shape {
private int x;
private int y;
private Color color;
public Shape(int x, int y) {
this.x = x;
this.y = y;
color = new Color((float) Math.random(), (float) Math.random(), (float) Math.random());
}
// getters and setters
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public abstract boolean isVisible(int w, int h);
public abstract boolean isIn(int x, int y);
public void draw(Graphics g) {
g.setColor(color);
}
public static void testShape() {
}
}

View File

@@ -0,0 +1,6 @@
public class Start {
public static void main(String[] args) {
Shape.testShape();
Bubble.testBubble();
}
}

View File

@@ -0,0 +1,65 @@
import java.awt.Graphics;
public class Bubble extends Shape implements IShape {
// instance variables
private double radius;
// constructor
public Bubble(int x, int y) {
super(x, y);
this.radius = 10;
}
// Find the point (wx, wy) inside the window which is closest to the
// center (x, y) of the circle. In other words, find the wx in the
// interval [0, w - 1] which is closest to x, and find the wy in the
// interval [0, h - 1] which is closest to y.
// If the distance between (wx, wy) and (x, y) is less than the radius
// of the circle (using Pythagoras's theorem) then at least part of
// the circle is visible in the window.
// Note: if the center of the circle is inside the window, then (wx, wy)
// is the same as (x, y), and the distance is 0.
@Override
public boolean isVisible(int w, int h) {
double x = getX();
double y = getY();
double wx = (x < 0 ? 0 : (x > w - 1 ? w - 1 : x));
double wy = (y < 0 ? 0 : (y > h - 1 ? h - 1 : y));
double dx = wx - x;
double dy = wy - y;
return dx * dx + dy * dy <= radius * radius;
}
@Override
public boolean isIn(int x, int y) {
double dx = x - getX();
double dy = y - getY();
return dx * dx + dy * dy < radius * radius;
}
@Override
public void draw(Graphics g) {
super.draw(g);
g.drawOval(
getX() - (int) radius,
getY() - (int) radius,
(int) radius * 2,
(int) radius * 2);
}
public static void testBubble() {
Bubble b = new Bubble(20, 30);
System.out.println(b.getX() == 20);
System.out.println(b.getY() == 30);
b.setX(40);
System.out.println(b.getX() == 40);
System.out.println(b.getY() == 30);
b.setY(60);
System.out.println(b.getX() == 40);
System.out.println(b.getY() == 60);
System.out.println(b.isVisible(100, 100) == true);
b.setX(50);
b.setY(0);
System.out.println(b.isVisible(100, 100) == true);
}
}

View File

@@ -0,0 +1,17 @@
import java.awt.Graphics;
public interface IShape {
public int getX();
public int getY();
public void setX(int x);
public void setY(int y);
public boolean isVisible(int w, int h);
public boolean isIn(int x, int y);
public void draw(Graphics g);
}

View File

@@ -0,0 +1,105 @@
import java.awt.Graphics;
import java.util.ArrayList;
public class Model {
// instance variables
private int score;
private ArrayList<IShape> bubbles;
// constructor
public Model() {
score = 0;
bubbles = new ArrayList<IShape>();
}
// getters and setters
public int getScore() {
return score;
}
/**
* @param w
* @param h
*/
public void addBubble(int w, int h) {
bubbles.add(new Bubble((int) (w * Math.random()), (int) (h * Math.random())));
}
/**
* @param dx
* @param dy
*/
public void moveAll(int dx, int dy) {
for (int i = 0; i < bubbles.size(); i++) {
bubbles.get(i).setX(bubbles.get(i).getX() + dx);
bubbles.get(i).setY(bubbles.get(i).getY() + dy);
}
}
/**
* clear all bubbles
*
* @param w
* @param h
*/
public void clearInvisibles(int w, int h) {
for (int i = bubbles.size() - 1; i >= 0; i--) {
if (!bubbles.get(i).isVisible(w, h)) {
bubbles.remove(i);
score--;
}
}
}
/**
* delete bubbles that are hit by the mouse
*
* @param x
* @param y
*/
public void deleteBubblesAtPoint(int x, int y) {
for (int i = bubbles.size() - 1; i >= 0; i--) {
if (bubbles.get(i).isIn(x, y)) {
bubbles.remove(i);
score++;
}
}
}
/**
* draw all bubbles
*
* @param g
*/
public void drawAll(Graphics g) {
for (IShape bubble : bubbles) {
bubble.draw(g);
}
}
public static void testModel() {
Model m = new Model();
System.out.println(m.getScore() == 0);
System.out.println(m.bubbles.size() == 0);
m.addBubble(100, 100);
m.addBubble(100, 100);
System.out.println(m.getScore() == 0);
System.out.println(m.bubbles.size() == 2);
m.clearInvisibles(200, 200);
System.out.println(m.getScore() == 0);
System.out.println(m.bubbles.size() == 2);
m.clearInvisibles(0, 0);
System.out.println(m.getScore() == -2);
System.out.println(m.bubbles.size() == 0);
m.addBubble(100, 100);
m.addBubble(100, 100);
System.out.println(m.getScore() == -2);
System.out.println(m.bubbles.size() == 2);
m.moveAll(200, 200);
System.out.println(m.getScore() == -2);
System.out.println(m.bubbles.size() == 2);
m.clearInvisibles(200, 200);
System.out.println(m.getScore() == -4);
System.out.println(m.bubbles.size() == 0);
}
}

View File

@@ -0,0 +1,41 @@
import java.awt.Color;
import java.awt.Graphics;
public abstract class Shape {
private int x;
private int y;
private Color color;
public Shape(int x, int y) {
this.x = x;
this.y = y;
color = new Color((float) Math.random(), (float) Math.random(), (float) Math.random());
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public abstract boolean isVisible(int w, int h);
public abstract boolean isIn(int x, int y);
public void draw(Graphics g) {
g.setColor(color);
}
public static void testShape() {
}
}

View File

@@ -0,0 +1,7 @@
public class Start {
public static void main(String[] args) {
Shape.testShape();
Bubble.testBubble();
Model.testModel();
}
}

View File

@@ -0,0 +1,30 @@
import java.awt.*;
import javax.swing.*;
public class MyFrame extends JFrame {
public MyFrame() {
// init frame
setSize(400, 300);
setTitle("MyFrame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// craete components
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton button1 = new JButton("left");
JButton button2 = new JButton("right");
// set layout
panel.add(button1);
panel.add(button2);
add(panel, BorderLayout.PAGE_START);
// MyPanel object
MyPanel mp = new MyPanel(new FlowLayout(FlowLayout.CENTER));
add(mp, BorderLayout.CENTER);
// done
setVisible(true);
}
}

View File

@@ -0,0 +1,21 @@
import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MyPanel extends JPanel {
public MyPanel(LayoutManager layout) {
super(layout);
// add anonymouse listener
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// print if left clicked
if (e.getButton() == MouseEvent.BUTTON1) {
System.out.println("Left button clicked at " + e.getX() + "," + e.getY());
}
}
});
}
}

View File

@@ -0,0 +1,12 @@
/**
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007).
*/
public class Start {
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MyFrame();
}
});
}
}

View File

@@ -0,0 +1,28 @@
import java.awt.*;
import javax.swing.*;
public class MyFrame extends JFrame {
public MyFrame() {
// init
setSize(400, 300);
setTitle("MyFrame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// add layout
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton button1 = new JButton("left");
JButton button2 = new JButton("right");
panel.add(button1);
panel.add(button2);
add(panel, BorderLayout.PAGE_START);
// MyPanel object
MyPanel mp = new MyPanel(new FlowLayout(FlowLayout.CENTER));
add(mp, BorderLayout.CENTER);
// done
setVisible(true);
}
}

View File

@@ -0,0 +1,37 @@
import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MyPanel extends JPanel {
private int x = -1;
private int y = -1;
public MyPanel(LayoutManager layout) {
super(layout);
// anonymouse listener
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// left clicked
if (e.getButton() == MouseEvent.BUTTON1) {
System.out.println("Left button clicked at " + e.getX() + "," + e.getY());
}
x = e.getX();
y = e.getY();
repaint();
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// red dot
g.setColor(Color.RED);
g.drawRect(x, y, 1, 1);
}
}

View File

@@ -0,0 +1,12 @@
/**
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007).
*/
public class Start {
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MyFrame();
}
});
}
}

View File

@@ -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);
}
}

View File

@@ -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();
}
}

View File

@@ -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<Point> points;
private ArrayList<ModelListener> listeners;
public Model() {
points = new ArrayList<Point>();
listeners = new ArrayList<ModelListener>();
if (new java.io.File("points.bin").exists()) {
try {
FileInputStream fi = new FileInputStream("points.bin");
ObjectInputStream in = new ObjectInputStream(fi);
points = (ArrayList<Point>) 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<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();
}
}
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();
}
}

View File

@@ -0,0 +1,3 @@
public interface ModelListener {
public void update();
}

View File

@@ -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<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());
// 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();
}
}

View File

@@ -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<Point> 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());
}
}
}
}

View 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 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);
}
});
}
}

View File

@@ -0,0 +1,5 @@
public class Test {
public static void main(String[] args) {
Model.testModel();
}
}

View File

@@ -0,0 +1,22 @@
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);
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();
}

View File

@@ -0,0 +1,21 @@
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();
this.add(label);
this.setVisible(true);
}
@Override
public void update() {
label.setText("Number of points is: " + m.numberOfPoints());
}
}

View 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);
}
}

View File

@@ -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();
}
}

View File

@@ -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<Point> points;
private ArrayList<ModelListener> listeners;
/**
* Constructor
*/
public Model() {
points = new ArrayList<Point>();
listeners = new ArrayList<ModelListener>();
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<Point>) 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<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();
}
}
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();
}
}

View File

@@ -0,0 +1,3 @@
public interface ModelListener {
public void update();
}

View File

@@ -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<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);
// final
this.setVisible(true);
}
@Override
public void update() {
repaint();
}
}

View File

@@ -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<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());
}
}
}
}

View 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 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);
}
});
}
}

View File

@@ -0,0 +1,5 @@
public class Test {
public static void main(String[] args) {
Model.testModel();
}
}

View File

@@ -0,0 +1,23 @@
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);
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();
}

View File

@@ -0,0 +1,21 @@
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();
this.add(label);
this.setVisible(true);
}
@Override
public void update() {
label.setText("Number of points is: " + m.numberOfPoints());
}
}

View File

@@ -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);
}
}

View File

@@ -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();
}
}

View File

@@ -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<Point> points;
private ArrayList<ModelListener> listeners;
public Model() {
points = new ArrayList<Point>();
listeners = new ArrayList<ModelListener>();
// 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<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();
}
}
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.
}
}

View File

@@ -0,0 +1,3 @@
public interface ModelListener {
public void update();
}

View File

@@ -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<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);
// final
this.setVisible(true);
}
@Override
public void update() {
repaint();
}
}

View 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());
}
}
}
}

View 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 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);
}
});
}
}

View File

@@ -0,0 +1,5 @@
public class Test {
public static void main(String[] args) {
Model.testModel();
}
}

View File

@@ -0,0 +1,21 @@
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);
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();
}

View File

@@ -0,0 +1,20 @@
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();
this.add(label);
this.setVisible(true);
}
@Override
public void update() {
label.setText("Number of points is: " + m.numberOfPoints());
}
}