FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaExercises/Java 1/SwingHungryCats.java at master · biblelamp/JavaExercises · GitHub
biblelamp
/
JavaExercises
Public
Notifications
You must be signed in to change notification settings
Fork
130
Star
153
Code
Issues
1
Pull requests
9
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaExercises
/
Java 1
/
SwingHungryCats.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
131 lines (114 loc) · 3.86 KB
Breadcrumbs
JavaExercises
/
Java 1
/
SwingHungryCats.java
Copy path
File metadata and controls
131 lines (114 loc) · 3.86 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/**
* Java. Level 1. Lesson 7. Additional homework
*
* @author Sergey Iryupin
* @version 0.2 dated Jul 19, 2019
* @see HW7Lesson.java using classes Cat and Plate
*/
import
javax
.
swing
.*;
import
java
.
awt
.*;
import
java
.
awt
.
event
.*;
import
java
.
util
.
Random
;
class
SwingHungryCats
extends
JFrame
{
final
String
TITLE_OF_PROGRAM
=
"Hungry Cats"
;
final
String
BTN_ADD_FOOD
=
"Add food"
;
final
String
BTN_FEED_CATS
=
"Feed cats"
;
final
String
BTN_RESET
=
"Reset"
;
final
int
WINDOW_WIDTH
=
300
;
final
int
WINDOW_HEIGHT
=
400
;
final
int
PLATE_VOLUME
=
50
;
final
int
PLATE_INIT
=
25
;
final
int
PLATE_ADD
=
5
;
final
int
NUMBER_OF_CATS
=
10
;
final
int
MAX_APPETITE
=
20
;
Panel
panel
;
SwingPlate
plate
;
// plate with food
SwingCat
[]
cats
;
// array of cats
public
static
void
main
(
String
args
[]) {
new
SwingHungryCats
();
}
SwingHungryCats
() {
setTitle
(
TITLE_OF_PROGRAM
);
setDefaultCloseOperation
(
EXIT_ON_CLOSE
);
setSize
(
WINDOW_WIDTH
,
WINDOW_HEIGHT
);
setLocationRelativeTo
(
null
);
// to the center
setResizable
(
false
);
panel
=
new
Panel
();
panel
.
setBackground
(
Color
.
white
);
Random
random
=
new
Random
();
plate
=
new
SwingPlate
(
PLATE_VOLUME
,
PLATE_INIT
);
cats
=
new
SwingCat
[
NUMBER_OF_CATS
];
for
(
int
i
=
0
;
i
<
cats
.
length
;
i
++)
cats
[
i
] =
new
SwingCat
(
""
,
random
.
nextInt
(
MAX_APPETITE
) +
1
);
JButton
btnAdd
=
new
JButton
(
BTN_ADD_FOOD
);
btnAdd
.
addActionListener
(
new
ActionListener
() {
public
void
actionPerformed
(
ActionEvent
e
) {
plate
.
add
(
PLATE_ADD
);
panel
.
repaint
();
}
});
JButton
btnFeed
=
new
JButton
(
BTN_FEED_CATS
);
btnFeed
.
addActionListener
(
new
ActionListener
() {
public
void
actionPerformed
(
ActionEvent
e
) {
for
(
SwingCat
cat
:
cats
) {
cat
.
eat
(
plate
);
}
panel
.
repaint
();
}
});
JButton
btnReset
=
new
JButton
(
BTN_RESET
);
btnReset
.
addActionListener
(
new
ActionListener
() {
public
void
actionPerformed
(
ActionEvent
e
) {
for
(
SwingCat
cat
:
cats
) {
cat
.
setFullness
(
false
);
}
panel
.
repaint
();
}
});
JPanel
btnPanel
=
new
JPanel
();
btnPanel
.
setLayout
(
new
GridLayout
());
// for button panel
btnPanel
.
add
(
btnAdd
);
btnPanel
.
add
(
btnFeed
);
btnPanel
.
add
(
btnReset
);
setLayout
(
new
BorderLayout
());
// for main window
add
(
btnPanel
,
BorderLayout
.
NORTH
);
add
(
panel
,
BorderLayout
.
CENTER
);
setVisible
(
true
);
}
class
Panel
extends
JPanel
{
// for painting
@
Override
public
void
paint
(
Graphics
g
) {
super
.
paint
(
g
);
Dimension
size
=
this
.
getSize
();
plate
.
paint
(
g
, (
int
)
size
.
getWidth
(),
20
);
for
(
int
i
=
0
;
i
<
cats
.
length
;
i
++) {
cats
[
i
].
paint
(
g
, (
i
+
1
) *
30
+
10
,
(
int
)(
size
.
getWidth
()) /
PLATE_VOLUME
,
20
);
}
}
}
}
class
SwingCat
extends
Cat
{
SwingCat
(
String
name
,
int
appetite
) {
super
(
name
,
appetite
);
}
void
paint
(
Graphics
g
,
int
y
,
int
dx
,
int
dy
) {
if
(
fullness
) {
g
.
setColor
(
Color
.
green
);
g
.
fill3DRect
(
20
,
y
,
appetite
*
dx
+
1
,
dy
+
1
,
true
);
}
g
.
setColor
(
Color
.
black
);
g
.
drawRect
(
20
,
y
,
appetite
*
dx
,
dy
);
}
}
class
SwingPlate
extends
Plate
{
SwingPlate
(
int
volume
,
int
food
) {
super
(
volume
,
food
);
}
void
paint
(
Graphics
g
,
int
windowWidth
,
int
dy
) {
g
.
setColor
(
Color
.
red
);
g
.
fill3DRect
(
10
,
10
,
food
* (
windowWidth
-
20
) /
volume
+
1
,
dy
+
1
,
false
);
g
.
setColor
(
Color
.
black
);
g
.
drawRect
(
10
,
10
,
windowWidth
-
20
,
dy
);
}
}
Back
|
FazBrowse Home
|
New Git URL