This commit is contained in:
Luca Conte 2023-04-05 17:35:23 +02:00
parent ac563779c2
commit 1abb1b50ec
6 changed files with 143 additions and 0 deletions

11
z02/.classpath Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="/home/stud/p8q-yhw-u1/Downloads/DrawingPanel.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

1
z02/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/bin/

17
z02/.project Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>z02</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -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

79
z02/src/Loc.java Normal file
View File

@ -0,0 +1,79 @@
import java.awt.Graphics;
/** Klasseninvariante: x&gt;=0 und y&gt;=0. */
public class Loc {
private int x;
private int y;
/** Initialisiert das Objekt mit gegebenen Koordinatenwerten.
* @param initialX Vorbedingung: &gt;= 0
* @param initialY Vorbedigung: &gt;= 0.
* @exception IllegalArgumentException falls einer der Eingabeparameter ung&uuml;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: &gt;= 0
* @param newy Vorbedigung: &gt;= 0.
* @exception IllegalArgumentException falls einer der Eingabeparameter ung&uuml;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;
}
}

21
z02/src/Main.java Normal file
View File

@ -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<N; i++) {
Loc loc= new Loc(rand.nextInt(W), rand.nextInt(H));
int index= loc.getSector();
g.setColor(colors[index]);
loc.draw(g);
}
}
}