-
Notifications
You must be signed in to change notification settings - Fork 0
/
menuBar.java
168 lines (143 loc) · 5.96 KB
/
menuBar.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
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
public class menuBar extends JMenuBar {
private static final Color LOGO_RED = new Color(237, 28, 36);
private static final Color BACKGROUND_BLACK = Color.BLACK;
private static final Color TEXT_COLOR = Color.WHITE; // Changed to white
private static final Color HOVER_COLOR = new Color(255, 255, 255, 30);
public JMenuItem newfile, open, save, saveas, move, rename, print, exit;
public JMenuItem undo, redo, copy, paste, cut, selectall;
public menuBar() {
setOpaque(false);
setBorder(BorderFactory.createEmptyBorder());
JMenu file = createMenu("File");
JMenu edit = createMenu("Edit");
JMenu insert = createMenu("Insert");
JMenu help = createMenu("Help");
newfile = createMenuItem("New", KeyEvent.VK_N);
open = createMenuItem("Open", KeyEvent.VK_O);
save = createMenuItem("Save", KeyEvent.VK_S);
saveas = createMenuItem("Save As...", KeyEvent.VK_S, InputEvent.SHIFT_MASK);
move = createMenuItem("Move", KeyEvent.VK_M);
rename = createMenuItem("Rename", KeyEvent.VK_R);
print = createMenuItem("Print", KeyEvent.VK_P);
exit = createMenuItem("Exit", KeyEvent.VK_ESCAPE, 0);
undo = createMenuItem("Undo", KeyEvent.VK_Z);
redo = createMenuItem("Redo", KeyEvent.VK_Y);
copy = createMenuItem("Copy", KeyEvent.VK_C);
paste = createMenuItem("Paste", KeyEvent.VK_V);
cut = createMenuItem("Cut", KeyEvent.VK_X);
selectall = createMenuItem("Select All", KeyEvent.VK_A);
addItemsToMenu(file, newfile, open, save, saveas, move, rename, print, exit);
addItemsToMenu(edit, undo, redo, copy, paste, cut, selectall);
add(file);
add(edit);
add(insert);
add(help);
}
private JMenu createMenu(String title) {
JMenu menu = new JMenu(title) {
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(new GradientPaint(0, 0, LOGO_RED, 0, getHeight(), BACKGROUND_BLACK));
g2.fill(new Rectangle2D.Double(0, 0, getWidth(), getHeight()));
g2.dispose();
super.paintComponent(g);
}
@Override
public void updateUI() {
super.updateUI();
setOpaque(false);
setContentAreaFilled(false);
setBorderPainted(false);
}
};
menu.setForeground(TEXT_COLOR);
menu.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
menu.setFont(menu.getFont().deriveFont(Font.BOLD, 14f));
menu.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
menu.setOpaque(true);
menu.setBackground(HOVER_COLOR);
}
@Override
public void mouseExited(MouseEvent e) {
menu.setOpaque(false);
menu.setBackground(null);
}
});
return menu;
}
private JMenuItem createMenuItem(String title, int keyEvent) {
return createMenuItem(title, keyEvent, InputEvent.CTRL_MASK);
}
private JMenuItem createMenuItem(String title, int keyEvent, int modifiers) {
JMenuItem item = new JMenuItem(title) {
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(new GradientPaint(0, 0, BACKGROUND_BLACK, 0, getHeight(), LOGO_RED));
g2.fill(new Rectangle2D.Double(0, 0, getWidth(), getHeight()));
g2.dispose();
super.paintComponent(g);
}
@Override
public void updateUI() {
super.updateUI();
setOpaque(false);
setContentAreaFilled(false);
setBorderPainted(false);
}
};
item.setAccelerator(KeyStroke.getKeyStroke(keyEvent, modifiers));
item.setForeground(TEXT_COLOR);
item.setBorder(BorderFactory.createEmptyBorder(5, 15, 5, 15));
item.setFont(item.getFont().deriveFont(12f));
item.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
item.setOpaque(true);
item.setBackground(HOVER_COLOR);
}
@Override
public void mouseExited(MouseEvent e) {
item.setOpaque(false);
item.setBackground(null);
}
});
return item;
}
private void addItemsToMenu(JMenu menu, JMenuItem... items) {
for (JMenuItem item : items) {
menu.add(item);
}
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
GradientPaint gp = new GradientPaint(0, 0, LOGO_RED, 0, getHeight(), BACKGROUND_BLACK);
g2.setPaint(gp);
g2.fillRect(0, 0, getWidth(), getHeight());
// Add a subtle glossy effect
GradientPaint highlight = new GradientPaint(0, 0, new Color(255, 255, 255, 50), 0, getHeight() / 2, new Color(255, 255, 255, 0));
g2.setPaint(highlight);
g2.fillRect(0, 0, getWidth(), getHeight() / 2);
g2.dispose();
}
@Override
public void updateUI() {
super.updateUI();
setOpaque(false);
setBorderPainted(false);
}
}