-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask1bVidhyaMoorthy.java
More file actions
226 lines (210 loc) · 6.51 KB
/
Copy pathTask1bVidhyaMoorthy.java
File metadata and controls
226 lines (210 loc) · 6.51 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
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
/**
*
* @author VidhyaMoorthy
* Task1b --> Add a menu bar and add a menu called "Color" to change the background of the JFrame
* depending on the menu item selected
*
*/
public class Task1bVidhyaMoorthy extends JFrame implements ActionListener{
// initialize data members
private JPanel contentPane;
private JMenuBar menubar;
private JMenu color;
private JMenuItem red;
private JMenuItem blue;
private JMenuItem green;
private JMenuItem yellow;
private JMenuItem black;
private JMenuItem pink;
private JMenuItem cyan;
private JMenuItem magenta;
private JMenuItem random;
/**
* Launch the application with a frame class that extends JFrame
*/
public static void main(String[] args) {
Task1bVidhyaMoorthy frame = new Task1bVidhyaMoorthy();
frame.setVisible(true);
}
/**
* Create the frame that will hold the panel of components that draws the house as a graphic
*/
public Task1bVidhyaMoorthy() {
/**
* this method initiates the frame class that extends JFrame and calls the paint() method to display the
* house graphic
*/
setTitle(" HOUSE ");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(200, 200, 550, 400);
contentPane = new JPanel(); // creating the panel that holds the graphic
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//Create a menu bar that will hold the color menu
menubar = new JMenuBar();
/**
* Creating a menu "color" that holds the list of colors to be changed as background
* when selected
*/
color = new JMenu("Color");
/**
* Creating the menu items that represent the colors
* adding action listener to each menu item
*/
red = new JMenuItem("Red");
red.addActionListener(this);
blue = new JMenuItem("Blue");
blue.addActionListener(this);
green = new JMenuItem("Green");
green.addActionListener(this);
yellow = new JMenuItem("Yellow");
yellow.addActionListener(this);
black = new JMenuItem("Black");
black.addActionListener(this);
pink = new JMenuItem("Pink");
pink.addActionListener(this);
cyan = new JMenuItem("Cyan");
cyan.addActionListener(this);
magenta = new JMenuItem("Magenta");
magenta.addActionListener(this);
random = new JMenuItem("Random");
random.addActionListener(this);
//register menuItems to menu
color.add(red);
color.add(blue);
color.add(green);
color.add(yellow);
color.add(black);
color.add(pink);
color.add(cyan);
color.add(magenta);
color.add(random);
// register color menu in menu bar
menubar.add(color);
//set menu bar
setJMenuBar(menubar);
}
/**
* this method draws the house graphic by using coordinates on the panel
*/
public void paint(Graphics G){
/**
* this method draws the house graphic by using coordinates on the panel
*/
super.paint(G);
G.setColor(Color.BLACK); // set the color of graphic as black
Graphics2D g = (Graphics2D) G;
g.setStroke(new BasicStroke(10));
// drawing the house using coordinates(startx,starty,endx,endy)
g.drawLine(300, 600, 300, 900);
g.drawLine(600, 580, 600, 950);
g.drawLine(300, 900, 600, 950);
g.drawLine(250, 650, 450, 450);
g.drawLine(450, 450, 650, 625);
g.drawLine(600, 950, 1000, 830);
g.drawLine(1000, 830, 1000,530);
g.drawLine(450, 450, 980, 330);
g.drawLine(650, 625, 1100, 500);
g.drawLine(980, 330, 1100, 500);
g.drawLine(750, 685, 850, 660);
g.drawLine(750, 645, 850, 620);
g.drawLine(750, 725, 850, 700);
g.drawLine(750, 725, 750, 645);
g.drawLine(850, 700, 850, 620);
g.drawLine(800, 715, 800, 630);
g.drawLine(405, 915, 405, 730);
g.drawLine(465, 925, 465, 730);
g.drawLine(405, 730, 465, 730);
g.drawLine(405, 915, 325, 950);
g.drawLine(465, 925, 400, 960);
g.drawLine(700, 350, 700, 450);
g.drawLine(750, 330, 750, 420);
g.drawLine(700, 350, 750, 330);
g.drawOval(800, 320, 80, 85);
g.drawLine(840, 360, 900, 300);
g.drawLine(840, 360, 840, 430);
g.drawOval(420, 600, 40, 50);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
// If selected color is red
if(arg0.getSource()==red){
this.getContentPane().setBackground(Color.RED);
red.setBackground(Color.RED);
}
// If selected color is blue
if(arg0.getSource()==blue){
this.getContentPane().setBackground(Color.BLUE);
blue.setBackground(Color.BLUE);
}
// If selected color is green
if(arg0.getSource()==green){
this.getContentPane().setBackground(Color.GREEN);
green.setBackground(Color.GREEN);
}
// If selected color is yellow
if(arg0.getSource()==yellow){
this.getContentPane().setBackground(Color.YELLOW);
yellow.setBackground(Color.YELLOW);
}
// If selected color is black
if(arg0.getSource()==black){
this.getContentPane().getGraphics().setColor(Color.WHITE);
this.getContentPane().setBackground(Color.BLACK);
black.setBackground(Color.BLACK);
}
// If selected color is pink
if(arg0.getSource()==pink){
this.getContentPane().setBackground(Color.PINK);
pink.setBackground(Color.PINK);
}
// If selected color is cyan
if(arg0.getSource()==cyan){
this.getContentPane().setBackground(Color.CYAN);
cyan.setBackground(Color.CYAN);
}
// If selected color is magenta
if(arg0.getSource()==magenta){
this.getContentPane().setBackground(Color.MAGENTA);
magenta.setBackground(Color.MAGENTA);
}
// If selected color is random
/**
* create a list of possible colors and select a random one and
* change the background
*/
List<Color> colors = new ArrayList<>();
colors.add(Color.BLUE);
colors.add(Color.GREEN);
colors.add(Color.RED);
colors.add(Color.YELLOW);
colors.add(Color.BLACK);
colors.add(Color.PINK);
colors.add(Color.CYAN);
colors.add(Color.MAGENTA);
Random rand = new Random();
Color c =colors.get(rand.nextInt(colors.size()));
if(arg0.getSource()==random){
this.getContentPane().setBackground(c);
random.setBackground(c);
}
repaint();
}
}