JAVA-1749 Move modules language interop and console
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.asciiart;
|
||||
|
||||
import java.awt.Font;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class AsciiArt {
|
||||
|
||||
public AsciiArt() {
|
||||
}
|
||||
|
||||
public void drawString(String text, String artChar, Settings settings) {
|
||||
BufferedImage image = getImageIntegerMode(settings.width, settings.height);
|
||||
|
||||
Graphics2D graphics2D = getGraphics2D(image.getGraphics(), settings);
|
||||
graphics2D.drawString(text, 6, ((int) (settings.height * 0.67)));
|
||||
|
||||
for (int y = 0; y < settings.height; y++) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
for (int x = 0; x < settings.width; x++) {
|
||||
stringBuilder.append(image.getRGB(x, y) == -16777216 ? " " : artChar);
|
||||
}
|
||||
|
||||
if (stringBuilder.toString()
|
||||
.trim()
|
||||
.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
System.out.println(stringBuilder);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private BufferedImage getImageIntegerMode(int width, int height) {
|
||||
return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
}
|
||||
|
||||
private Graphics2D getGraphics2D(Graphics graphics, Settings settings) {
|
||||
graphics.setFont(settings.font);
|
||||
|
||||
Graphics2D graphics2D = (Graphics2D) graphics;
|
||||
graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||
|
||||
return graphics2D;
|
||||
}
|
||||
|
||||
public class Settings {
|
||||
public Font font;
|
||||
public int width;
|
||||
public int height;
|
||||
|
||||
public Settings(Font font, int width, int height) {
|
||||
this.font = font;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.console;
|
||||
|
||||
import java.io.Console;
|
||||
|
||||
public class ConsoleConsoleClass {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Console console = System.console();
|
||||
|
||||
if (console == null) {
|
||||
System.out.print("No console available");
|
||||
return;
|
||||
}
|
||||
|
||||
String progLanguauge = console.readLine("Enter your favourite programming language: ");
|
||||
console.printf(progLanguauge + " is very interesting!");
|
||||
|
||||
char[] pass = console.readPassword("To finish, enter password: ");
|
||||
|
||||
if ("BAELDUNG".equals(pass.toString().toUpperCase()))
|
||||
console.printf("Good! Regards!");
|
||||
else
|
||||
console.printf("Nice try. Regards.");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.baeldung.console;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Scanner;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ConsoleScannerClass {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Please enter your name and surname: ");
|
||||
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
String nameSurname = scanner.nextLine();
|
||||
|
||||
System.out.println("Please enter your gender: ");
|
||||
|
||||
char gender = scanner.next().charAt(0);
|
||||
|
||||
System.out.println("Please enter your age: ");
|
||||
|
||||
int age = scanner.nextInt();
|
||||
|
||||
System.out.println("Please enter your height in meters: ");
|
||||
|
||||
double height = scanner.nextDouble();
|
||||
|
||||
System.out.println(nameSurname + ", " + age + ", is a great " + (gender == 'm' ? "guy" : "girl") + " with " + height + " meters height" + " and " + (gender == 'm' ? "he" : "she") + " reads Baeldung.");
|
||||
|
||||
System.out.print("Have a good");
|
||||
System.out.print(" one!");
|
||||
|
||||
System.out.println("\nPlease enter number of years of experience as a developer: ");
|
||||
|
||||
BufferedReader buffReader = new BufferedReader(new InputStreamReader(System.in));
|
||||
|
||||
int i = 0;
|
||||
|
||||
try {
|
||||
i = Integer.parseInt(buffReader.readLine());
|
||||
} catch (NumberFormatException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
System.out.println("You are a " + (i > 5 ? "great" : "good") + " developer!");
|
||||
|
||||
int sum = 0, count = 0;
|
||||
|
||||
System.out.println("Please enter your college degrees. To finish, enter baeldung website url");
|
||||
|
||||
while (scanner.hasNextInt()) {
|
||||
int nmbr = scanner.nextInt();
|
||||
sum += nmbr;
|
||||
count++;
|
||||
}
|
||||
int mean = sum / count;
|
||||
|
||||
System.out.println("Your average degree is " + mean);
|
||||
|
||||
if (scanner.hasNext(Pattern.compile("www.baeldung.com")))
|
||||
System.out.println("Correct!");
|
||||
else
|
||||
System.out.println("Baeldung website url is www.baeldung.com");
|
||||
|
||||
if (scanner != null)
|
||||
scanner.close();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.baeldung.printf;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
public class PrintfExamples {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
printfNewLine();
|
||||
printfChar();
|
||||
printfString();
|
||||
printfNumber();
|
||||
printfDateTime();
|
||||
printfBoolean();
|
||||
}
|
||||
|
||||
private static void printfNewLine() {
|
||||
System.out.printf("baeldung%nline%nterminator");
|
||||
}
|
||||
|
||||
private static void printfString() {
|
||||
System.out.printf("'%s' %n", "baeldung");
|
||||
System.out.printf("'%S' %n", "baeldung");
|
||||
System.out.printf("'%15s' %n", "baeldung");
|
||||
System.out.printf("'%-10s' %n", "baeldung");
|
||||
}
|
||||
|
||||
private static void printfChar() {
|
||||
System.out.printf("%c%n", 's');
|
||||
System.out.printf("%C%n", 's');
|
||||
}
|
||||
|
||||
private static void printfNumber() {
|
||||
System.out.printf("simple integer: %d%n", 10000L);
|
||||
|
||||
System.out.printf(Locale.US, "%,d %n", 10000);
|
||||
System.out.printf(Locale.ITALY, "%,d %n", 10000);
|
||||
|
||||
System.out.printf("%f%n", 5.1473);
|
||||
System.out.printf("'%5.2f'%n", 5.1473);
|
||||
System.out.printf("'%5.2e'%n", 5.1473);
|
||||
}
|
||||
|
||||
private static void printfBoolean() {
|
||||
System.out.printf("%b%n", null);
|
||||
System.out.printf("%B%n", false);
|
||||
System.out.printf("%B%n", 5.3);
|
||||
System.out.printf("%b%n", "random text");
|
||||
}
|
||||
|
||||
private static void printfDateTime() {
|
||||
Date date = new Date();
|
||||
System.out.printf("%tT%n", date);
|
||||
System.out.printf("hours %tH: minutes %tM: seconds %tS%n", date, date, date);
|
||||
System.out.printf("%1$tH:%1$tM:%1$tS %1$tp %1$tL %1$tN %1$tz %n", date);
|
||||
|
||||
System.out.printf("%1$tA %1$tB %1$tY %n", date);
|
||||
System.out.printf("%1$td.%1$tm.%1$ty %n", date);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.asciiart;
|
||||
|
||||
import com.baeldung.asciiart.AsciiArt.Settings;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class AsciiArtIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenTextWithAsciiCharacterAndSettings_shouldPrintAsciiArt() {
|
||||
AsciiArt asciiArt = new AsciiArt();
|
||||
String text = "BAELDUNG";
|
||||
Settings settings = asciiArt.new Settings(new Font("SansSerif", Font.BOLD, 24), text.length() * 30, 30); // 30 pixel width per character
|
||||
|
||||
asciiArt.drawString(text, "*", settings);
|
||||
|
||||
throw new NullPointerException();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user