PDA

View Full Version : Simulating Flashing Textbox line in Graphics2D



Steve
September 28th, 2010, 00:07
I guess since EVERYBODY HERE DOES RSPS, this is kinda useless, but anyone making their own game, and have customs textboxes, you could use this to simulate the flashing line after the last character. There are some problems, like since im using cursive in mine, the L is diagonal so the white line interferes. Oh and i use a thread to redraw it, the thread does it about every 1 second then repaints, so you might want to add double buffering so you don't have the flashy stuff happen. Anyway this is what i use.



/**
* Draws a flashing line, well simulates it
* @param shouldFlash If it's true, draw a black line, if false, draw a white line.
*/
public void drawFlashingLine(boolean shouldFlash) {
Graphics2D g2 = (Graphics2D) this.getGraphics();
Font font1 = new Font("Monotype Corsiva", Font.PLAIN, 20); //Your font
g2.setFont(font1);
FontMetrics fm = g2.getFontMetrics();
int x = X + fm.stringWidth(textBox) + 3; //X axis position to start, textBox is the string that i add to everytime a key is typed
if (shouldFlash) {
g2.drawLine(x, 363, x, 382);
} else {
g2.setColor(Color.WHITE); //Change to the textboxes color background
g2.drawLine(x, 363, x, 382);
}

}
The result,

Only the registered members can see the link.



Edit: I've been getting past my mental block, and have been figuring some things out, here's something to center shapes.



/**
* Paints a centered shape on your canvas
* @param g2 The graphics
* @param shape The shape we get the info from. Then convert
* @return The formatted shape, if you need to call it from somewhere else.
*/
public Shape paintCenteredShape(Graphics2D g2, Shape shape) {
Shape original = shape;
int x = (getSize().width / 2) - original.getBounds().width / 2;
Shape centered = new Rectangle(x, original.getBounds().y, original.getBounds().width, original.getBounds().height);
g2.fill(centered);
return centered;
}
}It's a shape because i use that in other methods i have. I THINK it's centered.

Snowz
September 28th, 2010, 04:53
Nice job, pretty cool, won't use though.

Emily
September 28th, 2010, 04:55
Good tutorial(snippet), to bad finding the coordinates for the center was hard :p

Steve
September 28th, 2010, 21:09
Good tutorial(snippet), to bad finding the coordinates for the center was hard :p
Yeah my gray square is still off, because i know the text is centered. Eh i'll figure it out.

Angel`
October 1st, 2010, 17:14
awsome, I doubt I'm going to ever use it, but if I kno anyone making their own game I'll direct them to this for the box.