From 1abb1b50ec090d945363864a1dac5910bbee4403 Mon Sep 17 00:00:00 2001 From: Luca Conte Date: Wed, 5 Apr 2023 17:35:23 +0200 Subject: [PATCH] z02 --- z02/.classpath | 11 ++++ z02/.gitignore | 1 + z02/.project | 17 +++++ z02/.settings/org.eclipse.jdt.core.prefs | 14 +++++ z02/src/Loc.java | 79 ++++++++++++++++++++++++ z02/src/Main.java | 21 +++++++ 6 files changed, 143 insertions(+) create mode 100644 z02/.classpath create mode 100644 z02/.gitignore create mode 100644 z02/.project create mode 100644 z02/.settings/org.eclipse.jdt.core.prefs create mode 100644 z02/src/Loc.java create mode 100644 z02/src/Main.java diff --git a/z02/.classpath b/z02/.classpath new file mode 100644 index 0000000..e933641 --- /dev/null +++ b/z02/.classpath @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/z02/.gitignore b/z02/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/z02/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/z02/.project b/z02/.project new file mode 100644 index 0000000..0e2cacf --- /dev/null +++ b/z02/.project @@ -0,0 +1,17 @@ + + + z02 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/z02/.settings/org.eclipse.jdt.core.prefs b/z02/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..8c9943d --- /dev/null +++ b/z02/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=17 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=17 diff --git a/z02/src/Loc.java b/z02/src/Loc.java new file mode 100644 index 0000000..fd1fe4f --- /dev/null +++ b/z02/src/Loc.java @@ -0,0 +1,79 @@ +import java.awt.Graphics; + +/** Klasseninvariante: x>=0 und y>=0. */ + +public class Loc { + private int x; + private int y; + + + /** Initialisiert das Objekt mit gegebenen Koordinatenwerten. + * @param initialX Vorbedingung: >= 0 + * @param initialY Vorbedigung: >= 0. + * @exception IllegalArgumentException falls einer der Eingabeparameter ungültig ist. + */ + public Loc(int initialX, int initialY) { + setLocation(initialX, initialY); + } + + public Loc() { + this(0, 0); + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + + public void draw(Graphics g) { + g.fillOval(x, y, 3, 3); + //g.drawString("(" + x + ", " + y + ")", x, y); + } + + /** Setzt die Koordinaten auf neue Werte. + * @param newx Vorbedingung: >= 0 + * @param newy Vorbedigung: >= 0. + * @exception IllegalArgumentException falls einer der Eingabeparameter ungültig ist. + */ + public void setLocation(int newx, int newy) { + if (newx < 0 || newy < 0) { + throw new IllegalArgumentException("x und y müssen >= 0 sein"); + } + x = newx; + y = newy; + } + + public void translate(int dx, int dy) { + setLocation(x + dx, y + dy); + } + + public double distance(Loc other) { + int dx = x - other.x; + int dy = y - other.y; + return Math.sqrt(dx * dx + dy * dy); + } + + public double distanceFromOrigin() { + Loc origin = new Loc(); + return distance(origin); + } + + public String toString() { + return "(" + x + ", " + y + ")"; + } + + /** + * gibt einen integer zwischen 0 und 8 abhängig vom Winkel des Punktes zur x-Achse zurück + */ + public int getSector() { + double alpha = Math.atan2(this.y, this.x); + int sector = (int) Math.floor(alpha / (Math.PI / 2) * 9); + if (sector > 8) { + return 8; + } + return sector; + } +} \ No newline at end of file diff --git a/z02/src/Main.java b/z02/src/Main.java new file mode 100644 index 0000000..8106b68 --- /dev/null +++ b/z02/src/Main.java @@ -0,0 +1,21 @@ +import com.bjp.*; +import java.util.*; +import java.awt.*; + +public class Main { + public static void main(String[] args) { + Color[] colors= { Color.RED, Color.BLUE, Color.CYAN, Color.GRAY, Color.GREEN, Color.MAGENTA, Color.ORANGE, Color.BLACK, Color.YELLOW }; + final int W=400; + final int H=300; + DrawingPanel pnl= new DrawingPanel(W,H); + Graphics g= pnl.getGraphics(); + final int N= 5000; + Random rand= new Random(); + for (int i=0; i