アラビア数字からローマ数字へ自動変換する Java
アプレットです。
変換前の数値入力欄には、デフォルトで起動時の時間×100+分が与えられています。
| アラビア数字→ローマ数字自動変換アプレット |
つかいかた
- アラビア数字を、変換元の数値入力欄に入力する。
- ボタンを押す。
- 変換後の数値が表示されるので、コピーするなり適当に料理る。
ご参考
ご注意
/*
(c)1987, 1999 A.Matsuda
pbuf range "1" .. "4999"
*/
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.*;
class infieldTextListener implements TextListener
{
public void textValueChanged(TextEvent e)
{
if( e.getID() == TextEvent.TEXT_VALUE_CHANGED )
{
}
};
};
public class roman extends java.applet.Applet
implements ActionListener, ItemListener
{
Label label1, label2;
boolean answerMode = false;
infieldTextListener iftl;
int fontsize = 48;
String fontFace = "ゴシック";
String choiceString[]={ "結果を大文字で表示", "結果を小文字で表示" };
String convbuttonFace = "変換する";
TextField infield;
TextField outfield;
Label outlabel;
Button convbutton;
Choice choice;
int windowWidth = 500;
int windowHeight= 150;
Font textFont;
Font outTextFont;
Font buttonFont;
public String getAppletInfo() {
return "アラビア数字→ローマ数字変換アプレット";
};
public String [][]getParameterInfo(){
String [][] info = {
{"convvalue", "1〜4999の範囲内の10進数またはHOURまたはYEARまたはMONTH", "文字列。[変換元アラビア数字 | HOUR | YEAR | MONTH ]"},
{"answermode", "文字列", "trueでアンサーモード" },
{"backgroundcolor", "16進RGB", "背景色" },
};
return info;
};
public void actionPerformed(ActionEvent e) {
String arg = e.getActionCommand();
if ( convbuttonFace.equals( arg ) ) { //イベントってさぁ、ボタンフェイス
String str;
int maxlen ;
boolean bUpperCase = false;
if( isAnswerMode() == true ){
bUpperCase = true;
str = getDefaultConvValue();
try{
str = roman( str, bUpperCase );
}
catch( Exception except ){
str = except.getMessage();
}
outlabel.setText( str );
convbutton.setEnabled( false );
}
else{
str = choice.getSelectedItem();
if ( choiceString[ 0 ].equals( str ) ){
bUpperCase = true;
}
str = infield.getText();
try{
str = roman( str, bUpperCase );
}
catch( Exception except ){
str = except.getMessage();
}
outfield.setText( str );
}
showStatus( str );
}
};
public void textValueChanged(TextEvent e)
{
if( e.getID() == (TextEvent.TEXT_VALUE_CHANGED) )
{
}
}
boolean isAnswerMode()
{
String val;
val = getParameter( "answermode" );
if( val == null ){
return false;
}
if( "true".equals( val )){
return true;
}
return false;
}
public String getDefaultConvValue()
{
String defaultValue;
defaultValue = getParameter( "convvalue" );
if( "MONTH".equals( defaultValue ) ){
return new Integer(
Calendar.getInstance().get( Calendar.MONTH ) * 100 +
Calendar.getInstance().get( Calendar.DATE ) ).toString();
}
if( "HOUR".equals( defaultValue ) ){
return new Integer(
Calendar.getInstance().get( Calendar.HOUR ) * 100 +
Calendar.getInstance().get( Calendar.MINUTE ) ).toString();
}
if( "YEAR".equals( defaultValue ) ){
return new Integer(
Calendar.getInstance().get( Calendar.YEAR ) ).toString();
}
return defaultValue;
}
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 itemStateChanged(ItemEvent e) {
}
public void init(){
textFont = new Font(fontFace, Font.BOLD, fontsize );
outTextFont = new Font(fontFace, Font.BOLD, fontsize );
buttonFont = new Font(fontFace, Font.BOLD, fontsize / 2 );
setBackground( getBackgroundColorParam() );
if( isAnswerMode() == true ){
windowWidth = 620;
windowHeight= 75;
textFont = new Font(fontFace, Font.BOLD, fontsize );
convbuttonFace = "答え";
convbutton = new Button( convbuttonFace );
convbutton.addActionListener(this); //リスナの登録
convbutton.setFont( buttonFont );
outlabel = new Label( " ", Label.LEFT );
outlabel.setFont( outTextFont );
this.add(convbutton);
this.add(outlabel);
}
else{
windowWidth = 550;
windowHeight= 150;
convbutton = new Button( convbuttonFace );
convbutton.addActionListener(this); //リスナの登録
convbutton.setFont( buttonFont );
infield = new TextField( "", 5 );
infield.setSize( infield.getPreferredSize() );
infield.addTextListener( iftl ); //リスナの登録
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]);
}
resize( windowWidth,windowHeight );
infield.setText( getDefaultConvValue() );
outfield.setText( "変換結果表示域" );
label1 = new Label("ここにアラビア数字を入力→" );
label2 = new Label("↓変換結果表示" );
this.add( label1 );
this.add( label2 );
this.add(infield);
this.add(convbutton);
this.add(choice);
this.add(outfield);
}
}
public int max( int a, int b )
{
if( a > b )
return a;
return b;
}
public int min( int a, int b )
{
if( a < b )
return a;
return b;
}
public void doLayout()
{
int x;
int y;
super.doLayout();
if( label1 == null || label2 == null || choice == null ||
convbutton == null || outfield == null || infield == null ){
return ;
}
label1.setLocation( 0 , 0 );
x = label1.getSize().width;
infield.setLocation( x, 0 );
label1.setLocation( infield.getLocation().x - label1.getSize().width , 0 );
x += infield.getSize().width + 20;
convbutton.setLocation( x, 2 );
y = convbutton.getSize().height + 4;
choice.setLocation( x, y );
y = infield.getSize().height +10;
outfield.setLocation( 0, y );
label2.setLocation( 0, y - label2.getSize().height );
}
public void paint(Graphics g){
g.setFont( textFont );
// convbutton.paint( g );
// choice.paint(g);
// infield.paint(g);
// outfield.paint(g);
}
public static void main(String argv[])
{
};
public String roman( String buf , boolean bUpper ) throws Exception
{
byte astr[];
String trimed;
String num;
int i;
int len;
String tbl[ ][ ] = {
{ "", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix", },
{ "", "x", "xx", "xxx", "xl", "l", "lx", "lxx", "lxxx", "xc", },
{ "", "c", "cc", "ccc", "cd", "d", "dc", "dcc", "dccc", "cm", },
{ "", "m", "mm", "mmm", "mmmm", "", "", "", "", "", }
};
trimed = buf.trim();
try{
i = new Integer( trimed ).intValue();
}
catch( NumberFormatException e ){
throw new Exception( "不正な数です" );
}
trimed = new Integer(i).toString() ; //再変換
len = trimed.length();
astr = trimed.getBytes();
StringBuffer roman = new StringBuffer();
if( i >= 5000 || 0 >= i ){
throw new Exception( "変換範囲外です" );
}
len = astr.length;
i = 0;
switch( len ){
default:
throw new Exception( "無効な数値です" );
case 4: // down through
roman.append( tbl[ len - i - 1 ][ (astr[ i ] - '0') ] );
i++;
case 3: // down through
roman.append( tbl[ len - i - 1 ][ (astr[ i ] - '0') ] );
i++;
case 2: // down through
roman.append( tbl[ len - i - 1 ][ (astr[ i ] - '0') ] );
i++;
case 1:
roman.append( tbl[ len - i - 1 ][ (astr[ i ] - '0') ] );
}
if( bUpper == true ){
return roman.toString().toUpperCase();
}
return roman.toString();
}
};
ソースプログラムのダウンロードはこちらをクリックして下さい。
ご注意
<ホームページへ> <ローマ数字変換へ> <アラビア数字→ローマ数字自動変換へ> <アラビア数字←→ローマ数字相互自動変換へ>
Copyright © MCMXCIX Ayumi Matsuda All rights reserved.