mirror of
https://github.com/jneug/zeichenmaschine.git
synced 2026-04-14 14:43:33 +02:00
Javadoc
This commit is contained in:
@@ -4,29 +4,30 @@ import schule.ngb.zm.Color;
|
|||||||
import schule.ngb.zm.Constants;
|
import schule.ngb.zm.Constants;
|
||||||
import schule.ngb.zm.Zeichenmaschine;
|
import schule.ngb.zm.Zeichenmaschine;
|
||||||
import schule.ngb.zm.util.io.FileLoader;
|
import schule.ngb.zm.util.io.FileLoader;
|
||||||
import schule.ngb.zm.util.io.FontLoader;
|
|
||||||
import schule.ngb.zm.util.io.ImageLoader;
|
import schule.ngb.zm.util.io.ImageLoader;
|
||||||
|
|
||||||
import java.awt.Font;
|
import java.awt.Font;
|
||||||
import java.awt.FontMetrics;
|
import java.awt.FontMetrics;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.font.LineMetrics;
|
import java.awt.font.LineMetrics;
|
||||||
import java.awt.geom.Rectangle2D;
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hilfsklasse, um zufällige Beispieldaten zu erzeugen.
|
||||||
|
* <p>
|
||||||
|
* Die Klasse kann verschiedene Arten realistischer Beispieldaten erzeugen,
|
||||||
|
* unter anderem Namen, E-Mail-Adressen, Passwörter oder Platzhalterbilder.
|
||||||
|
*/
|
||||||
public final class Faker {
|
public final class Faker {
|
||||||
|
|
||||||
public static final String FAKE_IMG_URL = "https://loremflickr.com/%d/%d";
|
public static final String FAKE_IMG_URL = "https://loremflickr.com/%d/%d";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Erzeugt ein Array mit den angegebenen Anzahl zufälliger Nutzernamen.
|
||||||
public static void main( String[] args ) {
|
*
|
||||||
Zeichenmaschine zm = new Zeichenmaschine(640, 480, "Faker");
|
* @param n Anzahl der Beispieldaten.
|
||||||
zm.getDrawingLayer().setAnchor(Constants.NORTHWEST);
|
* @return Ein Array mit den Beispieldaten.
|
||||||
zm.getDrawingLayer().image(Faker.fakeImage(zm.getWidth(), zm.getHeight()), 0, 0);
|
*/
|
||||||
zm.redraw();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String[][] fakeUsers( int n ) {
|
public static String[][] fakeUsers( int n ) {
|
||||||
String[][] data = loadMockfile("users");
|
String[][] data = loadMockfile("users");
|
||||||
String[][] result = new String[n][];
|
String[][] result = new String[n][];
|
||||||
@@ -36,6 +37,12 @@ public final class Faker {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Erzeugt ein Array mit den angegebenen Anzahl zufälliger Vornamen.
|
||||||
|
*
|
||||||
|
* @param n Anzahl der Beispieldaten.
|
||||||
|
* @return Ein Array mit den Beispieldaten.
|
||||||
|
*/
|
||||||
public static String[] fakeNames( int n ) {
|
public static String[] fakeNames( int n ) {
|
||||||
String[][] data = loadMockfile("users");
|
String[][] data = loadMockfile("users");
|
||||||
String[] result = new String[n];
|
String[] result = new String[n];
|
||||||
@@ -46,6 +53,14 @@ public final class Faker {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Erzeugt ein Array mit den angegebenen Anzahl zufälliger Namen (Vor- und
|
||||||
|
* Nachname).
|
||||||
|
*
|
||||||
|
* @param n Anzahl der Beispieldaten.
|
||||||
|
* @return Ein Array mit den Beispieldaten.
|
||||||
|
*/
|
||||||
public static String[] fakeFullnames( int n ) {
|
public static String[] fakeFullnames( int n ) {
|
||||||
String[][] data = loadMockfile("users");
|
String[][] data = loadMockfile("users");
|
||||||
String[] result = new String[n];
|
String[] result = new String[n];
|
||||||
@@ -56,6 +71,13 @@ public final class Faker {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Erzeugt ein Array mit den angegebenen Anzahl zufälliger Nutzernamen.
|
||||||
|
*
|
||||||
|
* @param n Anzahl der Beispieldaten.
|
||||||
|
* @return Ein Array mit den Beispieldaten.
|
||||||
|
*/
|
||||||
public static String[] fakeUsernames( int n ) {
|
public static String[] fakeUsernames( int n ) {
|
||||||
String[][] data = loadMockfile("users");
|
String[][] data = loadMockfile("users");
|
||||||
String[] result = new String[n];
|
String[] result = new String[n];
|
||||||
@@ -66,6 +88,12 @@ public final class Faker {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Erzeugt ein Array mit den angegebenen Anzahl zufälliger Passwörter.
|
||||||
|
*
|
||||||
|
* @param n Anzahl der Beispieldaten.
|
||||||
|
* @return Ein Array mit den Beispieldaten.
|
||||||
|
*/
|
||||||
public static String[] fakePasswords( int n ) {
|
public static String[] fakePasswords( int n ) {
|
||||||
String[][] data = loadMockfile("users");
|
String[][] data = loadMockfile("users");
|
||||||
String[] result = new String[n];
|
String[] result = new String[n];
|
||||||
@@ -76,6 +104,12 @@ public final class Faker {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Erzeugt ein Array mit den angegebenen Anzahl zufälliger E-Mail-Adressen.
|
||||||
|
*
|
||||||
|
* @param n Anzahl der Beispieldaten.
|
||||||
|
* @return Ein Array mit den Beispieldaten.
|
||||||
|
*/
|
||||||
public static String[] fakeEmails( int n ) {
|
public static String[] fakeEmails( int n ) {
|
||||||
String[][] data = loadMockfile("users");
|
String[][] data = loadMockfile("users");
|
||||||
String[] result = new String[n];
|
String[] result = new String[n];
|
||||||
@@ -86,16 +120,36 @@ public final class Faker {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Erzeugt ein Platzhalterbild in der angegebenen Größe.
|
||||||
|
* <p>
|
||||||
|
* Das Bild ist ein aus dem Internet geladenes, zufälliges Motiv, dass unter
|
||||||
|
* einer freien Lizenz (Creative Commons) steht.
|
||||||
|
*
|
||||||
|
* @param width Breite des Bildes.
|
||||||
|
* @param height Höhe des Bildes.
|
||||||
|
* @return Ein zufälliges Bild in der angegebenen Größe.
|
||||||
|
*/
|
||||||
public static BufferedImage fakeImage( int width, int height ) {
|
public static BufferedImage fakeImage( int width, int height ) {
|
||||||
return fakeImage(width, height, true);
|
return fakeImage(width, height, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Erzeugt ein Platzhalterbild in der angegebenen Größe.
|
||||||
|
*
|
||||||
|
* @param width Breite des Bildes.
|
||||||
|
* @param height Höhe des Bildes.
|
||||||
|
* @param fromWeb Bei {@code true} wird das Bild aus dem Internet geladen,
|
||||||
|
* bei {@code false} wird das Bild lokal erzeugt.
|
||||||
|
* @return Ein zufälliges Bild in der angegebenen Größe.
|
||||||
|
*/
|
||||||
public static BufferedImage fakeImage( int width, int height, boolean fromWeb ) {
|
public static BufferedImage fakeImage( int width, int height, boolean fromWeb ) {
|
||||||
if( !fromWeb ) {
|
if( !fromWeb ) {
|
||||||
BufferedImage img = ImageLoader.createImage(width, height);
|
BufferedImage img = ImageLoader.createImage(width, height);
|
||||||
Graphics2D graphics = (Graphics2D)img.getGraphics().create();
|
Graphics2D graphics = (Graphics2D) img.getGraphics().create();
|
||||||
|
|
||||||
String text = width+" x "+height;
|
String text = width + " x " + height;
|
||||||
|
|
||||||
Color clr = Constants.randomNiceColor();
|
Color clr = Constants.randomNiceColor();
|
||||||
|
|
||||||
@@ -103,12 +157,12 @@ public final class Faker {
|
|||||||
graphics.clearRect(0, 0, width, height);
|
graphics.clearRect(0, 0, width, height);
|
||||||
|
|
||||||
graphics.setColor(clr.textcolor().getJavaColor());
|
graphics.setColor(clr.textcolor().getJavaColor());
|
||||||
graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, (int)((width+height)*0.05)));
|
graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, (int) ((width + height) * 0.05)));
|
||||||
FontMetrics fontMerics = graphics.getFontMetrics();
|
FontMetrics fontMerics = graphics.getFontMetrics();
|
||||||
LineMetrics lineMetrics = fontMerics.getLineMetrics(text, graphics);
|
LineMetrics lineMetrics = fontMerics.getLineMetrics(text, graphics);
|
||||||
graphics.drawString(text,
|
graphics.drawString(text,
|
||||||
(int)((width - fontMerics.stringWidth(text))/2),
|
(int) ((width - fontMerics.stringWidth(text)) / 2),
|
||||||
(int)(height/2 - lineMetrics.getDescent() + lineMetrics.getAscent()/2)
|
(int) (height / 2 - lineMetrics.getDescent() + lineMetrics.getAscent() / 2)
|
||||||
);
|
);
|
||||||
graphics.dispose();
|
graphics.dispose();
|
||||||
|
|
||||||
@@ -123,7 +177,10 @@ public final class Faker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static String[][] loadMockfile( String name ) {
|
private static String[][] loadMockfile( String name ) {
|
||||||
return FileLoader.loadCsv("schule/ngb/zm/util/mock-"+name+".csv", true);
|
return FileLoader.loadCsv("schule/ngb/zm/util/mock-" + name + ".csv", true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Faker() {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user