アラビア数字とローマ数字を相互に自動変換する Java
アプレットです。
変換前の数値入力欄には、デフォルトでmcmxcixが与えられています。
| アラビア数字←→ローマ数字相互自動変換アプレット |
つかいかた
ご参考
ご注意
/*
RomanArabic.java
(c)1999 A.Matsuda
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.Toolkit.*;
import java.applet.*;
import java.util.*;
import java.lang.*;
import java.text.*;
import java.lang.System.*;
import arabic;
import roman;
public class RomanArabic extends java.applet.Applet
implements ActionListener, ItemListener
{
arabic arabic = new arabic();
roman roman = new roman();
Label label1, label2;
Label resultLabel;
int fontsize = 48;
String fontFace = "ゴシック";
String choiceString[]={ "結果を大文字で表示", "結果を小文字で表示" };
String convbuttonFace = "変換する";
TextField infield;
TextField outfield;
Label outlabel;
Button convbutton;
Choice choice;
int windowWidth = 680;
int windowHeight= 200;
Font textFont;
Font outTextFont;
Font buttonFont;
infieldTextListener iftl = new infieldTextListener();
class infieldTextListener implements TextListener
{
String statestr = "ボタン押下で自動変換";
String resultstr = "入力中(ボタンを押すとローマ数字はアラビア数字に、アラビア数字はローマ数字に変換します)";
public void textValueChanged(TextEvent e)
{
if( e.getID() == TextEvent.TEXT_VALUE_CHANGED )
{
if( !resultLabel.getText().equals( resultstr ) ){
resultLabel.setText( resultstr );
}
if( !outfield.getText().equals( statestr ) ){
outfield.setText( statestr );
}
showStatus("入力中");
}
};
};
public String getAppletInfo() {
return "アラビア数字←→ローマ数字相互変換アプレット";
};
public String [][]getParameterInfo(){
String [][] info = {
{"convvalue", "文字列", "変換元の値(アラビア数字でもローマ数字でもどちらでもいいよ)"},
{"backgroundcolor", "16進RGB", "背景色" },
};
return info;
};
public void itemStateChanged(ItemEvent e) {
}
public void actionPerformed(ActionEvent e) {
String arg = e.getActionCommand();
String str;
String buf;
boolean bUpperCase = false;
if ( convbuttonFace.equals( arg ) ) { //イベントってさぁ、ボタンフェイス
str = choice.getSelectedItem();
if ( choiceString[ 0 ].equals( str ) ){
bUpperCase = true;
}
buf = conv( infield.getText() ,bUpperCase );
outfield.setText( buf );
}
}
public Color getBackgroundColorParam()
{
String str;
int i;
str = getParameter( "backgroundcolor" );
try{
i = Integer.parseInt( str, 16 );
}
catch( NumberFormatException e ){
return new Color( 0xFFFFFF );
}
return new Color( i );
}
public void init(){
if( arabic == null ){
arabic = new arabic();
}
if( roman == null ){
roman = new roman();
}
textFont = new Font(fontFace, Font.BOLD, fontsize );
outTextFont = new Font(fontFace, Font.BOLD, fontsize );
buttonFont = new Font(fontFace, Font.BOLD, fontsize / 2 );
setBackground( getBackgroundColorParam() );
convbutton = new Button( convbuttonFace );
convbutton.addActionListener(this); //リスナの登録
convbutton.setFont( buttonFont );
infield = new TextField( "", 19 );
infield.addTextListener( iftl ); //リスナの登録
infield.setSize( infield.getPreferredSize() );
infield.setFont( textFont );
infield.setEditable( true );
outfield = new TextField( "", 19 );
outfield.setFont( outTextFont );
outfield.setEditable( false );
outfield.setSize( outfield.getPreferredSize() );
choice = new Choice();
for( int i = 0 ; i < choiceString.length; i++ ){
choice.addItem(choiceString[i]);
}
resultLabel = new Label();
label1 = new Label( "↓この欄に変換するローマ数字またはアラビア数字のいずれかを入力して下さい");
this.add(label1);
this.add(infield);
this.add(resultLabel);
this.add(convbutton);
this.add(choice);
this.add(outfield);
infield.setText( getParameter( "convvalue" ) );
outfield.setText( "変換結果表示域" );
resize( windowWidth,windowHeight );
}
public void paint(Graphics g){
g.setFont( textFont );
label1.print(g);
}
public void doLayout()
{
int x;
int y;
super.doLayout();
if( resultLabel == null || infield == null || choice == null ||
convbutton == null || outfield == null || label1 == null ){
return ;
}
label1.setLocation( 0,0 );
infield.setLocation( 0, label1.getSize().height );;
resultLabel.setLocation( 10, label1.getSize().height + infield.getSize().height + 2 );
outfield.setLocation( 0, 4 + infield.getSize().height + resultLabel.getSize().height + label1.getSize().height );
convbutton.setLocation( 2+infield.getSize().width, 5+label1.getSize().height );
choice.setLocation( 2+infield.getSize().width, 4 + infield.getSize().height + resultLabel.getSize().height + label1.getSize().height );
}
String conv( String src, boolean bUpperCase ){
arabic arabic = new arabic();
roman roman = new roman();
int val;
String dst;
src.trim();
if( "".equals(src) ){
resultLabel.setText( "ボタンを押した意図が把握できませんでした" );
resultLabel.setSize( infield.getSize().width, resultLabel.getSize().height );
showStatus( "中身がカラッポでした" );
dst = "カラッポ";
return dst;
}
try{
val = arabic.arabic( src );
}
catch( Exception e ){
boolean berr = false;
try{
dst = roman.roman( src, bUpperCase );
}
catch( Exception ee ){
berr = true;
dst = ee.getMessage();
resultLabel.setText( "変換に失敗しました" );
resultLabel.setSize( infield.getSize().width, resultLabel.getSize().height );
showStatus( "変換に失敗しました" );
}
if( berr == false ){
resultLabel.setText( "アラビア数字からローマ数字へ変換しました(結果はこちら↓)" );
resultLabel.setSize( infield.getSize().width, resultLabel.getSize().height );
showStatus( "アラビア数字からローマ数字へ変換しました" );
}
return dst;
}
try{
dst = new Integer( val ).toString();
}
catch( NumberFormatException e ){
resultLabel.setText( "変換に失敗しました" );
resultLabel.setSize( infield.getSize().width, resultLabel.getSize().height );
showStatus( "変換に失敗しました" );
return "変換に失敗しました";
}
resultLabel.setText( "ローマ数字からアラビア数字へ変換しました(結果はこちら↓)" );
resultLabel.setSize( infield.getSize().width, resultLabel.getSize().height );
showStatus( "ローマ数字からアラビア数字へ変換しました" );
return dst;
}
//でバグ用
public static void main( String arg[] )
{
arabic arabic = new arabic();
roman roman = new roman();
int i;
int val;
for( i = 0 ; i < arg.length ; i++ ){
try{
val = arabic.arabic( arg[ i ] );
}
catch( Exception e ){
String str;
try{
str = roman.roman( arg[i], false );
}
catch( Exception ee ){
System.out.println( ee.getMessage() );
continue;
}
System.out.println( str );
continue;
}
System.out.println( arg[i] + "->" + new Integer(val).toString() );
}
}
};
ソースプログラムのダウンロードはこちらをクリックして下さい。
ご注意
<ホームページへ> <ローマ数字変換へ> <アラビア数字→ローマ数字自動変換へ> <ローマ数字→アラビア数字自動変換へ>
Copyright © MCMXCIX Ayumi Matsuda All rights reserved.