add lab11

This commit is contained in:
2022-05-11 23:36:36 +08:00
parent 855bc8cc3c
commit 243a44f7ed
15 changed files with 421 additions and 0 deletions

View 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);
}
}

View 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() + ")");
}
}
});
}
}

View 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();
}
});
}
}

View 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);
}
}

View 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);
}
}

View 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();
}
});
}
}

View 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);
}
}

View 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();
}
}

View 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();
}
});
}
}

View 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);
}
}

View 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();
}
}

View 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();
}
});
}
}

View 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);
}
}

View 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();
}
}

View 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();
}
});
}
}