- JavaConsole_src.zip
- Console Menu
- Console Menu
- .classpath
- .project
- bin
- consoleWithMenu
- ConsoleWithMenu$1.class
- ConsoleWithMenu$2.class
- ConsoleWithMenu$3.class
- ConsoleWithMenu$4.class
- ConsoleWithMenu$5.class
- ConsoleWithMenu$6.class
- ConsoleWithMenu.class
- javaConsole
- BlockCaret.class
- JavaConsole$1.class
- JavaConsole.class
- menu
- Menu$MenuItem.class
- Menu.class
- MenuCallback.class
- MenuTest$1.class
- MenuTest.class
- src
- consoleWithMenu
- javaConsole
- menu
- test
- menu
- JavaConsole_demo.zip
- JavaConsoleTest.jar
|
package javaConsole;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;
import javax.swing.text.JTextComponent;
/**
* @brief Custom block caret for the Java Console.
*
* @par Comments:
* Adapted from http://www.java2s.com/Code/Java/Swing-JFC/Acustomcaretclass.htm
*
* @author David MacDermot
*
* @date 02-07-2012
*
* @bug
*/
public class BlockCaret extends DefaultCaret {
private static final long serialVersionUID = 1L;
/**
* @brief Class Constructor
*/
public BlockCaret() {
setBlinkRate(500); // half a second
}
/* (non-Javadoc)
* @see javax.swing.text.DefaultCaret#damage(java.awt.Rectangle)
*/
protected synchronized void damage(Rectangle r) {
if (r == null)
return;
// give values to x,y,width,height (inherited from java.awt.Rectangle)
x = r.x;
y = r.y;
height = r.height;
// A value for width was probably set by paint(), which we leave alone.
// But the first call to damage() precedes the first call to paint(), so
// in this case we must be prepared to set a valid width, or else
// paint()
// will receive a bogus clip area and caret will not get drawn properly.
if (width <= 0)
width = getComponent().getWidth();
repaint(); //Calls getComponent().repaint(x, y, width, height) to erase
repaint(); // previous location of caret. Sometimes one call isn't enough.
}
/* (non-Javadoc)
* @see javax.swing.text.DefaultCaret#paint(java.awt.Graphics)
*/
public void paint(Graphics g) {
JTextComponent comp = getComponent();
if (comp == null)
return;
int dot = getDot();
Rectangle r = null;
char dotChar;
try {
r = comp.modelToView(dot);
if (r == null)
return;
dotChar = comp.getText(dot, 1).charAt(0);
} catch (BadLocationException e) {
return;
}
if(Character.isWhitespace(dotChar)) dotChar = '_';
if ((x != r.x) || (y != r.y)) {
// paint() has been called directly, without a previous call to
// damage(), so do some cleanup. (This happens, for example, when
// the text component is resized.)
damage(r);
return;
}
g.setColor(comp.getCaretColor());
g.setXORMode(comp.getBackground()); // do this to draw in XOR mode
width = g.getFontMetrics().charWidth(dotChar);
if (isVisible())
g.fillRect(r.x, r.y, width, r.height);
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.