-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProg.java
More file actions
165 lines (113 loc) · 2.88 KB
/
Prog.java
File metadata and controls
165 lines (113 loc) · 2.88 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package lessson13;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Prog {
public static void main(String[] args) {
MyFrame window = new MyFrame();
}
}
class MyFrame extends JFrame {
public MyFrame() {
setBounds(0, 0, 800, 600);
setTitle("Ðàáîòà ñ ìûøüþ");
Panel pan = new Panel();
Container con = getContentPane();
con.add(pan);
setVisible(true);
}
}
class Panel extends JPanel {
Color [] arrayColor;
static int tCol;
static int mX;
static int mY;
static boolean flag;
public Panel() {
addMouseListener(new MyMouse1());
addMouseMotionListener(new MyMouse2());
setFocusable(true);
}
public void paintComponent(Graphics gr) {
arrayColor = new Color [7];
arrayColor[0] = Color.BLACK;
arrayColor[1] = Color.GREEN;
arrayColor[2] = Color.BLUE;
arrayColor[3] = Color.RED;
arrayColor[4] = Color.YELLOW;
arrayColor[5] = Color.WHITE;
arrayColor[6] = Color.ORANGE;
for (int i = 0; i < arrayColor.length; i++) {
gr.setColor(arrayColor[i]);
gr.fillRect(i*100, 0, 100, 50);
if (flag) {
gr.setColor(arrayColor[tCol]);
gr.fillRect(mX, mY, 3, 3);
}
}
}
}
class MyMouse1 implements MouseListener {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
int tX = e.getX();
int tY = e.getY();
int col = e.getClickCount();
int btn = e.getButton();
if ((tX > 0) && (tX < 700) && (tY > 0) && (tY < 50)) {
if (col == 1) {
if (btn == 1) {
Panel.tCol = tX / 100;
}
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
class MyMouse2 implements MouseMotionListener {
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
int tX = e.getX();
int tY = e.getY();
if (tY >50) {
Panel.mX = tX;
Panel.mY = tY;
Panel.flag = true;
repaint();
}
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
int tX = e.getX();
int tY = e.getY();
if ((tX > 0) && (tX < 700) && (tY > 0) && (tY < 50)) {
setCursor(new Cursor (Cursor.HAND_CURSOR));
} else {
setCursor(new Cursor (Cursor.DEFAULT_CURSOR));
}
}
}