-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCQueryField.java
More file actions
160 lines (148 loc) · 4.11 KB
/
Copy pathCQueryField.java
File metadata and controls
160 lines (148 loc) · 4.11 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
// -*- 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.Dimension;
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 javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CQueryField extends JPanel implements MouseListener, MouseMotionListener{
private static final long serialVersionUID = 328149080259L;
private JTextField field;
private boolean pressed = false;
private boolean mouseover = false;
public CQueryField (){
this(null);
}
public CQueryField (String text){
super(new BorderLayout());
field = new JTextField(text);
field.setBorder(null);
field.setFont(new Font("Ariel", Font.PLAIN, 13));
this.setBounds(0,0,200,20);
this.setPreferredSize(new Dimension(200,20));
this.setOpaque(false);
this.add(field, BorderLayout.CENTER);
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.revalidate();
this.repaint();
}
public Insets getInsets(){
int h= this.getHeight();
return new Insets(h/6,h,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);
//RoundRectangle2D.Double box = new RoundRectangle2D.Double(w-5*h/6, h/6, 2*h/3, 2*h/3, h/3, h/3);
return box;
}
private Shape getMag(int w, int h){
Ellipse2D.Double e = new Ellipse2D.Double(h/2, h/6, h*1/3, h*1/3) ;
GeneralPath shape = new GeneralPath();
shape.moveTo(h/3, h*2/3);
shape.lineTo(h/2,h/2);
shape.append(e, false);
return shape;
}
public JTextField getQueryField(){
return field;
}
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);
g2.setStroke(new BasicStroke(3));
g2.setColor(Color.darkGray.brighter());
g2.draw(this.getMag(w,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 String getText(){
return field.getText();
}
public void setText(String text){
field.setText(text);
}
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){
field.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 CQueryField());
f.setVisible(true);
}
}