diff options
Diffstat (limited to 'Week4 Drawing loipes/src')
-rw-r--r-- | Week4 Drawing loipes/src/no.png | bin | 0 -> 3662 bytes | |||
-rw-r--r-- | Week4 Drawing loipes/src/nw.png | bin | 0 -> 3646 bytes | |||
-rw-r--r-- | Week4 Drawing loipes/src/nz.png | bin | 0 -> 199 bytes | |||
-rw-r--r-- | Week4 Drawing loipes/src/nzow.png | bin | 0 -> 214 bytes | |||
-rw-r--r-- | Week4 Drawing loipes/src/oo15loipe/AsciiArt.java | 51 | ||||
-rw-r--r-- | Week4 Drawing loipes/src/oo15loipe/Fragment.java | 8 | ||||
-rw-r--r-- | Week4 Drawing loipes/src/oo15loipe/InfoLoipe.java | 35 | ||||
-rw-r--r-- | Week4 Drawing loipes/src/oo15loipe/Loipe.java | 226 | ||||
-rw-r--r-- | Week4 Drawing loipes/src/oo15loipe/LoipePlaatje.java | 191 | ||||
-rw-r--r-- | Week4 Drawing loipes/src/oo15loipe/Punt.java | 73 | ||||
-rw-r--r-- | Week4 Drawing loipes/src/oo15loipe/TekenLoipe.java | 19 | ||||
-rw-r--r-- | Week4 Drawing loipes/src/ow.png | bin | 0 -> 3273 bytes | |||
-rw-r--r-- | Week4 Drawing loipes/src/week4/Week4.java | 36 | ||||
-rw-r--r-- | Week4 Drawing loipes/src/zo.png | bin | 0 -> 3662 bytes | |||
-rw-r--r-- | Week4 Drawing loipes/src/zw.png | bin | 0 -> 3641 bytes |
15 files changed, 639 insertions, 0 deletions
diff --git a/Week4 Drawing loipes/src/no.png b/Week4 Drawing loipes/src/no.png Binary files differnew file mode 100644 index 0000000..daaec27 --- /dev/null +++ b/Week4 Drawing loipes/src/no.png diff --git a/Week4 Drawing loipes/src/nw.png b/Week4 Drawing loipes/src/nw.png Binary files differnew file mode 100644 index 0000000..2699df3 --- /dev/null +++ b/Week4 Drawing loipes/src/nw.png diff --git a/Week4 Drawing loipes/src/nz.png b/Week4 Drawing loipes/src/nz.png Binary files differnew file mode 100644 index 0000000..c4eeb9c --- /dev/null +++ b/Week4 Drawing loipes/src/nz.png diff --git a/Week4 Drawing loipes/src/nzow.png b/Week4 Drawing loipes/src/nzow.png Binary files differnew file mode 100644 index 0000000..96ab874 --- /dev/null +++ b/Week4 Drawing loipes/src/nzow.png diff --git a/Week4 Drawing loipes/src/oo15loipe/AsciiArt.java b/Week4 Drawing loipes/src/oo15loipe/AsciiArt.java new file mode 100644 index 0000000..4072d54 --- /dev/null +++ b/Week4 Drawing loipes/src/oo15loipe/AsciiArt.java @@ -0,0 +1,51 @@ +package oo15loipe;
+
+/**
+ * A class that draws the loipe as ascii-art.
+ *
+ * @author Thijs Heijligenberg, s4451414, Camil Staps, s4498062
+ */
+public class AsciiArt implements TekenLoipe{
+ private InfoLoipe L;
+ private Punt man;
+
+ /**
+ * Create the instance based on a loipe
+ *
+ * @param s
+ */
+ public AsciiArt(InfoLoipe s){
+ this.L = s;
+ this.man = new Punt(null);
+ }
+
+ @Override
+ public void setPosition(Punt p){
+ this.man = p;
+ }
+
+ @Override
+ public void teken(){
+ for(int j = 0; j < L.getHeight(); j++){
+ for(int i = 0; i < L.getWidth(); i++){
+ Fragment f = L.getFragment(i,j);
+ if(i == man.getX() && j == man.getY()){
+ System.out.print('*');
+ } else if (f == null) {
+ System.out.print(' ');
+ } else {
+ switch (f){
+ case KR: System.out.print('+'); break;
+ case NZ: System.out.print('|'); break;
+ case OW: System.out.print('-'); break;
+ case NO: System.out.print('`'); break;
+ case NW: System.out.print(','); break;
+ case ZO: System.out.print(','); break;
+ case ZW: System.out.print('.'); break;
+ }
+ }
+ }
+ System.out.print("\n");
+ }
+ }
+}
diff --git a/Week4 Drawing loipes/src/oo15loipe/Fragment.java b/Week4 Drawing loipes/src/oo15loipe/Fragment.java new file mode 100644 index 0000000..a4668da --- /dev/null +++ b/Week4 Drawing loipes/src/oo15loipe/Fragment.java @@ -0,0 +1,8 @@ +package oo15loipe;
+
+/**
+ * enum to describe a fragment of path by its direction (or as a crossing)
+ *
+ * @author Thijs Heijligenberg, s4451414, Camil Staps, s4498062
+ */
+public enum Fragment {NZ, OW, NO,NW, ZO, ZW, KR};
diff --git a/Week4 Drawing loipes/src/oo15loipe/InfoLoipe.java b/Week4 Drawing loipes/src/oo15loipe/InfoLoipe.java new file mode 100644 index 0000000..a1cf182 --- /dev/null +++ b/Week4 Drawing loipes/src/oo15loipe/InfoLoipe.java @@ -0,0 +1,35 @@ +package oo15loipe; + +/** + * Interface for a class describing a Loipe + * + * @author Camil Staps, s4498062, Thijs Heijligenberg, s4451414 + */ +public interface InfoLoipe { + + public int getWidth( ) ; // grootte in oostwest r i cht ing + public int getHeight( ) ; // grootte in noordzuid r i cht ing + + /** + * Get the fragment at position (x, y) + * + * @param x + * @param y + * @return + */ + public Fragment getFragment( int x, int y) ; // fragment van de loipe op po s i t i e (x , y) + + /** + * Get the first position of the loipe + * + * @return + */ + public Punt start( ) ; // Het startpunt op de kaart + + /** + * Get the next position in the loipe + * @return + */ + public Punt stap( ) ; // het volgende punt op de route +} + diff --git a/Week4 Drawing loipes/src/oo15loipe/Loipe.java b/Week4 Drawing loipes/src/oo15loipe/Loipe.java new file mode 100644 index 0000000..9155988 --- /dev/null +++ b/Week4 Drawing loipes/src/oo15loipe/Loipe.java @@ -0,0 +1,226 @@ +package oo15loipe; + +/** + * Loipe class + * + * @author Camil Staps, s4498062 // Thijs Heijligenberg, s4451414 + */ +public class Loipe implements InfoLoipe { + + /** + * Class constants for direction (for readability) + */ + private final static int + DIRECTION_EAST = 0, + DIRECTION_SOUTH = 1, + DIRECTION_WEST = 2, + DIRECTION_NORTH = 3; + + /** + * The height and width of the loipe + */ + private int height = 0, width = 0; + + /** + * Start point + * The path in coordinates + * The current position + * The map as 2D array of Fragments + */ + private Punt start; + private Punt[] path; + private int path_pointer = 0; + private Fragment[][] loipe; + + /** + * Construct the loipe based on a path + * + * @param pad + */ + public Loipe (String pad) { + setLoipe(pad); + } + + /** + * Refresh everything based on a new path + * + * @param pad + */ + public final void setLoipe(String pad) { + setWidthAndHeight(pad); + fillLoipe(pad); + + path_pointer = 0; + } + + /** + * Set the width and height of the loipe based on a path + * + * @param pad + */ + private void setWidthAndHeight(String pad) { + int direction = DIRECTION_NORTH; + + int maxX = 0, maxY = 0, minX = 0, minY = 0; + Punt current_point = new Punt(0,0); + + for (int i = 0; i < pad.length(); i++) { + if (current_point.getX() < minX) + minX = current_point.getX(); + if (current_point.getX() > maxX) + maxX = current_point.getX(); + if (current_point.getY() < minY) + minY = current_point.getY(); + if (current_point.getY() > maxY) + maxY = current_point.getY(); + + switch (pad.charAt(i)) { + case 'r': + switch (direction) { + case DIRECTION_EAST: direction = DIRECTION_SOUTH; break; + case DIRECTION_SOUTH: direction = DIRECTION_WEST; break; + case DIRECTION_WEST: direction = DIRECTION_NORTH; break; + case DIRECTION_NORTH: direction = DIRECTION_EAST; break; + } + break; + case 'l': + switch (direction) { + case DIRECTION_EAST: direction = DIRECTION_NORTH; break; + case DIRECTION_SOUTH: direction = DIRECTION_EAST; break; + case DIRECTION_WEST: direction = DIRECTION_SOUTH; break; + case DIRECTION_NORTH: direction = DIRECTION_WEST; break; + } + } + + switch (direction) { + case DIRECTION_EAST: current_point = new Punt(current_point.getX() + 1, current_point.getY()); break; + case DIRECTION_SOUTH: current_point = new Punt(current_point.getX(), current_point.getY() + 1); break; + case DIRECTION_WEST: current_point = new Punt(current_point.getX() - 1, current_point.getY()); break; + case DIRECTION_NORTH: current_point = new Punt(current_point.getX(), current_point.getY() - 1); break; + } + } + + height = maxY - minY + 1; + width = maxX - minX + 1; + + start = new Punt(-minX, -minY); + } + + /** + * Fill the loipe with fragments based on a path. + * Assumes the width, height and start attributes to be correct. + * + * @param pad + */ + private void fillLoipe(String pad) { + loipe = new Fragment[width + 1][height + 1]; + path = new Punt[pad.length()]; + Punt current_point = start; + int direction = DIRECTION_NORTH; + + for (int i = 0; i < pad.length(); i++) { + path[i] = current_point; + + if (loipe[current_point.getX()][current_point.getY()] != null) { + loipe[current_point.getX()][current_point.getY()] = Fragment.KR; + } else { + switch (pad.charAt(i)) { + case 'r': + switch (direction) { + case DIRECTION_EAST: loipe[current_point.getX()][current_point.getY()] = Fragment.ZW; break; + case DIRECTION_SOUTH: loipe[current_point.getX()][current_point.getY()] = Fragment.NW; break; + case DIRECTION_WEST: loipe[current_point.getX()][current_point.getY()] = Fragment.NO; break; + case DIRECTION_NORTH: loipe[current_point.getX()][current_point.getY()] = Fragment.ZO; break; + } + break; + case 'l': + switch (direction) { + case DIRECTION_EAST: loipe[current_point.getX()][current_point.getY()] = Fragment.NW; break; + case DIRECTION_SOUTH: loipe[current_point.getX()][current_point.getY()] = Fragment.NO; break; + case DIRECTION_WEST: loipe[current_point.getX()][current_point.getY()] = Fragment.ZO; break; + case DIRECTION_NORTH: loipe[current_point.getX()][current_point.getY()] = Fragment.ZW; break; + } + break; + case 's': + switch (direction) { + case DIRECTION_EAST: + case DIRECTION_WEST: + loipe[current_point.getX()][current_point.getY()] = Fragment.OW; + break; + case DIRECTION_SOUTH: + case DIRECTION_NORTH: + loipe[current_point.getX()][current_point.getY()] = Fragment.NZ; + } + } + } + + switch (pad.charAt(i)) { + case 'r': + switch (direction) { + case DIRECTION_EAST: direction = DIRECTION_SOUTH; break; + case DIRECTION_SOUTH: direction = DIRECTION_WEST; break; + case DIRECTION_WEST: direction = DIRECTION_NORTH; break; + case DIRECTION_NORTH: direction = DIRECTION_EAST; break; + } + break; + case 'l': + switch (direction) { + case DIRECTION_EAST: direction = DIRECTION_NORTH; break; + case DIRECTION_SOUTH: direction = DIRECTION_EAST; break; + case DIRECTION_WEST: direction = DIRECTION_SOUTH; break; + case DIRECTION_NORTH: direction = DIRECTION_WEST; break; + } + } + + switch (direction) { + case DIRECTION_EAST: current_point = new Punt(current_point.getX() + 1, current_point.getY()); break; + case DIRECTION_SOUTH: current_point = new Punt(current_point.getX(), current_point.getY() + 1); break; + case DIRECTION_WEST: current_point = new Punt(current_point.getX() - 1, current_point.getY()); break; + case DIRECTION_NORTH: current_point = new Punt(current_point.getX(), current_point.getY() - 1); break; + } + } + } + + @Override + public int getHeight() { + return height; + } + + @Override + public int getWidth() { + return width; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + for (Punt p : path) { + sb.append(p).append(" -> "); + } + return sb.toString(); + } + + @Override + public Fragment getFragment(int x, int y) { + return loipe[x][y]; + } + + @Override + public Punt start() { + return start; + } + + /** + * We chose to have the man go back to the start when he has finished the loipe. + */ + @Override + public Punt stap() { + if (path_pointer >= path.length) { + path_pointer = 0; + } + Punt result = path[path_pointer]; + path_pointer++; + return result; + } + +} diff --git a/Week4 Drawing loipes/src/oo15loipe/LoipePlaatje.java b/Week4 Drawing loipes/src/oo15loipe/LoipePlaatje.java new file mode 100644 index 0000000..db70d28 --- /dev/null +++ b/Week4 Drawing loipes/src/oo15loipe/LoipePlaatje.java @@ -0,0 +1,191 @@ +package oo15loipe; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.net.URL; +import javax.swing.ImageIcon; +import javax.swing.JFrame; +import javax.swing.JPanel; + +/** + * + * @author Pieter Koopman + */ +public class LoipePlaatje extends JFrame implements TekenLoipe +{ + /** Used for painting the tiles. + * We assume that all tiles have same width and height. + */ + private final int UNIT; + + /** Color for painting the background. */ + private static final Color SNOW = new Color(0xF0,0xF0,0xFF); + + /** + * The icons for drawing + */ + private final ImageIcon nz, ow, no, nw, zo, zw, nzow; + + private JPanel panel; + + /** + * the loipe to be painted and a boolean indicating whether it is okay + */ + private InfoLoipe loipe; + private boolean loipeOK = true; + /** + * previous position for walking the loipe + */ + private Punt previousPos = null; + + /** + * Constructor. Fill all icons. + */ + public LoipePlaatje(InfoLoipe s) { + super("Loipe"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + nz = plaatje("nz"); + ow = plaatje("ow"); + no = plaatje("no"); + nw = plaatje("nw"); + zo = plaatje("zo"); + zw = plaatje("zw"); + nzow = plaatje("nzow"); + UNIT = nz.getIconWidth(); + setLoipe(s); + if (loipeOK) { + panel = new JPanel(); + add(panel); + } + } + + /** + * Creates a LoipePlaatje object based on the track information in <code>Loipe</code>. + * @param s this Loipe contains the 2D track information + */ + public void setLoipe (InfoLoipe s) { + loipe = s; + checkLoipe(); + setBackground(SNOW); + } + + /** + * Checks validity of the input track description. Terminates normally + * when the track is valid, prints a message otherwise. + */ + private void checkLoipe() { + if (loipe == null) + { + System.err.println("Illegale loipe: null pointer"); + loipeOK = false; + } + else if (loipe.getHeight() == 0) + { + System.err.println("Illegale loipe: height should be at least 1"); + loipeOK = false; + } + else if (loipe.getWidth() == 0) + { + System.err.println("Illegale loipe: width should be at least 1"); + loipeOK = false; + } + } + /** + * Gets the preferred size of this component. + * + * @return a dimension object indicating this component's preferred size. + */ + @Override + public Dimension getPreferredSize() { + return new Dimension(loipe.getWidth() * UNIT, loipe.getHeight() * UNIT); + } + + /** + * try to read a picture with the given name + * @param naam "name.png" is filename of picture + * @return the picture + * @throws IllegalArgumentexception if file is not found + */ + private ImageIcon plaatje (String name) { + name += ".png"; + URL resource = getClass().getClassLoader().getResource(name); + if (resource != null) { + return new ImageIcon (resource); + } else { + System.err.printf ("ERROR: Resource \"%s\" not found... aborting...\n", name); + System.err.println("For NetBeans the file should be placed in the src folder."); + throw new IllegalArgumentException ("Image " + name + " not found!"); + } + } + + /** + * Update graphics according to stored track. + * @param g the graphics context to use for painting. + */ + @Override + public void paint(Graphics g) { + super.paint(g); + g = panel.getGraphics(); + for (int i = 0; i < loipe.getWidth(); i += 1) { + for (int j = 0; j < loipe.getHeight(); j += 1) { + tekenFragment(g, i, j); + } + } + if (previousPos != null) { + g.setColor(Color.RED); + g.fillOval(previousPos.getX() * UNIT + UNIT / 4, previousPos.getY() * UNIT + UNIT / 4, UNIT / 2, UNIT / 2); + } + } + + /** + * Draw the fragment from the loipe at the given position + * @param g + * @param x + * @param y + */ + private void tekenFragment(Graphics g, int x, int y) { + Fragment f = loipe.getFragment(x, y); + ImageIcon plaatje; + if (f != null) { + switch (f) { + case NZ: plaatje = nz; break; + case OW: plaatje = ow; break; + case NO: plaatje = no; break; + case NW: plaatje = nw; break; + case ZO: plaatje = zo; break; + case ZW: plaatje = zw; break; + default: plaatje = nzow; + } + if (plaatje != null) { + plaatje.paintIcon(this, g, x * UNIT, y * UNIT); + } + } + } + /** + * The method to call in order to draw a window with the given track. + * You are supposed to use this method once for each object. + */ + @Override + public void teken() { + if (loipeOK) { + setSize(loipe.getWidth() * UNIT, loipe.getHeight() * UNIT + UNIT / 2); + setVisible(true); // make frame visible + } + else + System.err.println("LoipePlaatje kan niet tekenen. Loipe is ongeldig of plaatjes niet gevonden"); + } + + @Override + public void setPosition(Punt p) { + Graphics g = panel.getGraphics(); + if (previousPos != null) { + g.setColor(SNOW); + g.fillRect(previousPos.getX() * UNIT, previousPos.getY() * UNIT, UNIT, UNIT); + tekenFragment(g,previousPos.getX(), previousPos.getY()); + } + g.setColor(Color.RED); + g.fillOval(p.getX() * UNIT + UNIT / 4, p.getY() * UNIT + UNIT / 4, UNIT / 2, UNIT / 2); + previousPos = p; + } +}
\ No newline at end of file diff --git a/Week4 Drawing loipes/src/oo15loipe/Punt.java b/Week4 Drawing loipes/src/oo15loipe/Punt.java new file mode 100644 index 0000000..2c94f1f --- /dev/null +++ b/Week4 Drawing loipes/src/oo15loipe/Punt.java @@ -0,0 +1,73 @@ +package oo15loipe; + +/** + * Een Punt in 2D + * @author pieter koopman + */ + +public class Punt { + private int x, y; + + /** + * de gewone constructor + * @param i: x + * @param j; y + */ + public Punt(int i, int j) { + x = i; + y = j; + } + + /** + * copy constructor + * @param p + */ + public Punt (Punt p) { + if (p != null) { + x = p.x; + y = p.y; + } else { + x = y = 0; + } + } + + /** + * getter voor x + * @return x + */ + public int getX (){ + return x; + } + + /** + * getter voor y + * @return y + */ + public int getY (){ + return y; + } + + /** + * equals methode vergelijkt x en y + * @param o + * @return + */ + @Override + public boolean equals(Object o) { + if (o != null && o instanceof Punt) { + Punt p = (Punt) o; + return x == p.x && y == p.y; + } else { + return false; + } + } + + /** + * Punt naar String conversie + * @return Strin met waarde van x en y + */ + @Override + public String toString () { + return "(" + x + "," + y + ")"; + } +} diff --git a/Week4 Drawing loipes/src/oo15loipe/TekenLoipe.java b/Week4 Drawing loipes/src/oo15loipe/TekenLoipe.java new file mode 100644 index 0000000..8c9386f --- /dev/null +++ b/Week4 Drawing loipes/src/oo15loipe/TekenLoipe.java @@ -0,0 +1,19 @@ +package oo15loipe; + +/** + * Interface for a class that can show a loipe on the screen + * + * @author Thijs Heijligenberg s4451414, Camil Staps, s4498062 + */ +public interface TekenLoipe { + /** + * Draw the loipe + */ + public void teken() ; // teken de gegeven loipe + + /** + * Set the current position of the skier + * @param p + */ + public void setPosition(Punt p) ; // teken de spor ter op de gegeven po s i t i e +} diff --git a/Week4 Drawing loipes/src/ow.png b/Week4 Drawing loipes/src/ow.png Binary files differnew file mode 100644 index 0000000..8d35019 --- /dev/null +++ b/Week4 Drawing loipes/src/ow.png diff --git a/Week4 Drawing loipes/src/week4/Week4.java b/Week4 Drawing loipes/src/week4/Week4.java new file mode 100644 index 0000000..e523a54 --- /dev/null +++ b/Week4 Drawing loipes/src/week4/Week4.java @@ -0,0 +1,36 @@ +package week4; + +import java.util.Scanner; +import oo15loipe.AsciiArt; +import oo15loipe.Loipe; +import oo15loipe.LoipePlaatje; + +/** + * Solutions for assignment 4 + * + * @author Thijs Heijligenberg, s4451414, Camil Staps, s4498062 + */ +public class Week4 { + + /** + * Create a Loipe, draw it using LoipePlaatje and AsciiArt + * + * @param args the command line arguments (are ignored) + */ + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + Loipe l = new Loipe("srssrsslsllsss"); + + LoipePlaatje lp = new LoipePlaatje(l); + lp.teken(); + + AsciiArt aa = new AsciiArt(l); + do { + aa.setPosition(l.stap()); + aa.teken(); + } while (sc.nextLine() != null); + + } + +} diff --git a/Week4 Drawing loipes/src/zo.png b/Week4 Drawing loipes/src/zo.png Binary files differnew file mode 100644 index 0000000..1b578c0 --- /dev/null +++ b/Week4 Drawing loipes/src/zo.png diff --git a/Week4 Drawing loipes/src/zw.png b/Week4 Drawing loipes/src/zw.png Binary files differnew file mode 100644 index 0000000..702e57d --- /dev/null +++ b/Week4 Drawing loipes/src/zw.png |