From d4e4a76e9418a2e68276e581c9a2ccd3fa10dd6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Kvarnstr=C3=B6m?= Date: Thu, 14 Jan 2021 17:37:20 +0100 Subject: Moved example code to a "se.liu.tddd78.examples" folder. --- src/se/liu/liuid123/.gitkeep | 1 + src/se/liu/liuid123/HelloWorld.java | 11 ----- src/se/liu/liuid123/ResourceTester.java | 62 -------------------------- src/se/liu/tddd78/examples/HelloWorld.java | 11 +++++ src/se/liu/tddd78/examples/ResourceTester.java | 62 ++++++++++++++++++++++++++ 5 files changed, 74 insertions(+), 73 deletions(-) create mode 100644 src/se/liu/liuid123/.gitkeep delete mode 100644 src/se/liu/liuid123/HelloWorld.java delete mode 100644 src/se/liu/liuid123/ResourceTester.java create mode 100644 src/se/liu/tddd78/examples/HelloWorld.java create mode 100644 src/se/liu/tddd78/examples/ResourceTester.java (limited to 'src') diff --git a/src/se/liu/liuid123/.gitkeep b/src/se/liu/liuid123/.gitkeep new file mode 100644 index 0000000..f2a87fc --- /dev/null +++ b/src/se/liu/liuid123/.gitkeep @@ -0,0 +1 @@ +This file makes sure the folder does not disappear from Git if it becomes empty. \ No newline at end of file diff --git a/src/se/liu/liuid123/HelloWorld.java b/src/se/liu/liuid123/HelloWorld.java deleted file mode 100644 index 36e2290..0000000 --- a/src/se/liu/liuid123/HelloWorld.java +++ /dev/null @@ -1,11 +0,0 @@ -package se.liu.liuid123; - -/** - * A simple test class used to verify that your development environment is working. - */ -public class HelloWorld -{ - public static void main(String[] args) { - System.out.println("Hello world!"); - } -} diff --git a/src/se/liu/liuid123/ResourceTester.java b/src/se/liu/liuid123/ResourceTester.java deleted file mode 100644 index eff4fdd..0000000 --- a/src/se/liu/liuid123/ResourceTester.java +++ /dev/null @@ -1,62 +0,0 @@ -package se.liu.liuid123; - -import javax.swing.*; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.URL; - -/** - * A simple test class used to exemplify how resources work. - */ -public class ResourceTester -{ - public static void main(String[] args) { - testResourceText(); - testResourceImage(); - } - - private static void testResourceText() { - // Reading a text file from *resources* is a bit cumbersome. - // We don't know if the file is stored directly in the file system - // or inside an *archive* (JAR file), so should access it - // as a "resource" identified by an URL. - final URL readme = ClassLoader.getSystemResource("README.md"); - - // Then we can use this URL to open an *input stream*, - // create an InputStreamReader that converts bytes to characters - // according to the default character encoding (typically UTF-8), - // and then createa a BufferedReader which can be used to read lines. - // - // All of this is insiude a "try" statement that ensures the streams - // and readers are closed when we are done. - try (final BufferedReader reader = new BufferedReader(new InputStreamReader(readme.openStream()))) { - System.out.println("Contents of the file:"); - - // Read and print strings until you get null, which indicates "end of file" - // for Reader objects - String str = reader.readLine(); - while (str != null) { - System.out.println(str); - str = reader.readLine(); - } - } catch (IOException e) { - // TODO: Needs to be handled somehow. This code is incomplete and should be - // extended by course participants. - e.printStackTrace(); - } - } - - - private static void testResourceImage() { - // Like above, we need to access the image through a resource. - final URL image = ClassLoader.getSystemResource("images/hello_world.png"); - - // The ImageIcon class can read an entire image directly from any URL. - ImageIcon icon = new ImageIcon(image); - - // We won't go all the way to showing the image here, but we can print - // some information about it! - System.out.println("Read an image with width " + icon.getIconWidth() + " and height " + icon.getIconHeight()); - } -} diff --git a/src/se/liu/tddd78/examples/HelloWorld.java b/src/se/liu/tddd78/examples/HelloWorld.java new file mode 100644 index 0000000..0b865ce --- /dev/null +++ b/src/se/liu/tddd78/examples/HelloWorld.java @@ -0,0 +1,11 @@ +package se.liu.tddd78.examples; + +/** + * A simple test class used to verify that your development environment is working. + */ +public class HelloWorld +{ + public static void main(String[] args) { + System.out.println("Hello world!"); + } +} diff --git a/src/se/liu/tddd78/examples/ResourceTester.java b/src/se/liu/tddd78/examples/ResourceTester.java new file mode 100644 index 0000000..81791d4 --- /dev/null +++ b/src/se/liu/tddd78/examples/ResourceTester.java @@ -0,0 +1,62 @@ +package se.liu.tddd78.examples; + +import javax.swing.*; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; + +/** + * A simple test class used to exemplify how resources work. + */ +public class ResourceTester +{ + public static void main(String[] args) { + testResourceText(); + testResourceImage(); + } + + private static void testResourceText() { + // Reading a text file from *resources* is a bit cumbersome. + // We don't know if the file is stored directly in the file system + // or inside an *archive* (JAR file), so should access it + // as a "resource" identified by an URL. + final URL readme = ClassLoader.getSystemResource("README.md"); + + // Then we can use this URL to open an *input stream*, + // create an InputStreamReader that converts bytes to characters + // according to the default character encoding (typically UTF-8), + // and then createa a BufferedReader which can be used to read lines. + // + // All of this is insiude a "try" statement that ensures the streams + // and readers are closed when we are done. + try (final BufferedReader reader = new BufferedReader(new InputStreamReader(readme.openStream()))) { + System.out.println("Contents of the file:"); + + // Read and print strings until you get null, which indicates "end of file" + // for Reader objects + String str = reader.readLine(); + while (str != null) { + System.out.println(str); + str = reader.readLine(); + } + } catch (IOException e) { + // TODO: Needs to be handled somehow. This code is incomplete and should be + // extended by course participants. + e.printStackTrace(); + } + } + + + private static void testResourceImage() { + // Like above, we need to access the image through a resource. + final URL image = ClassLoader.getSystemResource("images/hello_world.png"); + + // The ImageIcon class can read an entire image directly from any URL. + ImageIcon icon = new ImageIcon(image); + + // We won't go all the way to showing the image here, but we can print + // some information about it! + System.out.println("Read an image with width " + icon.getIconWidth() + " and height " + icon.getIconHeight()); + } +} -- cgit v1.2.1