-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCArrowButton.java
More file actions
157 lines (151 loc) · 4.29 KB
/
Copy pathCArrowButton.java
File metadata and controls
157 lines (151 loc) · 4.29 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
// -*- 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.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.geom.GeneralPath;
/**
* A CArrowButton is a swing-compatible widget that
* allows clients to display a semi-transpanrent arrow
* in any of the four traditional directions:
* NORTH, SOUTH, EAST, WEST.
*
* Clients of the CArrowButton may subscribe mouse triggers
* to particular actions by doing the following:
* this.addCButtonListener(new CButtonListener());
*/
public abstract class CArrowButton extends CButton implements ActionListener{
private static final long serialVersionUID = 328149080231L;
/** Directions */
public enum Direction{NORTH, SOUTH, EAST, WEST};
private static final int m = 3;
private final Direction dir;
private static final Color highlight = CGraphite.darkgreen;
private static final Color arrowColor = CGraphite.darkgreen;
/**
* @param dir
* @effects Constructs a new gray arrow button that
* brightens up to white when mouse-over
*/
public CArrowButton(Direction dir){
super(Color.black, CGraphite.darkgreen, null);
this.setOpaque(false);
this.dir=dir;
this.addActionListener(this);
}
private Shape getShape(Direction dir){
if(dir == Direction.NORTH){
return new GeneralPath();
}else if(dir == Direction.SOUTH){
return new GeneralPath();
}else if(dir == Direction.EAST){
GeneralPath arrow = new GeneralPath();
arrow.moveTo(m, m);
arrow.lineTo(this.getWidth()-m, this.getHeight()/2);
arrow.lineTo(m, this.getHeight()-m);
arrow.lineTo(m, m);
arrow.closePath();
return arrow;
}else {//if(dir == WEST){
GeneralPath arrow = new GeneralPath();
arrow.moveTo(this.getWidth()-m, m);
arrow.lineTo(m, this.getHeight()/2);
arrow.lineTo(this.getWidth()-m, this.getHeight()-m);
arrow.lineTo(this.getWidth()-m, m);
arrow.closePath();
return arrow;
}
/*
if(dir == Direction.NORTH){
return new GeneralPath();
}else if(dir == Direction.SOUTH){
return new GeneralPath();
}else if(dir == Direction.EAST){
GeneralPath shape = new GeneralPath();
shape.moveTo(m,2*m);
shape.lineTo(2*m,2*m);
shape.lineTo(2*m,m);
shape.lineTo(w-m,h/2);
shape.lineTo(2*m,h-m);
shape.lineTo(2*m,h-2*m);
shape.lineTo(m, h-2*m);
shape.lineTo(m,2*m);
shape.closePath();
return shape;
}else {//if(dir == WEST){
GeneralPath shape = new GeneralPath();
shape.moveTo(m,h/2);
shape.lineTo(w-2*m,m);
shape.lineTo(w-2*m,2*m);
shape.lineTo(w-m,2*m);
shape.lineTo(w-m,h-2*m);
shape.lineTo(w-2*m,h-2*m);
shape.lineTo(w-2*m,h-m);
shape.lineTo(m, h/2);
shape.closePath();
return shape;
}
*/
}
/**
* repaints this
*/
public void paint(Graphics g){
//super.paint(g);
int w = this.getWidth();
int h = this.getHeight();
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Shape arrow = this.getShape(this.dir);
if (focus){
g2.setColor(Color.gray);
g2.drawRoundRect(0,0,w-1,h-1,2*m,2*m);
}
if(pressed){
//g2.setPaint(new GradientPaint(0, 0, fade, 0, this.getHeight()/2,arrowColor, true));
g2.setColor(highlight);
g2.fill(arrow);
g2.setColor(Color.yellow);
g2.draw(arrow);
}else{
g2.setColor(arrowColor);
g2.fill(arrow);
g2.setColor(Color.white);
g2.draw(arrow);
}
}
/**
* continue to trigger the action of this arrow as user hold down the arrow
*/
public void mousePressed(MouseEvent e) {
this.pressed=true;
this.repaint();
//timer.start();
}
/**
* stop triggering the action os this arrow as the user holds down the arrow
*/
public void mouseReleased(MouseEvent e) {
this.pressed=false;
this.repaint();
//timer.stop();
}
/**
* this method has no use
*/
public void actionPerformed(ActionEvent e){
triggerAction();
}
/**
* The action triggered by mouse clicks and pressing and holding arrows
*/
abstract public void triggerAction();
}