-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCTextField.java
More file actions
147 lines (135 loc) · 3.7 KB
/
Copy pathCTextField.java
File metadata and controls
147 lines (135 loc) · 3.7 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
// -*- 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.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.text.Format;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.event.DocumentListener;
public class CTextField extends JFormattedTextField implements MouseListener, MouseMotionListener{
private static final long serialVersionUID = 328149080250L;
private boolean pressed = false;
private boolean mouseover = false;
public CTextField (){
this("");
}
public CTextField (Format format){
super(format);
this.setBorder(null);
this.setFont(new Font("Ariel", Font.PLAIN, 13));
this.setOpaque(false);
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.revalidate();
this.repaint();
}
public CTextField (String text){
super(text);
this.setBorder(null);
this.setFont(new Font("Ariel", Font.PLAIN, 13));
this.setOpaque(false);
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.revalidate();
this.repaint();
}
public Insets getInsets(){
int h= this.getHeight();
return new Insets(h/6,h/2,h/6,h);
}
private Shape getXCross(int w, int h){
GeneralPath shape = new GeneralPath();
shape.moveTo(w-h*2/3,h/3);
shape.lineTo(w-h/3,h*2/3);
shape.moveTo(w-h/3, h/3);
shape.lineTo(w-h*2/3, h*2/3);
return shape;
}
private Shape getXBox(int w, int h){
Ellipse2D.Double box = new Ellipse2D.Double(w-5*h/6, h/6, 2*h/3, 2*h/3);
return box;
}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;
int w = this.getWidth();
int h = this.getHeight();
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.white);
g2.fillRoundRect(0,0,w,h,h,h);
if(mouseover){
if(pressed){
g2.setColor(new Color(170,0,0));
}else{
g2.setColor(Color.red);
}
}else{
g2.setColor(Color.pink);
}
g2.fill(this.getXBox(w, h));
g2.setColor(Color.white);
g2.setStroke(new BasicStroke(2));
g2.draw(this.getXCross(w, h));
super.paint(g);
}
public void addDocumentListener(DocumentListener l){
this.getDocument().addDocumentListener(l);
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {
mouseover = false;
this.repaint();
}
public void mousePressed(MouseEvent e) {
if (e.getX()> this.getWidth()-this.getHeight()*5/6){
pressed = true;
this.repaint();
}
}
public void mouseReleased(MouseEvent e) {
if (e.getX()> this.getWidth()-this.getHeight()*5/6){
this.setText("");
pressed=false;
this.repaint();
}
}
public void mouseMoved(MouseEvent e){
if (e.getX()> this.getWidth()-this.getHeight()*5/6){
if(mouseover == false){
mouseover = true;
this.repaint();
}
}else{
if(mouseover == true){
mouseover = false;
this.repaint();
}
}
}
public void mouseDragged(MouseEvent e){}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new BorderLayout());
f.setSize(400,100);
f.add(new CTextField());
f.setVisible(true);
}
}