diff --git a/lab/lab11/Question1/MyFrame.java b/lab/lab11/Question1/MyFrame.java new file mode 100644 index 0000000..b42710a --- /dev/null +++ b/lab/lab11/Question1/MyFrame.java @@ -0,0 +1,33 @@ +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; + +import java.awt.BorderLayout; +import java.awt.FlowLayout; + +public class MyFrame extends JFrame { + public MyFrame() { + this.setTitle("MyFrame Title"); + this.setSize(400, 300); + // Set the operation of the window when you click to + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + // close + this.setLocationRelativeTo(null); + BorderLayout fl = new BorderLayout(); + // Set the layout manager for the frame. + this.setLayout(fl); + // The first panel + JPanel jp = new JPanel(); + this.add(jp, BorderLayout.PAGE_START); + // The second panel + MyPanel p = new MyPanel(); + p.setLayout(new FlowLayout(FlowLayout.CENTER)); + JButton b1 = new JButton("left"); + p.add(b1); + JButton b2 = new JButton("right"); + p.add(b2); + this.add(p, BorderLayout.CENTER); + // Perform drawing and display to the desktop + this.setVisible(true); + } +} diff --git a/lab/lab11/Question1/MyPanel.java b/lab/lab11/Question1/MyPanel.java new file mode 100644 index 0000000..7303f63 --- /dev/null +++ b/lab/lab11/Question1/MyPanel.java @@ -0,0 +1,20 @@ +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +import javax.swing.JPanel; + +public class MyPanel extends JPanel { + public MyPanel() { + // Anonymous class. + this.addMouseListener(new MouseAdapter() { + public void mouseClicked(MouseEvent e) { + // Check whether the left mouse button is + if (e.getButton() == MouseEvent.BUTTON1) { + // get the coordinate + System.out.println("(" + e.getX() + "," + e.getY() + ")"); + } + } + }); + + } +} diff --git a/lab/lab11/Question1/Start.java b/lab/lab11/Question1/Start.java new file mode 100644 index 0000000..8a22936 --- /dev/null +++ b/lab/lab11/Question1/Start.java @@ -0,0 +1,11 @@ +public class Start { + public static void main(String[] args) { + javax.swing.SwingUtilities.invokeLater(new Runnable() { + // implements the Runnable interface with a run method + @Override + public void run() { + new MyFrame(); + } + }); + } +} diff --git a/lab/lab11/Question2/MyFrame.java b/lab/lab11/Question2/MyFrame.java new file mode 100644 index 0000000..c05fae9 --- /dev/null +++ b/lab/lab11/Question2/MyFrame.java @@ -0,0 +1,32 @@ +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; + +import java.awt.BorderLayout; +import java.awt.FlowLayout; + +public class MyFrame extends JFrame { + public MyFrame() { + this.setTitle("MyFrame Title"); + this.setSize(400, 300); + // Set the operation of the window when you click to close + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setLocationRelativeTo(null); + BorderLayout fl = new BorderLayout(); + // Set the layout manager for the frame. + this.setLayout(fl); + // The first panel + JPanel jp = new JPanel(); + this.add(jp, BorderLayout.PAGE_START); + // The second panel + MyPanel p = new MyPanel(); + p.setLayout(new FlowLayout(FlowLayout.CENTER)); + JButton b1 = new JButton("left"); + p.add(b1); + JButton b2 = new JButton("right"); + p.add(b2); + this.add(p, BorderLayout.CENTER); + // Perform drawing and display to the desktop + this.setVisible(true); + } +} diff --git a/lab/lab11/Question2/MyPanel.java b/lab/lab11/Question2/MyPanel.java new file mode 100644 index 0000000..cd7250d --- /dev/null +++ b/lab/lab11/Question2/MyPanel.java @@ -0,0 +1,38 @@ +import java.awt.Color; +import java.awt.Graphics; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +import javax.swing.JPanel; + +public class MyPanel extends JPanel { + private int x = -1; + private int y = -1; + + public MyPanel() { + this.addMouseListener(new MouseAdapter() { + // Anonymous class. + public void mouseClicked(MouseEvent e) { + // Check whether the left mouse button is clicked + if (e.getButton() == MouseEvent.BUTTON1) { + x = e.getX(); + y = e.getY(); + // get the coordinate + System.out.println("(" + e.getX() + "," + e.getY() + ")"); + // force Swing to repaint the red point + repaint(); + } + + } + }); + } + + @Override + protected void paintComponent(Graphics g) { + // must clean the panel before drawing on it. + super.paintComponent(g); + g.setColor(Color.RED); + // draw the red square + g.drawRect(x, y, 1, 1); + } +} diff --git a/lab/lab11/Question2/Start.java b/lab/lab11/Question2/Start.java new file mode 100644 index 0000000..8a22936 --- /dev/null +++ b/lab/lab11/Question2/Start.java @@ -0,0 +1,11 @@ +public class Start { + public static void main(String[] args) { + javax.swing.SwingUtilities.invokeLater(new Runnable() { + // implements the Runnable interface with a run method + @Override + public void run() { + new MyFrame(); + } + }); + } +} diff --git a/lab/lab11/Question3/MyFrame.java b/lab/lab11/Question3/MyFrame.java new file mode 100644 index 0000000..c05fae9 --- /dev/null +++ b/lab/lab11/Question3/MyFrame.java @@ -0,0 +1,32 @@ +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; + +import java.awt.BorderLayout; +import java.awt.FlowLayout; + +public class MyFrame extends JFrame { + public MyFrame() { + this.setTitle("MyFrame Title"); + this.setSize(400, 300); + // Set the operation of the window when you click to close + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setLocationRelativeTo(null); + BorderLayout fl = new BorderLayout(); + // Set the layout manager for the frame. + this.setLayout(fl); + // The first panel + JPanel jp = new JPanel(); + this.add(jp, BorderLayout.PAGE_START); + // The second panel + MyPanel p = new MyPanel(); + p.setLayout(new FlowLayout(FlowLayout.CENTER)); + JButton b1 = new JButton("left"); + p.add(b1); + JButton b2 = new JButton("right"); + p.add(b2); + this.add(p, BorderLayout.CENTER); + // Perform drawing and display to the desktop + this.setVisible(true); + } +} diff --git a/lab/lab11/Question3/MyPanel.java b/lab/lab11/Question3/MyPanel.java new file mode 100644 index 0000000..8f8a20f --- /dev/null +++ b/lab/lab11/Question3/MyPanel.java @@ -0,0 +1,40 @@ +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Point; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.ArrayList; + +import javax.swing.JPanel; + +public class MyPanel extends JPanel { + private ArrayList point; + + public MyPanel() { + point = new ArrayList(); + // Anonymous class. + this.addMouseListener(new MouseAdapter() { + public void mouseClicked(MouseEvent e) { + // Check whether the left mouse button is clicked + if (e.getButton() == MouseEvent.BUTTON1) { + // store the point which been clicked + point.add(e.getPoint()); + // get the coordinate + System.out.println("(" + e.getX() + "," + e.getY() + ")"); + } + + } + }); + } + + @Override + protected void paintComponent(Graphics g) { + // must clean the panel before drawing on it. + super.paintComponent(g); + g.setColor(Color.RED); + for (int i = 0; i < point.size(); i++) { + g.drawRect((int) point.get(i).getX(), (int) point.get(i).getY(), 1, 1); + } // draw the red square + repaint(); + } +} diff --git a/lab/lab11/Question3/Start.java b/lab/lab11/Question3/Start.java new file mode 100644 index 0000000..8a22936 --- /dev/null +++ b/lab/lab11/Question3/Start.java @@ -0,0 +1,11 @@ +public class Start { + public static void main(String[] args) { + javax.swing.SwingUtilities.invokeLater(new Runnable() { + // implements the Runnable interface with a run method + @Override + public void run() { + new MyFrame(); + } + }); + } +} diff --git a/lab/lab11/Question4/MyFrame.java b/lab/lab11/Question4/MyFrame.java new file mode 100644 index 0000000..3b801a3 --- /dev/null +++ b/lab/lab11/Question4/MyFrame.java @@ -0,0 +1,33 @@ +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; + +import java.awt.BorderLayout; +import java.awt.FlowLayout; + +public class MyFrame extends JFrame { + public MyFrame() { + this.setTitle("MyFrame Title"); + this.setSize(400, 300); + // Set the operation of the window when you click to close + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setLocationRelativeTo(null); + BorderLayout fl = new BorderLayout(); + // Set the layout manager for the frame. + this.setLayout(fl); + // The first panel + JPanel jp = new JPanel(); + this.add(jp, BorderLayout.PAGE_START); + // The second panel + MyPanel p = new MyPanel(); + this.add(p, BorderLayout.PAGE_START); + p.setLayout(new FlowLayout(FlowLayout.CENTER)); + JButton b1 = new JButton("left"); + p.add(b1); + JButton b2 = new JButton("right"); + p.add(b2); + this.add(p, BorderLayout.CENTER); + // Perform drawing and display to the desktop + this.setVisible(true); + } +} diff --git a/lab/lab11/Question4/MyPanel.java b/lab/lab11/Question4/MyPanel.java new file mode 100644 index 0000000..863a46c --- /dev/null +++ b/lab/lab11/Question4/MyPanel.java @@ -0,0 +1,46 @@ +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Point; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.ArrayList; + +import javax.swing.JPanel; + +public class MyPanel extends JPanel { + private ArrayList point; + + public MyPanel() { + point = new ArrayList(); + // Anonymous class. + this.addMouseListener(new MouseAdapter() { + public void mouseClicked(MouseEvent e) { + // Check whether the left mouse button is clicked + if (e.getButton() == MouseEvent.BUTTON1) { + // store the point which been clicked + point.add(e.getPoint()); + // get the coordinate + System.out.println("(" + e.getX() + "," + e.getY() + ")"); + } + + } + }); + } + + @Override + protected void paintComponent(Graphics g) { + // must clean the panel before drawing on it. + super.paintComponent(g); + g.setColor(Color.RED); + for (int i = 0; i < point.size(); i++) { + g.drawRect((int) point.get(i).getX(), (int) point.get(i).getY(), 1, 1); + if (i > 0) { + // connect next point + g.drawLine((int) point.get(i).getX(), (int) point.get(i).getY(), + (int) point.get(i - 1).getX(), (int) point.get(i - 1).getY()); + } + } + // draw the red square + repaint(); + } +} diff --git a/lab/lab11/Question4/Start.java b/lab/lab11/Question4/Start.java new file mode 100644 index 0000000..8a22936 --- /dev/null +++ b/lab/lab11/Question4/Start.java @@ -0,0 +1,11 @@ +public class Start { + public static void main(String[] args) { + javax.swing.SwingUtilities.invokeLater(new Runnable() { + // implements the Runnable interface with a run method + @Override + public void run() { + new MyFrame(); + } + }); + } +} diff --git a/lab/lab11/Question5/MyFrame.java b/lab/lab11/Question5/MyFrame.java new file mode 100644 index 0000000..c24fba8 --- /dev/null +++ b/lab/lab11/Question5/MyFrame.java @@ -0,0 +1,41 @@ +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; + +import java.awt.BorderLayout; +import java.awt.FlowLayout; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +public class MyFrame extends JFrame { + public MyFrame() { + this.setTitle("MyFrame Title"); + this.setSize(400, 300); + // Set the operation of the window when you click to close + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setLocationRelativeTo(null); + BorderLayout fl = new BorderLayout(); + // Set the layout manager for the frame. + this.setLayout(fl); + // The first panel + JPanel jp = new JPanel(); + this.add(jp, BorderLayout.PAGE_START); + // The second panel + MyPanel p = new MyPanel(); + p.setLayout(new FlowLayout(FlowLayout.CENTER)); + JButton b1 = new JButton("Reset"); + p.add(b1); + JButton b2 = new JButton("right"); + p.add(b2); + b1.addMouseListener(new MouseAdapter() { + @Override + // use the button1 to reset + public void mouseClicked(MouseEvent e) { + p.cleanAllPoints(); + } + }); + this.add(p, BorderLayout.CENTER); + // Perform drawing and display to the desktop + this.setVisible(true); + } +} diff --git a/lab/lab11/Question5/MyPanel.java b/lab/lab11/Question5/MyPanel.java new file mode 100644 index 0000000..b3f7735 --- /dev/null +++ b/lab/lab11/Question5/MyPanel.java @@ -0,0 +1,51 @@ +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Point; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.ArrayList; +import javax.swing.JPanel; + +public class MyPanel extends JPanel { + private ArrayList point; + + public MyPanel() { + point = new ArrayList(); + // Anonymous class. + this.addMouseListener(new MouseAdapter() { + public void mouseClicked(MouseEvent e) { + // Check whether the left mouse button is clicked + if (e.getButton() == MouseEvent.BUTTON1) { + // store the point which been clicked + point.add(e.getPoint()); + // get the coordinate + System.out.println("(" + e.getX() + "," + e.getY() + ")"); + + } + + } + }); + } + + @Override + protected void paintComponent(Graphics g) { + // must clean the panel before drawing on it. + super.paintComponent(g); + g.setColor(Color.RED); + for (int i = 0; i < point.size(); i++) { + g.drawRect((int) point.get(i).getX(), (int) point.get(i).getY(), 1, 1); + if (i > 0) { + // connect next point + g.drawLine((int) point.get(i).getX(), (int) point.get(i).getY(), + (int) point.get(i - 1).getX(), (int) point.get(i - 1).getY()); + } + } + // draw the red square + repaint(); + } + + public void cleanAllPoints() { // uses the clear method of the arraylist to remove all the Point objects + point.clear(); + repaint(); + } +} diff --git a/lab/lab11/Question5/Start.java b/lab/lab11/Question5/Start.java new file mode 100644 index 0000000..8a22936 --- /dev/null +++ b/lab/lab11/Question5/Start.java @@ -0,0 +1,11 @@ +public class Start { + public static void main(String[] args) { + javax.swing.SwingUtilities.invokeLater(new Runnable() { + // implements the Runnable interface with a run method + @Override + public void run() { + new MyFrame(); + } + }); + } +}