-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtonSlider-3.java
More file actions
316 lines (301 loc) · 9.95 KB
/
ButtonSlider-3.java
File metadata and controls
316 lines (301 loc) · 9.95 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*Sudipti Dantuluri
* 3.16.2022
* ButtonSlider.java
* The program uses layouts to output pillsbury slider or border layouts.*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.Scanner;
public class ButtonSlider extends JFrame {
PillsburyLayoutP2 panel;
Practice canvas;
JFrame frame;
public ButtonSlider() {
super("ButtonSlider.java");
setSize( 900, 500);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocation(80,50);
setResizable(true);
//PillsburyLayoutP2 pillsPanel2 = new PillsburyLayoutP2();
//setContentPane(pillsPanel2); // OR frame.getContentPane().add(p_in);
//setVisible(true);
}
public static void main(String[] args) {
ButtonSlider sl = new ButtonSlider();
sl.slider();
}
public void slider() {
Scanner sc = new Scanner(System.in);
System.out.print("Would you like to run Pillsbury(1) or BorderLayout(2)? ");
int choice = sc.nextInt();
System.out.print("Here you go!");
/*if (choice==1) {
ButtonSlider pL2 = new ButtonSlider();
}
if (choice==2) {
//BorderLayoutExample2 BLE = new BorderLayoutExample2();
}*/
if (choice==1) {
PillsburyLayoutP2 pillsPanel2 = new PillsburyLayoutP2();
setContentPane( pillsPanel2 ); // OR frame.getContentPane().add(p_in);
setVisible(true);
} else if (choice==2) {
setSize( 600, 600);
Practice pc = new Practice();
setContentPane( pc ); // OR frame.getContentPane().add(p_in);
setVisible(true);
}
frame.setVisible(true);
}
}
class PillsburyLayoutP2 extends JPanel {
private boolean pressed1, pressed2, pressed3;
int countCommand2 = 0;
int countCommand = 0;
private JButton button1, button2, button3, button4;
private Font font;
private int counter;
private JSlider slider;
private int sliderValue;
public PillsburyLayoutP2()
{
pressed1 = pressed2 = false;
font = new Font ("Serif", Font.BOLD, 30);
setBackground( Color.BLUE );
//setLayout( new FlowLayout( FlowLayout.RIGHT, 0, 0) );
setLayout( new FlowLayout( FlowLayout.LEFT, 0, 0) );
button1 = new JButton("Press my belly."); // construct button
Button1Handler b1handler = new Button1Handler(); // this is so the actionPerformed is dedicated to this button only
button1.addActionListener( b1handler ); // add listener to button
add( button1 ); // add button to panel (Pillsbury2)
button1.setPreferredSize(new Dimension(140,50)); //original height = 25
button2 = new JButton("Hello");
Button2Handler b2handler = new Button2Handler();
button2.addActionListener( b2handler );
add( button2 );
button2.setPreferredSize(new Dimension(70,50)); //original height = 25
button3 = new JButton("Press my belly.");
Button1Handler b3handler = new Button1Handler();
button3.addActionListener( b1handler );
add( button3 );
button3.setPreferredSize(new Dimension(140,50)); //original height = 25
button4 = new JButton("Hello");
Button2Handler b4handler = new Button2Handler();
button4.addActionListener( b2handler );
add( button4 );
button4.setPreferredSize(new Dimension(70,50)); //original height = 25
JPanel jp = new JPanel();
add ( jp );
slider = new JSlider (JSlider.HORIZONTAL, 10, 125, 12); // construct slider bar
slider.setMajorTickSpacing(67); // create tick marks on slider every 40 units
slider.setMinorTickSpacing(10);
slider.setForeground(Color.BLUE);
slider.setPaintTicks(true);
slider.setLabelTable( slider.createStandardLabels(50) ); // create labels on tick marks
slider.setPaintLabels(true);
SliderHandler sHandler = new SliderHandler();
add(slider); // add button to panel
slider.addChangeListener( sHandler ); // add listener to slider
sliderValue = 12;
slider.setInverted(true);
}
public void paintComponent(Graphics g)
{
super.paintComponent (g);
Font f = new Font("Serif", Font.BOLD, sliderValue);
g.setFont(f);
g.drawString ("Move the slider to see",100, 250 ); //length 43 total
g.drawString ("the font size change",100,285); //length 22
if (pressed1 && countCommand%5!=0)
{
Font f2 = new Font("Serif", Font.PLAIN, 25);
g.setFont( f2 );
g.drawString("Hee-Hee", 100, 150);
Image pillsbury = new ImageIcon("Pillbury-Doughboy1.jpg").getImage();
g.drawImage(pillsbury, 10, 320, 150, 140, null);
g.drawString("You're wonderful!",160,460);
pressed1 = false;
}
if (pressed2 && countCommand%5!=0)
{
Font f2 = new Font("Serif", Font.PLAIN, 25);
g.setFont( f2 );
g.drawString("Hee-Hee", 100, 150);
Image pillsbury = new ImageIcon("Pillbury-Doughboy1.jpg").getImage();
g.drawImage(pillsbury, 10, 320, 150, 140, null);
g.drawString("You're wonderful!",160,460);
pressed2 = false;
}
if (pressed3 && countCommand2%4==0) {
Font f2 = new Font("Serif", Font.PLAIN, 25);
g.setFont( f2 );
g.drawString("Stop saying Hello", 100, 150);
pressed3 = false;
}
if (pressed3) {
Font f2 = new Font("Serif", Font.PLAIN, 25);
g.setFont( f2 );
g.drawString("Goodbye", 100, 150);
pressed3 = false;
}
if (pressed1 || pressed2 && countCommand%5==0) {
if (pressed1) {
Font f2 = new Font("Serif", Font.PLAIN, 25);
g.setFont( f2 );
g.drawString("Stop pressing my belly", 100, 150);
pressed1 = false;
}
if (pressed2) {
Font f2 = new Font("Serif", Font.PLAIN, 25);
g.setFont( f2 );
g.drawString("Stop pressing my belly", 100, 150);
pressed2 = false;
}
}
}
class Button1Handler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
System.out.println("line164: " + command);
if ( command.equals ("Press my belly.") )
{
pressed1 = true;
countCommand++;
button1.setText ("reset");
}
else {
button1.setText("Press my belly.");
}
if ( command.equals ("Press my belly") )
{
pressed2 = true;
countCommand++;
button3.setText("reset");
}
else {
button3.setText("Press my belly");
}
repaint();
}
} // end class Button1Handler
class Button2Handler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String command2 = e.getActionCommand();
System.out.println("line 192: " + command2);
if ( command2.equals ("Hello") )
{
pressed3 = true;
countCommand2++;
//button2.setText ("Goodbye");
}
else
button2.setText("Hello"); //4 times stop saying hello
repaint();
}
} // end class Button2Handler
class SliderHandler implements ChangeListener
{
public void stateChanged(ChangeEvent e)
{ // event handler for JSliderBar (stateChanged)
sliderValue = slider.getValue();
repaint();
}
}
}
class Practice extends JPanel
{
private boolean pressedMath, pressedScience, pressedJava;
int countCommand1 = 0;
//int countCommand = 0;
int mathcount = 0;
int sciencecount = 0;
int javacount = 0;
int totalNum = 0;
public Practice()
{
setBackground( Color.YELLOW );
runIt();
}
public void paintComponent(Graphics g)
{
super.paintComponent (g);
//g.drawString("JoeBob",50,400);
/*if (ba1) {
num1 = 0;
num2 = 0;
num3 = 0;
totalNum = 0;
}
if (ba2) {
pressedScience = true;
num2++;
}*/
}
public void runIt()
{
JButton ba1 = new JButton("Reset");
ba1.setPreferredSize(new Dimension(600,20));
JLabel jl1 = new JLabel("<html>" + "Total Time: " + "<br>" +totalNum+ " hrs" + "</html>");
//JButton ba2=new JButton("Total Time: " + num + " hrs");
//ba2.setPreferredSize(new Dimension(600,20));
clickHandle resethandler = new clickHandle();
ba1.addActionListener(resethandler);;
JButton ba3 = new JButton("<html>" + "Java" + "<br>" +javacount+ "</html>");
ba3.setPreferredSize(new Dimension(200,50));
clickHandle javahandler = new clickHandle();
ba3.addActionListener(javahandler);;
JButton ba4=new JButton("<html>" + "Math" + "<br>" +mathcount+ "</html>");
ba4.setPreferredSize(new Dimension(200,50));
clickHandle mathhandler = new clickHandle();
ba4.addActionListener(mathhandler);;
//JButton ba5=new JButton("Center");
//JLabel jl1 = new JLabel("<html>" + "Science" + "<br>" +num+ "</html>");
JButton ba2 = new JButton("<html>" + "Science" + "<br>" +sciencecount+ "</html>");
ba2.setPreferredSize(new Dimension(200,50));
clickHandle sciencehandler = new clickHandle();
ba2.addActionListener(sciencehandler);;
//jl1.setPreferredSize(new Dimension(600,50));
jl1.setHorizontalAlignment(JLabel.CENTER);
JPanel pa = new JPanel();
add(pa);
pa.setLayout(new BorderLayout());
pa.add(ba1, BorderLayout.NORTH);
pa.add(jl1, BorderLayout.SOUTH);
pa.add(ba3, BorderLayout.EAST);
pa.add(ba4, BorderLayout.WEST);
pa.add(ba2, BorderLayout.CENTER);
}
class clickHandle implements ActionListener
{
private boolean pressedMath, pressedScience, pressedJava;
public void actionPerformed(ActionEvent e)
{
String commandClick = e.getActionCommand();
System.out.println(commandClick);
if (commandClick.indexOf("Math") != -1) {
mathcount++;
System.out.println("MATH matched");
}
if (commandClick.indexOf("Java") != -1) {
javacount++;
System.out.println("java matched");
}
if (commandClick.indexOf("Reset") != -1) {
totalNum++;
System.out.println("Reset matched");
}
if (commandClick.indexOf("Science") != -1) {
sciencecount++;
System.out.println("Science matched");
}
repaint();
//ba2= JButton("<html>" + "Science" + "<br>" +num2+ "</html>");
//ba2.setPreferredSize(new Dimension(200,50));
}
} // end class Button2Handler
}