Restore
This commit is contained in:
46
Labs/Lab11/Lab11_2230026071/Question5/src/MyFrame.java
Normal file
46
Labs/Lab11/Lab11_2230026071/Question5/src/MyFrame.java
Normal file
@@ -0,0 +1,46 @@
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.FlowLayout;
|
||||
import java.awt.Point;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
public class MyFrame extends JFrame {
|
||||
public MyFrame() {
|
||||
//Auxiliary
|
||||
this.setTitle("My Frame Title");
|
||||
this.setSize(400, 300);
|
||||
this.setLocationRelativeTo(null);
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
this.setLayout(new BorderLayout());
|
||||
|
||||
//JPanel
|
||||
JPanel p = new JPanel();
|
||||
p.setLayout(new FlowLayout());
|
||||
this.add(p, BorderLayout.PAGE_START);
|
||||
JButton b1 = new JButton("Reset");
|
||||
JButton b2 = new JButton("right");
|
||||
p.add(b1);
|
||||
p.add(b2);
|
||||
|
||||
//MyPanel
|
||||
MyPanel myPanel = new MyPanel();
|
||||
this.add(myPanel, BorderLayout.CENTER);
|
||||
|
||||
//button setting
|
||||
b1.addActionListener(new ActionListener() { // Anonymous class.
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
myPanel.clearAllPoints();
|
||||
}
|
||||
});
|
||||
|
||||
//setVisible
|
||||
this.setVisible(true);
|
||||
}
|
||||
}
|
||||
46
Labs/Lab11/Lab11_2230026071/Question5/src/MyPanel.java
Normal file
46
Labs/Lab11/Lab11_2230026071/Question5/src/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 {
|
||||
//instance variables
|
||||
private static ArrayList<Point> points = new ArrayList<Point>();
|
||||
|
||||
//constructor
|
||||
public MyPanel() {
|
||||
//mouse listener
|
||||
this.addMouseListener(new MouseAdapter() { // Anonymous class.
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
//if left button clicked
|
||||
if(e.getButton() == MouseEvent.BUTTON1) {
|
||||
Point p = e.getPoint();
|
||||
System.out.println("x: " + p.x + " y: " + p.y);
|
||||
points.add(p);
|
||||
repaint(); //force invoke paintComponent()
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//methods
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.setColor(Color.RED);
|
||||
for(int i = 0; i < points.size(); ++i) {
|
||||
if(i == 0) g.drawLine((int)points.get(0).x, (int)points.get(0).y, (int)points.get(0).x, (int)points.get(0).y);
|
||||
else g.drawLine((int)points.get(i).x, (int)points.get(i).y, (int)points.get(i-1).x, (int)points.get(i-1).y);
|
||||
}
|
||||
}
|
||||
|
||||
public void clearAllPoints() {
|
||||
points.clear(); //clear all the array elements
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
13
Labs/Lab11/Lab11_2230026071/Question5/src/Start.java
Normal file
13
Labs/Lab11/Lab11_2230026071/Question5/src/Start.java
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
public class Start {
|
||||
|
||||
public static void main(String[] args) {
|
||||
javax.swing.SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
new MyFrame();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user