-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay30.java
More file actions
199 lines (165 loc) · 5.58 KB
/
Day30.java
File metadata and controls
199 lines (165 loc) · 5.58 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
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
// ---------------------------------------------------
// Author : Benjamin Kataliko Viranga
// Community : Stunt Business
// Community website : www.stuntbusiness.com
//
// 30 Days - Q&A Java basic
// Day 30 :Challenge XIV - Moving a Drawing in your GUI
// Day 30 | IG : https://www.instagram.com/benjivrik/
// ----------------------------------------------------
// what would be the output of this program ?
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.Box;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class DrawCircle extends JPanel {
/**
*
*/
private static final long serialVersionUID = 4418630376090808431L;
private int x = 75;
private int y = 25;
private int width = 100;
private int height = 100;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillOval(this.x, this.y, this.width, this.height);
}
// ypu could customize the following functions
public void moveLeft()
{
this.x--;
repaint();
}
public void moveRight()
{
this.x++;
repaint();
}
public void moveUp()
{
this.y--;
repaint();
}
public void moveDown()
{
this.y++;
repaint();
}
}
class GUI_D30 extends JPanel
{
/**
*
*/
private static final long serialVersionUID = -6021547927386781783L;
public static int currentUserID = 0; // for the user id in the text file
// GUI is your main panel
public GUI_D30()
{
JPanel imgPanel = new JPanel();
JLabel imgLabel = new JLabel(new ImageIcon("img_data/android-chrome-192x192.png"));
imgLabel.setHorizontalAlignment(JLabel.CENTER);
imgLabel.setVerticalAlignment(JLabel.CENTER);
imgLabel.setPreferredSize(new Dimension(200,270));
imgPanel.setBackground(Color.WHITE);
imgPanel.setPreferredSize(new Dimension(200, 270));
imgPanel.add(
imgLabel, BorderLayout.CENTER
);
JPanel infoPanel = new JPanel();
infoPanel.setBackground(Color.WHITE);
JPanel drawingPanel = new JPanel();
drawingPanel.setPreferredSize(new Dimension(250, 100));
drawingPanel.setLayout(new BoxLayout(drawingPanel, BoxLayout.Y_AXIS));
drawingPanel.setBackground(Color.WHITE);
DrawCircle circle = new DrawCircle();
drawingPanel.add(circle);
// Panel indicator
JLabel panelIndicator;
panelIndicator = new JLabel("No event.", SwingConstants.LEFT);
JButton left, right, up, down;
left = new JButton("Left");
left.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
circle.moveLeft();
panelIndicator.setText("Left");
}
}
);
right = new JButton("Right");
right.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
circle.moveRight();
panelIndicator.setText("Right");
}
}
);
up = new JButton("Up");
up.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
circle.moveUp();
panelIndicator.setText("Up");
}
}
);
down = new JButton("Down");
down.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
circle.moveDown();
panelIndicator.setText("Down");
}
}
);
JPanel buttonsPanel = new JPanel();
buttonsPanel.setBackground(Color.WHITE);
buttonsPanel.setPreferredSize(new Dimension(250, 50));
buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
buttonsPanel.add(left);
buttonsPanel.add(Box.createRigidArea(new Dimension(5, 0)));
buttonsPanel.add(right);
buttonsPanel.add(Box.createRigidArea(new Dimension(5, 0)));
buttonsPanel.add(up);
buttonsPanel.add(Box.createRigidArea(new Dimension(5, 0)));
buttonsPanel.add(down);
infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.Y_AXIS));
infoPanel.setPreferredSize(new Dimension(250, 270));
infoPanel.add(Box.createRigidArea(new Dimension(250, 30)));
infoPanel.add(drawingPanel);
infoPanel.add(Box.createRigidArea(new Dimension(250,20)));
infoPanel.add(buttonsPanel);
infoPanel.add(Box.createRigidArea(new Dimension(250,10)));
infoPanel.add(panelIndicator);
add(imgPanel, BorderLayout.CENTER);
add(infoPanel, BorderLayout.EAST);
}
}
public class Day30
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Moving a Circle.");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new GUI_D30());
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
frame.pack();
}
}