import java.awt.*; import java.util.*; public class Port extends Portal { private double nView = 400.0; private double nPort = -160.0; public Port(Scene scn) { super(scn); } public void paintScene(Graphics gr) { Enumeration enum = _scn.elements(); while (enum.hasMoreElements()) { Object obj = enum.nextElement(); if (obj instanceof Coordinates) { paintScene(gr, (Coordinates)obj); } else if (obj instanceof Vector) { Vector vec = (Vector)obj; paintScene(gr, vec.elements()); } else if (obj instanceof Scenery) { Scenery sc = (Scenery)obj; paintScene(gr, sc.elements()); } } } private void paintScene(Graphics gr, Coordinates coords) { coords = perspectivate(coords); if (!isVisible(coords)) return; Dimension dim = size(); int w = dim.width; int h = dim.height; int tx = (int)coords.x; int ty = (int)coords.y; int tz = (int)coords.z; gr.fillRect(tx + w/2 - 2, ty + h/2 - 2, 5, 5); } private void paintScene(Graphics gr, Enumeration enum) { boolean fVisible = false; boolean fWasVisible = true; int x = 0; int y = 0; while (enum.hasMoreElements()) { Dimension dim = size(); Coordinates coords = (Coordinates)enum.nextElement(); coords = perspectivate(coords); if (coords == null) { x = 0; y = 0; fVisible = false; continue; } int w = dim.width; int h = dim.height; int tx = (int)coords.x; int ty = (int)coords.y; int tz = (int)coords.z; if (fVisible && fWasVisible && isVisible(coords)) gr.drawLine(x + w/2, y + h/2, tx + w/2, ty + h/2); fWasVisible = isVisible(coords); fVisible = true; x = tx; y = ty; } } private Coordinates perspectivate(Coordinates coords) { if (coords == null) return null; coords = new Coordinates(coords); coords.x = coords.x * Math.abs(nView / (nView + nPort - coords.z)); coords.y = coords.y * Math.abs(nView / (nView + nPort - coords.z)); return coords; } private boolean isVisible(Coordinates coords) { return coords.z < nView + nPort; } public boolean keyDown(Event e, int nKey) { boolean fRepaint = false; Transformation tr = null; double pi = Math.PI; switch (nKey) { case Event.UP: fRepaint = true; if (e.shiftDown()) tr = new Translation(0.0, -5.0, 0.0); else tr = new Rotation(0.0, 0.0, 0.0, 1.0, 0.0, 0.0, -pi/20.0); break; case Event.DOWN: fRepaint = true; if (e.shiftDown()) tr = new Translation(0.0, 5.0, 0.0); else tr = new Rotation(0.0, 0.0, 0.0, 1.0, 0.0, 0.0, pi/20.0); break; case Event.RIGHT: fRepaint = true; if (e.shiftDown()) tr = new Translation(5.0, 0.0, 0.0); else tr = new Rotation(0.0, 0.0, 0.0, 0.0, 1.0, 0.0, -pi/20.0); break; case Event.LEFT: fRepaint = true; if (e.shiftDown()) tr = new Translation(-5.0, 0.0, 0.0); else tr = new Rotation(0.0, 0.0, 0.0, 0.0, 1.0, 0.0, pi/20.0); break; } if (fRepaint == false) return false; repaint(); if (tr == null) return true; _scn.transform(tr); return true; } }