Download this file

KeyButton.java    167 lines (142 with data), 5.2 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
161
162
163
164
165
166
167
package cologne.eck.all_peas.gui;
/*
* Peafactory - Production of Password Encryption Archives
* Copyright (C) 2015 Axel von dem Bruch
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* See: http://www.gnu.org/licenses/gpl-2.0.html
* You should have received a copy of the GNU General Public License
* along with this library.
*/
/**
* Button to prevent hardware key logging and to hamper software key logging
*/
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.accessibility.AccessibleContext;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import cologne.eck.all_peas.data.PeaProperties;
@SuppressWarnings("serial")
class KeyButton extends JButton implements MouseListener {
/* // this is for the strategy "hiding mouse cursor"
private static final BufferedImage img = new BufferedImage(1, 1,
BufferedImage.TYPE_INT_ARGB);
private static final Cursor zeroCursor = Toolkit.getDefaultToolkit().createCustomCursor(
img , new Point(0,0), "zeroCursor");
*/
// the current default cursor
private static final Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);
public KeyButton() { // avoid any displaying of events
super.setContentAreaFilled(false);
this.setRolloverEnabled(false);
this.setFocusPainted(false);
this.setBorder(BorderFactory.createBevelBorder(1, Color.gray, Color.LIGHT_GRAY)); // Two Colors Inner Bevel
this.addMouseListener(this);
}
public KeyButton(String text) {
super(text);
super.setContentAreaFilled(false);
this.setRolloverEnabled(false);
this.setFocusPainted(false);
this.setBorder(BorderFactory.createBevelBorder(1, Color.gray, Color.LIGHT_GRAY)); // Two Colors Inner Bevel
this.addMouseListener(this);
super.getAccessibleContext().setAccessibleDescription(PeaProperties.getBundle().getString("type") + ": " + text);
}
public KeyButton(Icon icon) {
super(icon);
}
@Override
protected void paintComponent(Graphics g) {
// the same background for all actions
// avoid vulnerabilities to register keystroke
if (getModel().isPressed()) {
g.setColor(PswDialogView.getPeaColor());//Color.WHITE);
} else if (getModel().isRollover()) {
g.setColor(PswDialogView.getPeaColor());//Color.WHITE);
} else {
g.setColor(PswDialogView.getPeaColor());//Color.WHITE);
}
g.fillRect(0, 0, getWidth(), getHeight());
super.paintComponent(g);
}
public void setNewBackground(Color color) {
this.setBackground(color);
}
@Override
public void mouseClicked(MouseEvent mc) {}
@Override
public void mouseEntered(MouseEvent e) {
// Blinking with swingTimer?
}
@Override
public void mouseExited(MouseEvent e) {
this.setCursor(defaultCursor);
}
@Override
public void mousePressed(MouseEvent e) {
/*
// Variant 1: hide cursor - against screenshots of whole screen
this.setCursor(zeroCursor);
try {
Thread.sleep(200);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
this.setCursor(defaultCursor);
*/
// Variant 2: cover the area of the button - against 50*50 screenshots of buttons
int size = 64;
BufferedImage img = new BufferedImage(size, size,// win max: 32 * 32...
BufferedImage.TYPE_BYTE_INDEXED);
Graphics2D graph = img.createGraphics();
// graph.setColor(PswDialogView.getPeaColor());//Color.WHITE);//LIGHT_GRAY);
graph.fill(new Rectangle(0, 0, size, size));
graph.dispose();
Toolkit toolkit = Toolkit.getDefaultToolkit();
Cursor c = null;
try {
c = toolkit.createCustomCursor(
img , new Point(size / 2, size / 2), "img");
} catch (IndexOutOfBoundsException ioobe){ // Windows: max 32 * 32
c = toolkit.createCustomCursor(
img , new Point(size / 4, size / 4), "img");
}
this.setCursor((c));
try { // cover only 0.1 second
Thread.sleep(100);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
this.setCursor(defaultCursor);
}
@Override
public void mouseReleased(MouseEvent e) {}
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessibleKeyButton();
}
return accessibleContext;
}
protected class AccessibleKeyButton extends AccessibleJButton {
//Inherit everything, override nothing.
}
}