Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
titleBaseSpaceObject
collapsetrue
package inheritance;

import javafx.collections.ObservableList;
import javafx.geometry.Bounds;
import javafx.geometry.Point2D;
import javafx.scene.shape.Polygon;

public class BaseSpaceObject extends Polygon {
    /**
     * Adds the point given by x,y to this Polygon
     * @param x the x-coordinate
     * @param y the y-coordinate
     */
    protected void addPoint(double x, double y) {
        getPoints().add(x);
        getPoints().add(y);
    }
    /**
     * Adds the points given by the sequence of x- and y-coordinates
     * @param xys the x- and y-coordinates
     */
    protected void addPoints(double... xys) {
        for (int i = 0; i < xys.length; i += 2) {
            addPoint(xys[i], xys[i + 1]);
        }
    }
    
    /**
     * Adds the point given by angle, length in polar coordinates
     * @param angle the angle
     * @param length the length
     */
    protected void addPolarPoint(double angle, double length) {
        addPoint(Math.cos(angle) * length, Math.sin(angle) * length);
    }
    
    /**
     * Returns the position of this Polygon as a Point2D object
     * @return the position as a Point2D object
     */
    public Point2D getPosition() {
        return new Point2D(getTranslateX(), getTranslateY());
    }
    /**
     * Moves (displaces) this Polygon by the given dx, dy
     * @param dx the x displacement
     * @param dy the y displacement
     */
    public void translate(double dx, double dy) {
        setTranslateX(getTranslateX() + dx);
        setTranslateY(getTranslateY() + dy);
    }
    
    /**
     * Returns the position of the center of this Polygon.
     * The asParentCoordinates determines if the position is transformed into the parent coordinate system.
     * @param asParentCoordinates determines if the position is transformed into the parent coordinate system
     * @return the center position
     */
    public Point2D getCenter(boolean asParentCoordinates) {
        Bounds bounds = getBoundsInLocal();
        Point2D center = new Point2D((bounds.getMaxX() + bounds.getMinX()) / 2, (bounds.getMaxY() + bounds.getMinY()) / 2);
        if (asParentCoordinates) {
            center = localToParent(center);
        }
        return center;
    }
    /**
     * Returns the number of points in this Polygon
     * @return the number of points in this Polygon
     */
    public int getPointCount() {
        return getPoints().size() / 2;
    }
    /**
     * Determines of a specific point of another Polygon is inside this Polygon
     * @param other the other Polygon
     * @param pointNum the number of the point to check
     * @return
     */
    public boolean contains(BaseSpaceObject other, int pointNum) {
        ObservableList<Double> points = other.getPoints();
        double x = points.get(pointNum * 2), y = points.get(pointNum * 2 + 1);
        return this.contains(parentToLocal(other.localToParent(x, y)));
    }
}
Code Block
languagejava
titleAsteroids-klassen
collapsetrue
package inheritance;

import java.util.ArrayList;
import java.util.List;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Point2D;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.util.Duration;

public class Asteroids extends Pane implements EventHandler<KeyEvent>  {

	private List<SpaceObject> spaceObjects;
    
    protected void add(SpaceObject so, double x, double y) {
        so.translate(x, y);
        getChildren().add(so);
        spaceObjects.add(so);
    }
    public void init() {
        spaceObjects = new ArrayList<SpaceObject>();
    }

	public void run() {
		// setter opp simuleringstakt
        Timeline tickTimer = new Timeline(new KeyFrame(Duration.millis(50), new EventHandler<ActionEvent>() {
    		private int tickCount = 0;
            @Override
            public void handle(ActionEvent event) {
                tick(tickCount++);
            }
        }));
        tickTimer.setCycleCount(Timeline.INDEFINITE);
        tickTimer.play();
        requestFocus();
        setOnKeyPressed(this);
        setOnKeyTyped(this);
    }

	// kalles når spesialtaster som pilene trykkes
	protected void handleKey(KeyCode keyCode) {
    }

	// kalles når vanlige bokstavtaster trykkes
    protected void handleKey(String character) {
        if ("+".equals(character)) {
            zoom(2);
        } else if ("-".equals(character)) {
            zoom(0.5);
        }
    }
    
    private void zoom(double factor) {
        setScaleX(getScaleX() * factor);
        setScaleY(getScaleY() * factor);
    }
    
    @Override
    public void handle(KeyEvent keyEvent) {
        if (keyEvent.getCode() == KeyCode.UNDEFINED) {
            handleKey(keyEvent.getCharacter());
        } else {
            handleKey(keyEvent.getCode());
        }
    }

	private void tick(int tickCount) {
        // accelerate due to gravity

		// lag en (dobbel) løkke her, som
		// håndterer gravitasjonskraften for alle par i spaceObjects-lista 

        // move all objects
        for (SpaceObject spaceObject : spaceObjects) {
            spaceObject.tick();
        }

        // check for collision
		// lag en (dobbel) løkke her, som
		// sjekker for og håndterer kollisjoner for alle par i spaceObjects-lista
    }

	public void handleGravity(SpaceObject spaceObject1, SpaceObject spaceObject2) {
		// regn ut avstanden mellom spaceObject1 og spaceObject2 (bruk getCenter-metoden),
		// beregn den gjensidige gravitasjonskraften basert på formelen
		// masse1 * masse2 / avstand ^ 2, og
		// påfør kraften med SpaceObject.applyForce-metoden
    }
}
Code Block
languagejava
titleAsteroidsProgram
collapsetrue
package inheritance;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class AsteroidsProgram extends Application {
    private int width = 800, height = 600;
    @Override
    public void start(Stage stage) throws Exception {
        String paneClassName = getParameters().getRaw().get(0);
        final Asteroids asteroidsPane = (Asteroids) Class.forName(paneClassName).newInstance();
        asteroidsPane.setPrefSize(width, height);
        Scene scene = new Scene(asteroidsPane, width, height, Color.BLACK);
        stage.setScene(scene);
        stage.show();
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                asteroidsPane.init();
                asteroidsPane.run();
            }
        });
    }
    
    public static void main(String[] args) {
        launch(AsteroidsProgram.class, args);
    }
}