-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraf.java
125 lines (115 loc) · 3.4 KB
/
graf.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.Rectangle;
import java.awt.RenderingHints.Key;
import java.awt.TextArea;
import java.awt.TextComponent;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.xml.crypto.dsig.keyinfo.KeyValue;
public class graf extends Frame implements MouseListener,MouseMotionListener,Runnable{
private int prx,pry,x,y;
class pl extends Canvas{
LinkedList<Rectangle> list=new LinkedList<>();
public void paint(Graphics g) {
for (int i = 0; i < list.size(); i++) {
g.drawRect((int)list.get(i).getX(),(int)list.get(i).getY(),(int)list.get(i).getWidth(),(int)list.get(i).getHeight());
}
g.drawRect(prx>x?x:prx, pry>y?y:pry,Math.abs(x-prx), Math.abs(y-pry));
}
}
pl can=new pl();
private Thread nit=new Thread(this);
public graf() {
super();setBounds(500,100,600,600);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {dispose();}
});
Panel p1=new Panel();can.setSize(600,600);can.addMouseListener(this);
can.addMouseMotionListener(this);//can.addKeyListener(this);
p1.add(can);can.setBackground(new Color(250,250,250));
can.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getExtendedKeyCode()==KeyEvent.VK_UP) {
pry-=5;y-=5;can.repaint();
}
else if (e.getExtendedKeyCode()==KeyEvent.VK_DOWN) {
pry+=5;y+=5;can.repaint();
}
else if (e.getExtendedKeyCode()==KeyEvent.VK_RIGHT) {
prx+=5;x+=5;can.repaint();
}
else if (e.getExtendedKeyCode()==KeyEvent.VK_LEFT) {
prx-=5;x-=5;can.repaint();
}
}
});
add(p1);nit.start();
setVisible(true);
}
public static void main(String[] args) {
graf fGraf=new graf();
}
public void run() {
while(!Thread.interrupted()) {
can.repaint();
//nit.sleep();
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
prx=e.getX();
pry=e.getY();
}
public void mouseReleased(MouseEvent e) {
can.list.add(new Rectangle(prx>x?x:prx, pry>y?y:pry,Math.abs(x-prx), Math.abs(y-pry)));
}
/*public void keyPressed(KeyEvent e) {
if (e.getExtendedKeyCode()==KeyEvent.VK_UP) {
pry-=5;y-=5;can.repaint();
}
else if (e.getExtendedKeyCode()==KeyEvent.VK_DOWN) {
pry+=5;y+=5;can.repaint();
}
else if (e.getExtendedKeyCode()==KeyEvent.VK_RIGHT) {
prx+=5;x+=5;can.repaint();
}
else if (e.getExtendedKeyCode()==KeyEvent.VK_LEFT) {
prx-=5;x-=5;can.repaint();
}
}*/
/*public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}*/
public void mouseDragged(MouseEvent e) {
x=e.getX();
y=e.getY();
can.repaint();
}
public void mouseMoved(MouseEvent e) {
}
}