From 3afb7abd244ac5103c3f3dcb2a65563eb6d8a69b Mon Sep 17 00:00:00 2001 From: Keiko Katsuragawa <kkatsuragawa@uwaterloo.ca> Date: Wed, 17 Jan 2018 00:09:50 -0500 Subject: [PATCH] add 2-2-JavaGUI examples --- java/2-2-JavaGUI/BasicForm1.java | 26 ++++++++++++ java/2-2-JavaGUI/BasicForm2.java | 56 ++++++++++++++++++++++++++ java/2-2-JavaGUI/BasicForm3.java | 40 ++++++++++++++++++ java/2-2-JavaGUI/BasicForm4.java | 44 ++++++++++++++++++++ java/2-2-JavaGUI/SimpleDraw.java | 40 ++++++++++++++++++ java/2-2-JavaGUI/SimpleDraw2.java | 61 ++++++++++++++++++++++++++++ java/2-2-JavaGUI/SimpleDraw3.java | 67 +++++++++++++++++++++++++++++++ java/2-2-JavaGUI/SimpleDraw4.java | 55 +++++++++++++++++++++++++ java/2-2-JavaGUI/makefile | 15 +++++++ 9 files changed, 404 insertions(+) create mode 100644 java/2-2-JavaGUI/BasicForm1.java create mode 100644 java/2-2-JavaGUI/BasicForm2.java create mode 100644 java/2-2-JavaGUI/BasicForm3.java create mode 100644 java/2-2-JavaGUI/BasicForm4.java create mode 100644 java/2-2-JavaGUI/SimpleDraw.java create mode 100644 java/2-2-JavaGUI/SimpleDraw2.java create mode 100644 java/2-2-JavaGUI/SimpleDraw3.java create mode 100644 java/2-2-JavaGUI/SimpleDraw4.java create mode 100644 java/2-2-JavaGUI/makefile diff --git a/java/2-2-JavaGUI/BasicForm1.java b/java/2-2-JavaGUI/BasicForm1.java new file mode 100644 index 0000000..2d50de8 --- /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 0000000..d5dc90b --- /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 0000000..d036964 --- /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 0000000..84d5fa5 --- /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 0000000..5ed4579 --- /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 0000000..cffb3ec --- /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 0000000..0e8141c --- /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 0000000..0bd081d --- /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 0000000..0938cfc --- /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 -- GitLab