Click here to Skip to main content
15,886,067 members
Articles / Programming Languages / Java

Java Console Apps Made Easy

Rate me:
Please Sign up or sign in to vote.
4.83/5 (20 votes)
17 Mar 2018CPOL5 min read 133.7K   7.1K   27  
This article describes the creation of a Java console type application.
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions