//---------------------------------------------------------------------- // ナンバープレイス // // Copyright(C) 1996 Hirofumi Fujiwara // 本プログラムは、改造も含め、その商用利用は禁止します。 //---------------------------------------------------------------------- import java.awt.*; import java.applet.*; import java.lang.*; import java.io.*; import java.util.*; import java.net.*; public class NumberPlace extends Applet { final static int CTRLPANEL_Y = 5; final static int CTRLPANEL_H = 45; final static int SIZE = 9; int bd_offx, bd_offy; Dimension app_size; // アプレットのサイズ int unit = 0; int fontsize; int buttonfontsize; boolean curChanged = false; boolean valChanged = false; int curX = -1; int curY = -1; int oldX = -1; int oldY = -1; Font boxfont = null; Panel controlPanel, buttonPanel; public History history = new History( this ); public int probTable[][] = new int[SIZE][SIZE]; public int ansTable[][] = new int[SIZE][SIZE]; public int guessTable[][] = new int[SIZE][SIZE]; public boolean redTable[][] = new boolean[SIZE][SIZE]; public NumberButton buttons[] = new NumberButton[SIZE+1]; int fillcount[] = new int[SIZE+1]; public void init() { String filename = getParameter("problemfile"); app_size = this.size(); unit = (int)(app_size.width*0.92)/SIZE; String str = getParameter("unitsize"); if( str != null ) { try { unit = (Integer.valueOf(str)).intValue(); } catch( NoSuchElementException e ) {} } fontsize = (unit*16)/20; buttonfontsize = (unit*15)/20; setLayout( new BorderLayout() ); setBackground( Color.white ); bd_offx = ( app_size.width - unit * SIZE ) / 2; bd_offy = CTRLPANEL_H + 15; controlPanel = new Panel(); controlPanel.setLayout(new FlowLayout()); buttonPanel = new Panel(); buttonPanel.setLayout(new FlowLayout()); controlPanel.add( new ControlButton( this, "Back", " Back " ) ); controlPanel.add( new ControlButton( this, " ", " " ) ); controlPanel.add( new ControlButton( this, "Replay", " Replay " ) ); controlPanel.add( new ControlButton( this, "Check", " Check " ) ); Panel pd = new Panel(); pd.setLayout( new BorderLayout() ); add( "South", pd ); for( int i=1; i<=SIZE; ++i ) { NumberButton nb = new NumberButton( this, i, buttonfontsize ); buttonPanel.add( nb ); buttons[i] = nb; } add( "North", controlPanel ); controlPanel.reshape( 0, CTRLPANEL_Y, app_size.width, CTRLPANEL_H ); pd.add( "Center", buttonPanel ); buttonPanel.reshape( 0, app_size.height-22-50+5, app_size.width, 50 ); Label copyright = new Label("Number Place V0.3" + " " + "Copyright(C)1996,2000 Hirofumi Fujiwara.", Label.CENTER ); pd.add( "South", copyright ); copyright.reshape( 0, app_size.height-22, app_size.width, 18 ); loadData( filename ); } // 問題、解答の読み込み void loadData( String filename ) { URL url; DataInputStream file; try { url = new URL( getDocumentBase(), filename ); file = new DataInputStream( url.openStream() ); } catch( MalformedURLException e ) { showStatus( "file name error : " + filename ); return; } catch( IOException e ) { showStatus( "file open error : " + filename ); return; } String line; try { boolean isproblem = false; boolean isanswer = false; int x, y=0, v; nextline: for(;;) { line=file.readLine(); if( line == null ) break; if( '#' == line.charAt(0) ) continue; StringTokenizer st = new StringTokenizer(line); try { for( x=0; ; ++x ) { String tk = st.nextToken(); if( tk.equals("problem") ) { isproblem = true; y = 0; continue nextline; } if( tk.equals("answer") ) { isproblem = false; isanswer = true; y = 0; continue nextline; } if( tk.equals("end") ) { break nextline; } if( tk.equals("-") ) v = 0; else v = (Integer.valueOf(tk)).intValue(); if( isproblem ) probTable[x][y] = v; if( isanswer ) ansTable[x][y] = v; } } catch( NoSuchElementException e ) {} ++y; } } catch( IOException e ) {} initFillCount(); } void initFillCount() { for( int i=0; i 0 ) isused[v] = true; } // 垂直方向 for( i=0; i 0 ) isused[v] = true; } // 太枠内部 int bx = (x / 3) * 3; int by = (y / 3) * 3; for( i=bx; i 0 ) isused[v] = true; } } } int toXPos( int i ) { return bd_offx + i * unit + (i/3-1)*3 + 1; } int toYPos( int i ) { return bd_offy + i * unit + (i/3-1)*3 + 1; } boolean isInside() { return isInside( curX, curY ); } boolean isInside( int i, int j ) { if( i<0 || i>=SIZE ) return false; if( j<0 || j>=SIZE ) return false; return true; } void drawChecked( Graphics g, int x, int y ) { g.setColor( Color.black ); g.drawRect( toXPos(x)+2, toYPos(y)+2, unit-6, unit-6 ); } void drawNumber( Graphics g, Color c, int x, int y ) { drawNumber( g, c, x, y, guessTable[x][y] ); } void drawNumber( Graphics g, Color c, int x, int y, int v ) { if( boxfont == null ) { boxfont = new java.awt.Font( "Helvetica", 0, fontsize ); } if( v == 0 ) return; g.setColor( c ); g.setFont( boxfont ); String s = String.valueOf(v); FontMetrics fm = g.getFontMetrics(); int sWidth = fm.stringWidth(s); int sHeight = fm.getHeight(); int sAscent = fm. getAscent(); g.drawString( s, toXPos(x)+(unit-sWidth)/2, toYPos(y)+(unit+sAscent)/2-2 ); } public void setValue( int i, boolean ispush ) { if( ! isInside( curX, curY ) ) return; int ov = guessTable[curX][curY]; if( ov > 0 ) { if( fillcount[ov] == SIZE ) buttons[ov].show(); --fillcount[ov]; } if( i > 0 ) { ++fillcount[i]; if( fillcount[i] == SIZE ) buttons[i].hide(); } guessTable[curX][curY] = i; if( ispush ) { history.push( curX, curY, ov, i ); } valChanged = true; repaint(); } public void replay() { history.clean(); initFillCount(); curX = curY = -1; for( int i=0; i= history.length ) return; history[current][0] = x; history[current][1] = y; history[current][2] = o; history[current][3] = v; ++current; if( last < current ) last = current; } public void pop() { if( current <= 0 ) return; --current; int x = history[current][0]; int y = history[current][1]; int o = history[current][2]; int v = history[current][3]; np.changePos( x, y ); np.setValue( o, false ); } } //-------------------------------------------------------------------------------- // 情報ダイアログ //-------------------------------------------------------------------------------- class InformationDialog extends Frame { Applet app; protected Button button; public InformationDialog( Applet a, Color col, String title, String message ) { app = a; setTitle( title ); setFont(new Font("TimesRoman", Font.BOLD, 32)); this.setLayout( new BorderLayout( 30, 30 ) ); setBackground( col ); Label label = new Label( message ); add( "Center", label ); button = new Button( "OK" ); Panel p = new Panel(); p.setLayout( new FlowLayout( FlowLayout.CENTER, 30, 30 ) ); p.add( button ); add( "South", p ); pack(); } public void popup() { Frame frame = (Frame)app.getParent(); Point pos = frame.location(); Dimension size = frame.size(); Dimension mysize = size(); move( pos.x+(size.width-mysize.width)/2, pos.y+(size.height-mysize.height)/2 ); show(); } public boolean action( Event e, Object obj ) { if( e.target == button ) { hide(); dispose(); return true; } else return false; } public boolean getFocus( Event e, Object obj ) { button.requestFocus(); return true; } } //-------------------------------------------------------------------------------- // end of "NumberPlace.java", by Hirofumi Fujiwara //--------------------------------------------------------------------------------