画像などにより独自のボタンを作成してみる -- 菱形ボタン追加
戻る
::::::::::::::
DecisionButton.java
::::::::::::::
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
/**
$Id: uitest.html,v 1.1 2009/06/22 16:12:32 kishi Exp kishi $
@author KISHI Yasuhiro
*/
public class DecisionButton extends JButton {
private int x[];
private int y[];
public DecisionButton( String text ) {
super( text );
x = new int[ 4 ];
y = new int[ 4 ];
setContentAreaFilled( false );
}
protected void paintComponent( Graphics g ) {
if ( getModel().isArmed() ) {
g.setColor( Color.lightGray );
} else {
g.setColor( getBackground() );
}
// ひし形の描画
int width = getSize().width - 1;
int height = getSize().height - 1;
int cx = width / 2;
int cy = height / 2;
// ひし形の各頂点を求める
x[ 0 ] = cx;
x[ 1 ] = width;
x[ 2 ] = cx;
x[ 3 ] = 0;
y[ 0 ] = 0;
y[ 1 ] = cy;
y[ 2 ] = height;
y[ 3 ] = cy;
g.setColor( Color.green ); // 色を緑に設定
g.fillPolygon( x, y, 4 ); // ひし形をfillPolygon()で描画
super.paintComponent( g );
}
protected void paintBorder( Graphics g ) {
g.setColor( getForeground() );
g.drawPolygon( x, y, 4 ); // ひし形の外枠を描画
}
}
::::::::::::::
DrawingPanel.java
::::::::::::::
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
/**
$Id: uitest.html,v 1.1 2009/06/22 16:12:32 kishi Exp kishi $
@author KISHI Yasuhiro
*/
public class DrawingPanel extends JPanel implements ActionListener {
public DrawingPanel() {
super();
// レイアウトマネージャーは使用しない
setLayout( null );
MyButton button = new MyButton();
// 幅・高さはオリジナルのサイズと合わせる
button.setBounds( 100, 200, 100, 79 );
button.addActionListener( this );
button.setActionCommand( "画像ボタン" );
button.addMouseMotionListener( new MouseDraggedEventHandler( this, button ) );
OvalButton entryPoint = new OvalButton( "EntryPoint" );
entryPoint.setBounds( 10, 10, 100, 100 );
entryPoint.addActionListener( this );
entryPoint.setActionCommand( "入口" );
entryPoint.addMouseMotionListener( new MouseDraggedEventHandler( this, entryPoint ) );
OvalButton exitPoint = new OvalButton( "ExitPoint" );
exitPoint.setBounds( 50, 300, 100, 100 );
exitPoint.addActionListener( this );
exitPoint.setActionCommand( "出口" );
exitPoint.addMouseMotionListener( new MouseDraggedEventHandler( this, exitPoint ) );
DecisionButton decision = new DecisionButton( "DECISION" );
decision.setBounds( 150, 300, 100, 60 );
decision.addActionListener( this );
decision.setActionCommand( "判断" );
decision.addMouseMotionListener( new MouseDraggedEventHandler( this, decision ) );
this.add( button );
this.add( entryPoint );
this.add( exitPoint );
this.add( decision );
}
public void actionPerformed( ActionEvent e ) {
System.out.println( "command: " + e.getActionCommand() );
System.out.println( "Clicked ... " + e.getSource() );
}
public void paintComponent( Graphics g ) {
int width = getWidth();
int height = getHeight();
// 画面クリア
g.setColor( Color.black );
// このコンポーネントのサイズを取得する
g.fillRect( 0 , 0 , width , height );
// ひし形の描画を試す
int cx = width / 2;
int cy = height / 2;
int x [] = {cx, width, cx, 0}; // 菱形の各頂点のX座標値
int y [] = {0, cy, height, cy}; // 菱形の各頂点のY座標値
g.setColor( Color.blue ); // 色を青に設定
g.fillPolygon( x, y, 4 ); // 菱形をfillPolygon()で描画
}
}
::::::::::::::
MouseDraggedEventHandler.java
::::::::::::::
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;
/**
$Id: uitest.html,v 1.1 2009/06/22 16:12:32 kishi Exp kishi $
@author KISHI Yasuhiro
*/
public class MouseDraggedEventHandler implements MouseMotionListener {
private JComponent component;
private Container container;
public MouseDraggedEventHandler( Container container, JComponent component ) {
this.container = container;
this.component = component;
}
public void mouseDragged( MouseEvent argEvent ) {
System.out.printf( "(%3d,%3d)\n", argEvent.getX() , argEvent.getY() );
if ( argEvent.getX() < 0 || argEvent.getY() < 0 ) {
Rectangle objBounds = component.getBounds();
component.setLocation( objBounds.x + argEvent.getX(), objBounds.y + argEvent.getY() );
} else {
Rectangle objBounds = component.getBounds();
if ( objBounds.x <= objBounds.x + argEvent.getX() && objBounds.x + argEvent.getX() <= objBounds.x + objBounds.width &&
objBounds.y <= objBounds.y + argEvent.getY() && objBounds.y + argEvent.getY() <= objBounds.y + objBounds.height ) {
component.setLocation( objBounds.x + argEvent.getX(), objBounds.y + argEvent.getY() );
} else {
component.setLocation( argEvent.getX(), argEvent.getY() );
}
}
container.repaint();
}
public void mouseMoved( MouseEvent argEvent ) { }
}
::::::::::::::
MyButton.java
::::::::::::::
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
/**
$Id: uitest.html,v 1.1 2009/06/22 16:12:32 kishi Exp kishi $
@author KISHI Yasuhiro
*/
public class MyButton extends JButton {
public MyButton() {
super( new ImageIcon( "test.gif" ) );
// setBackground(Color.blue);
setContentAreaFilled( false );
}
}
::::::::::::::
OvalButton.java
::::::::::::::
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
/**
$Id: uitest.html,v 1.1 2009/06/22 16:12:32 kishi Exp kishi $
@author KISHI Yasuhiro
*/
public class OvalButton extends JButton {
public OvalButton( String text ) {
super( text );
setContentAreaFilled( false );
}
protected void paintComponent( Graphics g ) {
if ( getModel().isArmed() ) {
g.setColor( Color.lightGray );
} else {
g.setColor( getBackground() );
}
g.fillOval( 0, 0, getSize().width - 1, getSize().height - 1 );
super.paintComponent( g );
}
protected void paintBorder( Graphics g ) {
g.setColor( getForeground() );
g.drawOval( 0, 0, getSize().width - 1, getSize().height - 1 );
}
Shape shape;
public boolean contains( int x, int y ) {
if ( shape == null || !shape.getBounds().equals( getBounds() ) ) {
shape = new Ellipse2D.Float( 0, 0, getWidth(), getHeight() );
}
return shape.contains( x, y );
}
}
::::::::::::::
UITest.java
::::::::::::::
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
/**
$Id: uitest.html,v 1.1 2009/06/22 16:12:32 kishi Exp kishi $
*/
public class UITest extends JFrame {
private int width = 640;
private int height = 480;
public UITest() {
super( "オリジナルのJComponentを作ってみる --- その2" );
Container contentPane = getContentPane();
contentPane.setLayout( new FlowLayout() );
DrawingPanel dp = new DrawingPanel();
dp.setPreferredSize( new Dimension( width, height ) );
contentPane.add( dp );
}
public static void main( String[] args ) {
UITest frame = new UITest();
/* 終了処理を追加 */
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit( 0 );
}
}
);
/** サイズと位置を指定 */
frame.setBounds( 0, 0, frame.width, frame.height );
frame.setVisible( true );
frame.pack();
}
}
戻る