diff --git a/java/2-2-JavaGUI/BasicForm1.java b/java/2-2-JavaGUI/BasicForm1.java new file mode 100644 index 0000000000000000000000000000000000000000..2d50de84624b56824a1e3e3417862641c5900bfb --- /dev/null +++ b/java/2-2-JavaGUI/BasicForm1.java @@ -0,0 +1,26 @@ + +import javax.swing.*; + +// Create a simple form +public class BasicForm1 { + public static void main(String[] args) { + // create a window + JFrame frame = new JFrame("Window Demo"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // create a panel and add components + JPanel panel = new JPanel(); + JButton button = new JButton("Ok"); + panel.add(button); + + // add panel to the window + frame.add(panel); + + // set window behaviour and display it + frame.setResizable(false); + frame.setSize(200, 200); + // frame.pack(); + frame.setVisible(true); + } +} + diff --git a/java/2-2-JavaGUI/BasicForm2.java b/java/2-2-JavaGUI/BasicForm2.java new file mode 100644 index 0000000000000000000000000000000000000000..d5dc90bb7154978125f1bb52958784179e404455 --- /dev/null +++ b/java/2-2-JavaGUI/BasicForm2.java @@ -0,0 +1,56 @@ + +import javax.swing.*; +import javax.swing.event.MouseInputListener; +import java.awt.event.MouseEvent; + +// Create a simple form +// Adds a listener +public class BasicForm2 { + + public static void main(String[] args) { + // create a window + JFrame frame = new JFrame("Window Demo"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // create a panel and add components + JPanel panel = new JPanel(); + JButton button = new JButton("Ok"); + button.addMouseListener(new MyMouseListener()); + panel.add(button); + + // add panel to the window + frame.add(panel); + + // set window behaviour and display it + frame.setResizable(false); + frame.setSize(200, 200); + // frame.pack(); + frame.setVisible(true); + } + + // create a custom listener for my ok button + static class MyMouseListener implements MouseInputListener { + @Override + public void mouseClicked(MouseEvent e) { + System.exit(1); + } + + @Override + public void mousePressed(MouseEvent e) { } + + @Override + public void mouseReleased(MouseEvent e) { } + + @Override + public void mouseEntered(MouseEvent e) { } + + @Override + public void mouseExited(MouseEvent e) { } + + @Override + public void mouseDragged(MouseEvent e) { } + + @Override + public void mouseMoved(MouseEvent e) { } + } +} \ No newline at end of file diff --git a/java/2-2-JavaGUI/BasicForm3.java b/java/2-2-JavaGUI/BasicForm3.java new file mode 100644 index 0000000000000000000000000000000000000000..d0369640604be6cc764e011119fdf10d96479f0e --- /dev/null +++ b/java/2-2-JavaGUI/BasicForm3.java @@ -0,0 +1,40 @@ + +import javax.swing.*; +import javax.swing.event.MouseInputListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.MouseMotionListener; + +// Create a simple form +// Converts listener to adapter +public class BasicForm3 { + + public static void main(String[] args) { + // create a window + JFrame frame = new JFrame("Window Demo"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // create a panel and add components + JPanel panel = new JPanel(); + JButton button = new JButton("Ok"); + button.addMouseListener(new MyMouseAdapter()); + panel.add(button); + + // add panel to the window + frame.add(panel); + + // set window behaviour and display it + frame.setResizable(false); + frame.setSize(200, 200); + // frame.pack(); + frame.setVisible(true); + } + + // create a custom adapter from MouseAdapter base class + static class MyMouseAdapter extends MouseAdapter { + public void mouseClicked(MouseEvent e) { + System.exit(1); + } + } +} + diff --git a/java/2-2-JavaGUI/BasicForm4.java b/java/2-2-JavaGUI/BasicForm4.java new file mode 100644 index 0000000000000000000000000000000000000000..84d5fa5e43c33c0373b66ed0f83dc3186fdd2acf --- /dev/null +++ b/java/2-2-JavaGUI/BasicForm4.java @@ -0,0 +1,44 @@ + +import javax.swing.*; +import javax.swing.event.MouseInputListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.MouseMotionListener; + +// Create a simple form +// Converts adapter to anonymous inner class +public class BasicForm4 { + + public static void main(String[] args) { + // create a window + JFrame frame = new JFrame("Window Demo"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // create a panel and add components + JPanel panel = new JPanel(); + JButton button = new JButton("Ok"); + button.addMouseListener(new MouseAdapter() { + public void mouseClicked(MouseEvent e) { // use anonymous inner class instead of MyMouseAdapter class + System.exit(1); + }}); + panel.add(button); + + // add panel to the window + frame.add(panel); + + // set window behaviour and display it + frame.setResizable(false); + frame.setSize(200, 200); + // frame.pack(); + frame.setVisible(true); + } + +// We do not need to implement a custom adapter +/* // create a custom adapter from MouseAdapter base class + static class MyMouseAdapter extends MouseAdapter { + public void mouseClicked(MouseEvent e) { + System.exit(1); + } + }*/ +} + diff --git a/java/2-2-JavaGUI/SimpleDraw.java b/java/2-2-JavaGUI/SimpleDraw.java new file mode 100644 index 0000000000000000000000000000000000000000..5ed4579a7b8fb93d62b73115da7c8f67018b42b9 --- /dev/null +++ b/java/2-2-JavaGUI/SimpleDraw.java @@ -0,0 +1,40 @@ +import javax.swing.*; +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.BasicStroke; +import java.awt.RenderingHints; + + + +// JComponent is a base class for custom components +public class SimpleDraw extends JComponent { + + public static void main(String[] args) { + JFrame f = new JFrame("SimpleDraw"); // jframe is the app window + f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + f.setSize(400, 400); // window size + + SimpleDraw canvas = new SimpleDraw(); + f.setContentPane(canvas); // add canvas to jframe + f.setVisible(true); // show the window + } + + // Constructor for SimpleDraw + public SimpleDraw() { + } + + // custom graphics drawing + // this is a method that we overrode from the JComponent class + public void paintComponent(Graphics g) { + Graphics2D g2 = (Graphics2D) g; // 2D drawing + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + g2.setStroke(new BasicStroke(32)); // 32 pixels wide + g2.setColor(Color.BLUE); // make it blue + g2.drawLine(0, 0, getWidth(), getHeight()); // draw line + g2.setColor(Color.RED); + g2.drawLine(getWidth(), 0, 0, getHeight()); + } +} + diff --git a/java/2-2-JavaGUI/SimpleDraw2.java b/java/2-2-JavaGUI/SimpleDraw2.java new file mode 100644 index 0000000000000000000000000000000000000000..cffb3ecb34b4040ad1692f6369f3fcbdb336864b --- /dev/null +++ b/java/2-2-JavaGUI/SimpleDraw2.java @@ -0,0 +1,61 @@ +import javax.swing.*; +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.BasicStroke; +import java.awt.RenderingHints; +import java.awt.event.*; + + +// JComponent is a base class for custom components +public class SimpleDraw2 extends JComponent { + + public static void main(String[] args) { + JFrame f = new JFrame("SimpleDraw"); // jframe is the app window + f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + f.setSize(400, 400); // window size + + SimpleDraw2 canvas = new SimpleDraw2(); + f.setContentPane(canvas); // add canvas to jframe + f.setVisible(true); // show the window + } + + // Object properties + private int mouseX; + private int mouseY; + + // Constructor for SimpleDraw + public SimpleDraw2() { + this.addMouseMotionListener( new MyMotionListener() ); + } + + // custom graphics drawing + public void paintComponent(Graphics g) { + Graphics2D g2 = (Graphics2D) g; // cast to get 2D drawing methods + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // antialiasing look nicer + RenderingHints.VALUE_ANTIALIAS_ON); + g2.setStroke(new BasicStroke(32)); // 32 pixel thick stroke + g2.setColor(Color.BLUE); // make it blue + g2.drawLine(0, 0, getWidth(), getHeight()); // draw line + g2.setColor(Color.RED); + g2.drawLine(getWidth(), 0, 0, getHeight()); + + String label = "Mouse at (" + mouseX + ", " + mouseY + ")"; + g2.setColor(Color.BLACK); + g2.drawString(label, 130, 40); + } + + // Inner class for handling mouse motion events + private class MyMotionListener implements MouseMotionListener { + public void mouseMoved(MouseEvent e) { + mouseX = e.getX(); + mouseY = e.getY(); + repaint(); + } + + public void mouseDragged(MouseEvent e) { + // Do nothing. + // But we still need this method implemented because it's part of the interface + } + } +} diff --git a/java/2-2-JavaGUI/SimpleDraw3.java b/java/2-2-JavaGUI/SimpleDraw3.java new file mode 100644 index 0000000000000000000000000000000000000000..0e8141ca0ac6d004a78bee2834593162548619ce --- /dev/null +++ b/java/2-2-JavaGUI/SimpleDraw3.java @@ -0,0 +1,67 @@ +import javax.swing.*; +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.BasicStroke; +import java.awt.RenderingHints; +import java.awt.event.*; + + +// JComponent is a base class for custom components +public class SimpleDraw3 extends JComponent { + + public static void main(String[] args) { + JFrame f = new JFrame("SimpleDraw"); // jframe is the app window + f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + f.setSize(400, 400); // window size + + SimpleDraw3 canvas = new SimpleDraw3(); + f.setContentPane(canvas); // add canvas to jframe + f.setVisible(true); // show the window + } + + // Object properties + private int mouseX; + private int mouseY; + + // Constructor for SimpleDraw + public SimpleDraw3() { + this.addMouseMotionListener( new MyMotionListener() ); + } + + // custom graphics drawing + public void paintComponent(Graphics g) { + Graphics2D g2 = (Graphics2D) g; // cast to get 2D drawing methods + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // antialiasing look nicer + RenderingHints.VALUE_ANTIALIAS_ON); + g2.setStroke(new BasicStroke(32)); // 32 pixel thick stroke + g2.setColor(Color.BLUE); // make it blue + g2.drawLine(0, 0, getWidth(), getHeight()); // draw line + g2.setColor(Color.RED); + g2.drawLine(getWidth(), 0, 0, getHeight()); + + String label = "Mouse at (" + mouseX + ", " + mouseY + ")"; + g2.setColor(Color.BLACK); + g2.drawString(label, 130, 40); + } + + // Inner class for handling mouse motion events + private class MyMotionListener extends MouseMotionAdapter { + public void mouseMoved(MouseEvent e) { + mouseX = e.getX(); + mouseY = e.getY(); + repaint(); + } + + // Notice the difference? + // We implemented the MotionListener using an adapter instead of an interface + // This allows is to ignore methods we don't want! + + /* + public void mouseDragged(MouseEvent e) { + // Do nothing. + // But we still need this method implemented because it's part of the interface + } + */ + } +} diff --git a/java/2-2-JavaGUI/SimpleDraw4.java b/java/2-2-JavaGUI/SimpleDraw4.java new file mode 100644 index 0000000000000000000000000000000000000000..0bd081df2cbb1b4810fdbd20c30c503c384ef698 --- /dev/null +++ b/java/2-2-JavaGUI/SimpleDraw4.java @@ -0,0 +1,55 @@ +import javax.swing.*; +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.BasicStroke; +import java.awt.RenderingHints; +import java.awt.event.*; +import java.util.Vector; + +// JComponent is a base class for custom components +public class SimpleDraw4 extends JComponent { + + public static void main(String[] args) { + JFrame f = new JFrame("SimpleDraw"); // jframe is the app window + f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + f.setSize(400, 400); // window size + + SimpleDraw4 canvas = new SimpleDraw4(); + f.setContentPane(canvas); // add canvas to jframe + f.setVisible(true); // show the window + } + + // Object properties + private int mouseX; + private int mouseY; + + // Constructor for SimpleDraw + public SimpleDraw4() { + + // this is an example of an anonymous inner class + this.addMouseMotionListener( new MouseAdapter() { + public void mouseMoved(MouseEvent e) { + mouseX = e.getX(); + mouseY = e.getY(); + repaint(); + } + }); + } + + // custom graphics drawing + public void paintComponent(Graphics g) { + Graphics2D g2 = (Graphics2D) g; // cast to get 2D drawing methods + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // antialiasing look nicer + RenderingHints.VALUE_ANTIALIAS_ON); + g2.setStroke(new BasicStroke(32)); // 32 pixel thick stroke + g2.setColor(Color.BLUE); // make it blue + g2.drawLine(0, 0, getWidth(), getHeight()); // draw line + g2.setColor(Color.RED); + g2.drawLine(getWidth(), 0, 0, getHeight()); + + String label = "Mouse at (" + mouseX + ", " + mouseY + ")"; + g2.setColor(Color.BLACK); + g2.drawString(label, 130, 40); + } +} diff --git a/java/2-2-JavaGUI/makefile b/java/2-2-JavaGUI/makefile new file mode 100644 index 0000000000000000000000000000000000000000..0938cfc46625d902333214a6fb146f5cafd17802 --- /dev/null +++ b/java/2-2-JavaGUI/makefile @@ -0,0 +1,15 @@ +# super simple makefile +# call it using 'make NAME=name_of_code_file_without_extension' +# (assumes a .java extension) +NAME = "BasicForm1" + +all: + @echo "Compiling..." + javac *.java + +run: all + @echo "Running..." + java $(NAME) + +clean: + rm -rf $(NAME).class