Restore
This commit is contained in:
10
Labs/Lab11/Lab11_2230026071/Question5/.classpath
Normal file
10
Labs/Lab11/Lab11_2230026071/Question5/.classpath
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
17
Labs/Lab11/Lab11_2230026071/Question5/.project
Normal file
17
Labs/Lab11/Lab11_2230026071/Question5/.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Question5</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,2 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
||||
BIN
Labs/Lab11/Lab11_2230026071/Question5/bin/MyFrame$1.class
Normal file
BIN
Labs/Lab11/Lab11_2230026071/Question5/bin/MyFrame$1.class
Normal file
Binary file not shown.
BIN
Labs/Lab11/Lab11_2230026071/Question5/bin/MyFrame.class
Normal file
BIN
Labs/Lab11/Lab11_2230026071/Question5/bin/MyFrame.class
Normal file
Binary file not shown.
BIN
Labs/Lab11/Lab11_2230026071/Question5/bin/MyPanel$1.class
Normal file
BIN
Labs/Lab11/Lab11_2230026071/Question5/bin/MyPanel$1.class
Normal file
Binary file not shown.
BIN
Labs/Lab11/Lab11_2230026071/Question5/bin/MyPanel.class
Normal file
BIN
Labs/Lab11/Lab11_2230026071/Question5/bin/MyPanel.class
Normal file
Binary file not shown.
BIN
Labs/Lab11/Lab11_2230026071/Question5/bin/Start$1.class
Normal file
BIN
Labs/Lab11/Lab11_2230026071/Question5/bin/Start$1.class
Normal file
Binary file not shown.
BIN
Labs/Lab11/Lab11_2230026071/Question5/bin/Start.class
Normal file
BIN
Labs/Lab11/Lab11_2230026071/Question5/bin/Start.class
Normal file
Binary file not shown.
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