diff options
-rw-r--r-- | Week4/src/oo15loipe/LoipePlaatje.java | 191 | ||||
-rw-r--r-- | Week4/src/week4/Punt.java | 71 |
2 files changed, 262 insertions, 0 deletions
diff --git a/Week4/src/oo15loipe/LoipePlaatje.java b/Week4/src/oo15loipe/LoipePlaatje.java new file mode 100644 index 0000000..3166309 --- /dev/null +++ b/Week4/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 Point previousPos = null; + + /** + * Constructor. Fill all icons. + */ + public LoipePlaatje(InfoLoipe s) { + super("Kaart"); + 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(Point 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/src/week4/Punt.java b/Week4/src/week4/Punt.java new file mode 100644 index 0000000..daaddd6 --- /dev/null +++ b/Week4/src/week4/Punt.java @@ -0,0 +1,71 @@ +/** + * 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 + ")"; + } +} |