summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonas Kvarnström <jonas.kvarnstrom@liu.se>2020-12-19 11:35:40 +0100
committerJonas Kvarnström <jonas.kvarnstrom@liu.se>2020-12-19 11:35:40 +0100
commit16d8bf228004f71aa9435e0e74b52b87bae0c0ca (patch)
tree5d2ce97817f5ef1424d209402ace73b2a412de7a
parentdb09ef0f42330e2c469283cfb3b7783da13c3b2c (diff)
downloadtdde30-16d8bf228004f71aa9435e0e74b52b87bae0c0ca.tar.gz
Resource folder with example text file and image. ResourceTester class.
-rw-r--r--Javalabbar.iml1
-rw-r--r--resources/README.md2
-rw-r--r--resources/images/hello_world.pngbin0 -> 4224 bytes
-rw-r--r--src/se/liu/liuid123/HelloWorld.java (renamed from src/se/liu/liuid123/Main.java)4
-rw-r--r--src/se/liu/liuid123/ResourceTester.java47
5 files changed, 52 insertions, 2 deletions
diff --git a/Javalabbar.iml b/Javalabbar.iml
index c90834f..54b8c26 100644
--- a/Javalabbar.iml
+++ b/Javalabbar.iml
@@ -3,6 +3,7 @@
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
+ <sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
diff --git a/resources/README.md b/resources/README.md
new file mode 100644
index 0000000..d3dd293
--- /dev/null
+++ b/resources/README.md
@@ -0,0 +1,2 @@
+I den här katalogen kan man lägga *resursfiler* -- bilder, ljudfiler och andra filer som programmet ska kunna komma åt när det körs. Du kan
+se exempel på detta i klassen `Test`. \ No newline at end of file
diff --git a/resources/images/hello_world.png b/resources/images/hello_world.png
new file mode 100644
index 0000000..b142f6f
--- /dev/null
+++ b/resources/images/hello_world.png
Binary files differ
diff --git a/src/se/liu/liuid123/Main.java b/src/se/liu/liuid123/HelloWorld.java
index 9ad934c..36e2290 100644
--- a/src/se/liu/liuid123/Main.java
+++ b/src/se/liu/liuid123/HelloWorld.java
@@ -3,8 +3,8 @@ package se.liu.liuid123;
/**
* A simple test class used to verify that your development environment is working.
*/
-public class Main {
-
+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
new file mode 100644
index 0000000..89bd438
--- /dev/null
+++ b/src/se/liu/liuid123/ResourceTester.java
@@ -0,0 +1,47 @@
+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) {
+ // 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
+ String str = reader.readLine();
+ while (str != null) {
+ System.out.println(str);
+ str = reader.readLine();
+ }
+ } catch (IOException e) {
+ // Needs to be handled somehow...
+ e.printStackTrace();
+ }
+
+ final URL image = ClassLoader.getSystemResource("images/hello_world.png");
+
+ ImageIcon icon = new ImageIcon(image);
+ System.out.println("Read an image with width " + icon.getIconWidth() + " and height " + icon.getIconHeight());
+ }
+}