add lab11
This commit is contained in:
33
lab/lab11/Question1/MyFrame.java
Normal file
33
lab/lab11/Question1/MyFrame.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
20
lab/lab11/Question1/MyPanel.java
Normal file
20
lab/lab11/Question1/MyPanel.java
Normal file
@@ -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() + ")");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
11
lab/lab11/Question1/Start.java
Normal file
11
lab/lab11/Question1/Start.java
Normal file
@@ -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();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
32
lab/lab11/Question2/MyFrame.java
Normal file
32
lab/lab11/Question2/MyFrame.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
38
lab/lab11/Question2/MyPanel.java
Normal file
38
lab/lab11/Question2/MyPanel.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
11
lab/lab11/Question2/Start.java
Normal file
11
lab/lab11/Question2/Start.java
Normal file
@@ -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();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
32
lab/lab11/Question3/MyFrame.java
Normal file
32
lab/lab11/Question3/MyFrame.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
40
lab/lab11/Question3/MyPanel.java
Normal file
40
lab/lab11/Question3/MyPanel.java
Normal file
@@ -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> point;
|
||||
|
||||
public MyPanel() {
|
||||
point = new ArrayList<Point>();
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
11
lab/lab11/Question3/Start.java
Normal file
11
lab/lab11/Question3/Start.java
Normal file
@@ -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();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
33
lab/lab11/Question4/MyFrame.java
Normal file
33
lab/lab11/Question4/MyFrame.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
46
lab/lab11/Question4/MyPanel.java
Normal file
46
lab/lab11/Question4/MyPanel.java
Normal file
@@ -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> point;
|
||||
|
||||
public MyPanel() {
|
||||
point = new ArrayList<Point>();
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
11
lab/lab11/Question4/Start.java
Normal file
11
lab/lab11/Question4/Start.java
Normal file
@@ -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();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
41
lab/lab11/Question5/MyFrame.java
Normal file
41
lab/lab11/Question5/MyFrame.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
51
lab/lab11/Question5/MyPanel.java
Normal file
51
lab/lab11/Question5/MyPanel.java
Normal file
@@ -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> point;
|
||||
|
||||
public MyPanel() {
|
||||
point = new ArrayList<Point>();
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
11
lab/lab11/Question5/Start.java
Normal file
11
lab/lab11/Question5/Start.java
Normal file
@@ -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();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user