//
// OvalButton class
// part of the set of documents known as Java no sugar.
// Copyright (c) 1996 Sunil Gupta, sunil@magnetic.demon.co.uk
// placed into the public domain by the author
//
import java.awt.*;
public class OvalButton extends Canvas
{
//##################################################################
//
//##################################################################
private static final int BUTTON_UP = 1;
private static final int BUTTON_DOWN = 2;
private static final double ROOT2 = 1.414213562373;
private static final double FUDGEFACTOR = 1.66666;
public int CLICK_INTERVAL = 100; //milliseconds
String caption;
int button_state = BUTTON_UP;
long button_down_time;
//##################################################################
//
//##################################################################
public OvalButton( String a_string)
{
caption = a_string;
}
//******************************************************************
// initial size must be set in addNotify, not in constructor or
// tharr be null pointers
public void addNotify()
{
super.addNotify();
resize(this.preferredSize());
}
//******************************************************************
public void paint (Graphics g)
{
Dimension size;
int x, y, text_width, text_height, oval_width, oval_height;
FontMetrics metrics;
Color colour;
//--------------------------------------------------------------
size = size();
metrics = getFontMetrics(getFont());
text_width = metrics.stringWidth(caption);
text_height = metrics.getHeight();
//------draw button shape --- bugs in oval as you can see.------
g.setColor( getBackground() );
g.fillRect(0,0,size.width, size.height);
oval_width = size.width - 3;
oval_height = size.height - 3;
if (button_state == BUTTON_UP)
{
g.setColor( Color.white );
g.fillOval(0,0, oval_width,oval_height);
g.setColor( Color.gray );
g.fillOval(2,2,oval_width,oval_height);
}
else
{
g.setColor( Color.gray );
g.fillOval(0,0, oval_width,oval_height);
g.setColor( Color.white );
g.fillOval(2,2,oval_width,oval_height);
}
g.setColor( Color.lightGray );
g.fillOval(1,1, oval_width,oval_height);
//-------------draw button text---------------------------------
x = (size.width - text_width) /2;
y = (size.height + text_height) /2;
if (button_state == BUTTON_UP)
{
g.setColor( Color.gray );
g.drawString(caption, x-1, y-1);
g.setColor( Color.white );
g.drawString(caption, x+1, y+1);
}
else
{
g.setColor( Color.gray );
g.drawString(caption, x-1, y-1);
g.drawString(caption, x+1, y+1);
}
if ( isEnabled() )
g.setColor( getForeground() );
else
g.setColor( Color.gray );
g.drawString(caption, x, y);
}
//******************************************************************
public Dimension minimumSize()
{
return (this.preferredSize());
}
//******************************************************************
public Dimension preferredSize()
{
int text_width, text_height;
int oval_width, oval_height;
FontMetrics metrics;
Font font;
Dimension preferred_dim;
font = getFont();
metrics = getFontMetrics( font);
text_width = metrics.stringWidth(caption);
text_height = metrics.getHeight();
oval_width = (int) Math.round( text_width * ROOT2 * FUDGEFACTOR);
oval_height = (int) Math.round( text_height * ROOT2);
preferred_dim = new Dimension( oval_width , oval_height);
return preferred_dim;
}
//******************************************************************
public boolean handleEvent(Event e)
{
long time_diff;
switch (e.id)
{
case Event.MOUSE_DOWN:
button_down_time = System.currentTimeMillis();
button_state = BUTTON_DOWN;
repaint();
return true;
case Event.MOUSE_UP:
time_diff = System.currentTimeMillis() - button_down_time;
if (time_diff <= CLICK_INTERVAL)
postEvent(new Event(this,Event.ACTION_EVENT,this));
button_state = BUTTON_UP;
repaint();
return true;
default:
return super.handleEvent(e);
}
}
}
| file: /Techref/language/java/nosugar/custom/button/code/OvalButton.java, 4KB, , updated: 1997/2/3 23:21, local time: 2012/5/25 08:24,
38.107.179.232:LOG IN |
| ©2012 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://www.piclist.com/techref/language/java/nosugar/custom/button/code/OvalButton.java"> language java nosugar custom button code OvalButton</A> |
| Did you find what you needed? |
LCD Front Panel Set: $8.99!
- 2x16 LCD HD44780 controller - 4 push buttons - 3 LEDs - 2x12 header |
Robotics nuts!Check out http://users.frii.com/dlc/robotics/projects/botproj.htm from Dennis Clark. This guy ROCKS! He has made (and sells but also releases code, docs, etc...) for a number of cool little robotic modules including whiskers, IR proximity detect and remote control, Sonar proximity detect, PWM, Servo, compass. Most of these use the little PIC 12C508 controller which costs basically nothing and is soooo tiny.The 4 servos, 2400 baud serial servo controller is a wonder of magic and he sells the programmed chip for $8. Wow! |
.