-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise9.java
210 lines (190 loc) · 5.82 KB
/
Exercise9.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package exercises;
/** Exercise: 9. Balloons Add to the class Balloon some additional data:
* a String that holds the name of the balloon and a Color variable that describes its color.
* Add code to initialize these values using a constructor method.
* Add the code to display the colored balloon and its name.
* Enhance the balloon program with buttons that move the balloon left, right, up and down.
Filename: Exercise9.java
@author: © Gary Hill (200WXYZ)
Course: BSc Computing
Module: CSY1020 Problem Solving & Programming
Tutor: Gary Hill
@version: 1.0
Date: 11/06/19
*/
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.lang.reflect.Field;
import java.awt.event.ActionEvent;
public class Exercise9 {
private JFrame frmExercise;
private Balloon panel;
private JLabel label;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Exercise9 window = new Exercise9();
window.frmExercise.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Exercise9() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmExercise = new JFrame();
frmExercise.setTitle("Exercise9");
frmExercise.setBounds(100, 100, 450, 300);
frmExercise.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmExercise.getContentPane().setLayout(null);
panel = new Balloon();
panel.setPreferredSize(new Dimension(450,175));
panel.setBounds(0, 0, 450, 175);
frmExercise.getContentPane().add(panel);
JLabel lblName = new JLabel("Name");
lblName.setBounds(10, 211, 48, 14);
frmExercise.getContentPane().add(lblName);
JLabel lblColor = new JLabel("Color");
lblColor.setBounds(10, 236, 48, 14);
frmExercise.getContentPane().add(lblColor);
textField = new JTextField();
textField.setBounds(67, 208, 96, 20);
frmExercise.getContentPane().add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(68, 233, 96, 20);
frmExercise.getContentPane().add(textField_1);
textField_1.setColumns(10);
JButton btnUpdate = new JButton("Update");
btnUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = textField.getText();
String color = textField.getText();
panel.Balloon(name, color);
label.setText(name);
}
});
btnUpdate.setBounds(171, 232, 89, 23);
frmExercise.getContentPane().add(btnUpdate);
JButton btnUp = new JButton("Up");
btnUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//calls the panel method
panel.up();
}
});
btnUp.setBounds(322, 183, 60, 23);
frmExercise.getContentPane().add(btnUp);
JButton btnDown = new JButton("Down");
btnDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.down();
}
});
btnDown.setBounds(322, 232, 76, 23);
frmExercise.getContentPane().add(btnDown);
JButton btnLeft = new JButton("Left");
btnLeft.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.left();
}
});
btnLeft.setBounds(275, 207, 71, 23);
frmExercise.getContentPane().add(btnLeft);
JButton btnRight = new JButton("Right");
btnRight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.right();
}
});
btnRight.setBounds(356, 207, 68, 23);
frmExercise.getContentPane().add(btnRight);
label = new JLabel("");
label.setBounds(53, 187, 150, 14);
frmExercise.getContentPane().add(label);
}
//inner class inorder to inherit the Graphics object
class Balloon extends JPanel{
private String name;
private String color;
//the superclass constructor
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.WHITE);
g.setColor(Color.blue);
//g.fillOval(x, y, width, height);
g.fillOval(185, 45, 50, 75);
}
public void Balloon(String name, String color) {
Graphics g = this.getGraphics();
//redraw the initial artifact
repaint();
this.name= name;
this.color = color;
setBackground(Color.WHITE);
g.setColor(Color.getColor(this.color));
g.fillOval(185, 45, 50, 75);
}
//method to move the balloon up
public void up() {
Graphics g = this.getGraphics();
//g.clearRect(0, 0, 500, 500);
//setBackground(Color.WHITE);
//clear the canvas in order to redraw the artifact
g.setColor(Color.WHITE);
g.fillRect(0, 0, 500, 500);
g.setColor(Color.black);
g.fillOval(185, 2, 50, 75);
}
//method to move the balloon down
public void down() {
Graphics g = this.getGraphics();
//g.clearRect(0, 0, 500, 500);
//super.setBackground(Color.WHITE);
//clear the canvas in order to redraw the artifact
g.setColor(Color.WHITE);
g.fillRect(0, 0, 500, 500);
g.setColor(Color.black);
g.fillOval(185, 100, 50, 75);
}
//method to move the balloon right
public void right() {
Graphics g = this.getGraphics();
//g.clearRect(0, 0, 500, 500);
super.setBackground(Color.WHITE);
//g.fillOval(x, y, width, height);
//clear the canvas in order to redraw the artifact
g.setColor(Color.WHITE);
g.fillRect(0, 0, 500, 500);
g.setColor(Color.black);
g.fillOval(350, 45, 50, 75);
}
//method to move the balloon left
public void left() {
Graphics g = this.getGraphics();
//g.clearRect(0, 0, 500, 500);
//clear the canvas in order to redraw the artifact
g.setColor(Color.WHITE);
g.fillRect(0, 0, 500, 500);
g.setColor(Color.black);
g.fillOval(30, 45, 50, 75);
}
}
}