-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCMenuItem.java
More file actions
75 lines (70 loc) · 2.36 KB
/
Copy pathCMenuItem.java
File metadata and controls
75 lines (70 loc) · 2.36 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
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the MIT License https://raw.github.com/mit-cml/app-inventor/master/mitlicense.txt
package openblocks.codeblockutil;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Rectangle2D;
import javax.swing.JButton;
public class CMenuItem extends JButton implements MouseListener{
private static final long serialVersionUID = 328149080429L;
public enum Position{CENTER, LEFT};
private Color background = CGraphite.darkgreen;
private Color highlight = CGraphite.white;
private boolean focus = false;
Position textPosition;
public CMenuItem(String text){
this(text, Position.LEFT);
}
public CMenuItem(String text, Position position){
super();
super.setText(text);
super.setOpaque(false);
this.addMouseListener(this);
textPosition = position;
}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;
if(focus){
g2.setColor(background);
}else{
g.setColor(highlight);
}
g2.fillRect(0,0,this.getWidth(), this.getHeight());
String text = this.getText();
if(text != null){
Font font = g2.getFont().deriveFont((float)(((float)this.getHeight()) * .8));
g2.setFont(font);
FontMetrics metrics = g2.getFontMetrics();
Rectangle2D textBounds = metrics.getStringBounds(this.getText(),g2);
double textHeight = textBounds.getHeight();
double textWidth = textBounds.getWidth() > this.getWidth() ? this.getWidth()/2 : textBounds.getWidth();
float y = (float)((this.getHeight() / 2) + (textHeight / 2)) - metrics.getDescent();
float x;
if(textPosition == Position.LEFT){
x = 10;
}else{
x = (float)((this.getWidth() / 2) - (textWidth / 2));
}
g2.setColor(Color.black);
g2.drawString(text, x, y);
}
}
public void mouseEntered(MouseEvent e) {
this.focus = true;
this.repaint();
}
public void mouseExited(MouseEvent e) {
this.focus = false;
this.repaint();
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
}