📄 arrowbutton.java
字号:
/*====================================================================*\ArrowButton.javaArrow button class.------------------------------------------------------------------------This file is part of FuncPlotter, a combined Java application and appletfor plotting explicit functions in one variable.Copyright 2005-2007 Andy Morgan-Richards.FuncPlotter is free software: you can redistribute it and/or modify itunder the terms of the GNU General Public License as published by theFree Software Foundation, either version 3 of the License, or (at youroption) any later version.This program is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public License alongwith this program. If not, see <http://www.gnu.org/licenses/>.\*====================================================================*/// PACKAGEpackage gui;//----------------------------------------------------------------------// IMPORTSimport java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Rectangle;import javax.swing.JComponent;//----------------------------------------------------------------------// ARROW BUTTON CLASSpublic class ArrowButton extends JComponent{////////////////////////////////////////////////////////////////////////// Constants//////////////////////////////////////////////////////////////////////// public enum Direction { UP, DOWN, LEFT, RIGHT } private static final int MIN_WIDTH = 1; private static final int MAX_WIDTH = (1 << 15) - 1; private static final int MIN_HEIGHT = 1; private static final int MAX_HEIGHT = (1 << 15) - 1; private static final int MIN_ARROW_SIZE = 1; private static final int MAX_ARROW_SIZE = (1 << 15) - 1; private static final Color BORDER_COLOUR = new Color( 96, 96, 96 ); private static final Color BACKGROUND_COLOUR = new Color( 188, 216, 188 ); private static final Color ACTIVE_BACKGROUND_COLOUR = new Color( 236, 192, 88 ); private static final Color DISABLED_BACKGROUND_COLOUR = new Color( 208, 208, 208 ); private static final Color FOREGROUND_COLOUR = Color.BLACK; private static final Color DISABLED_FOREGROUND_COLOUR = new Color( 144, 144, 144 );////////////////////////////////////////////////////////////////////////// Constructors//////////////////////////////////////////////////////////////////////// public ArrowButton( int width, int height, int arrowSize, Direction direction ) throws IllegalArgumentException { // Validate arguments if ( (width < MIN_WIDTH) || (width > MAX_WIDTH) || (height < MIN_HEIGHT) || (height > MAX_HEIGHT) || (arrowSize < MIN_ARROW_SIZE) || (arrowSize > MAX_ARROW_SIZE) || (direction == null) ) throw new IllegalArgumentException( ); // Initialise instance variables this.width = width; this.height = height; this.arrowSize = arrowSize; this.direction = direction; // Set attributes setOpaque( true ); setFocusable( false ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods : overriding methods//////////////////////////////////////////////////////////////////////// public Dimension getPreferredSize( ) { return new Dimension( width, height ); } //------------------------------------------------------------------ protected void paintComponent( Graphics gr ) { // Fill background Rectangle rect = gr.getClipBounds( ); gr.setColor( isEnabled( ) ? pressed ? ACTIVE_BACKGROUND_COLOUR : BACKGROUND_COLOUR : DISABLED_BACKGROUND_COLOUR ); gr.fillRect( rect.x, rect.y, rect.width, rect.height ); // Draw arrow gr.setColor( isEnabled( ) ? FOREGROUND_COLOUR : DISABLED_FOREGROUND_COLOUR ); switch ( direction ) { case UP: { int x1 = (width - 1) / 2; int x2 = x1; int y = (height - arrowSize) / 2; for ( int i = 0; i < arrowSize; ++i ) { gr.drawLine( x1, y, x2, y ); --x1; ++x2; ++y; } break; } case DOWN: { int x1 = (width - 1) / 2; int x2 = x1; int y = height - 1 - (height - arrowSize) / 2; for ( int i = 0; i < arrowSize; ++i ) { gr.drawLine( x1, y, x2, y ); --x1; ++x2; --y; } break; } case LEFT: { int x = (width - arrowSize) / 2; int y1 = (height - 1) / 2; int y2 = y1; for ( int i = 0; i < arrowSize; ++i ) { gr.drawLine( x, y1, x, y2 ); ++x; --y1; ++y2; } break; } case RIGHT: { int x = width - 1 - (width - arrowSize) / 2; int y1 = (height - 1) / 2; int y2 = y1; for ( int i = 0; i < arrowSize; ++i ) { gr.drawLine( x, y1, x, y2 ); --x; --y1; ++y2; } break; } } // Draw border gr.setColor( BORDER_COLOUR ); gr.drawRect( 0, 0, width - 1, height - 1 ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods//////////////////////////////////////////////////////////////////////// public Direction getDirection( ) { return direction; } //------------------------------------------------------------------ public boolean isPressed( ) { return pressed; } //------------------------------------------------------------------ public void setPressed( boolean pressed ) { if ( this.pressed != pressed ) { this.pressed = pressed; repaint( ); } } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance variables//////////////////////////////////////////////////////////////////////// private int width; private int height; private int arrowSize; private Direction direction; private boolean pressed;}//----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -