please dont rip this site

Language Java Nosugar Custom Button Code Ovalbutton.java

// 
//	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 22:21, local time: 2024/11/26 19:52,
TOP NEW HELP FIND: 
18.189.184.99:LOG IN

 ©2024 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?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://techref.massmind.org/Techref/language/java/nosugar/custom/button/code/OvalButton.java"> language java nosugar custom button code OvalButton</A>

Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to techref.massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .